Generic generator algorithm v1

This commit is contained in:
Fayez Abu Alia 2019-07-31 11:55:24 +02:00
parent efdb58e67c
commit 3ecb202a90
4 changed files with 38 additions and 11 deletions

View File

@ -454,7 +454,7 @@ public class BytecodeGen implements ASTVisitor {
}
des += ";";
System.out.println(des);
String sig = resultSet.resolveType(field.getType()).resolvedType.acceptTV(new TypeToSignature());
String sig = resultSet.resolveType(field.getType()).resolvedType.acceptTV(new TypeToSignature(generatedGenerics.getClassConstraints()));
System.out.println(sig);
if (sig.charAt(sig.length() - 1) != (";").charAt(0)) {
sig += ";";

View File

@ -358,7 +358,7 @@ public class Signature {
switch (type) {
case "RT":
String sig = t.acceptTV(new TypeToSignature());
String sig = t.acceptTV(new TypeToSignature(constraints));
sv.visitClassType(sig.substring(1, sig.length()));
break;
case "GRT":
@ -415,7 +415,7 @@ public class Signature {
case "SWC":
System.out.println("SWC---Signature");
SuperWildcardType swc = (SuperWildcardType) t;
String sigInner = swc.getInnerType().acceptTV(new TypeToSignature());
String sigInner = swc.getInnerType().acceptTV(new TypeToSignature(constraints));
if (swc.getInnerType() instanceof TypePlaceholder) {
sv.visitTypeArgument(SUPER_CHAR).visitTypeVariable(sigInner.substring(1, sigInner.length()));
} else if (swc.getInnerType() instanceof RefType) {
@ -433,7 +433,7 @@ public class Signature {
case "EWC":
System.out.println("EWC---Signature");
ExtendsWildcardType ewc = (ExtendsWildcardType) t;
String esigInner = ewc.getInnerType().acceptTV(new TypeToSignature());
String esigInner = ewc.getInnerType().acceptTV(new TypeToSignature(constraints));
System.out.println(esigInner);
if (ewc.getInnerType() instanceof TypePlaceholder) {
sv.visitTypeArgument(EXTENDS_CHAR).visitTypeVariable(esigInner.substring(1, esigInner.length()));

View File

@ -1,7 +1,11 @@
package de.dhbwstuttgart.bytecode.signature;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
@ -12,6 +16,15 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.type.TypeVisitor;
public class TypeToSignature implements TypeVisitor<String> {
private List<GenericsGeneratorResult> constraints;
public TypeToSignature() {
this.constraints = new ArrayList<>();
}
public TypeToSignature(List<GenericsGeneratorResult> constraints) {
this.constraints = constraints;
}
@Override
public String visit(RefType refType) {
@ -33,7 +46,7 @@ public class TypeToSignature implements TypeVisitor<String> {
// } else {
// params += "L"+param.toString().replace(".", "/");
// }
params += param.acceptTV(new TypeToSignature());
params += param.acceptTV(new TypeToSignature(constraints));
if(param instanceof TypePlaceholder)
params += ";";
@ -48,7 +61,7 @@ public class TypeToSignature implements TypeVisitor<String> {
@Override
public String visit(SuperWildcardType superWildcardType) {
// throw new NotImplementedException();
String sig = "-" + superWildcardType.getInnerType().acceptTV(new TypeToSignature());
String sig = "-" + superWildcardType.getInnerType().acceptTV(new TypeToSignature(constraints));
if(superWildcardType.getInnerType() instanceof TypePlaceholder)
sig += ";";
return sig;
@ -57,13 +70,21 @@ public class TypeToSignature implements TypeVisitor<String> {
@Override
public String visit(TypePlaceholder typePlaceholder) {
// return typePlaceholder.toString().replace(".", "/");
return "T" + typePlaceholder.getName() + "$";
String name = typePlaceholder.getName();
if(!constraints.isEmpty()){
Optional<GenericsGeneratorResult> equalName = getEqualTPHFromClassConstraints(constraints, name);
if(equalName.isPresent())
name = equalName.get().getConstraint().getLeft();
}
return "T" + name + "$";
}
@Override
public String visit(ExtendsWildcardType extendsWildcardType) {
// throw new NotImplementedException();
String sig = "+" + extendsWildcardType.getInnerType().acceptTV(new TypeToSignature());
String sig = "+" + extendsWildcardType.getInnerType().acceptTV(new TypeToSignature(constraints));
if(extendsWildcardType.getInnerType() instanceof TypePlaceholder)
sig += ";";
return sig;
@ -73,5 +94,10 @@ public class TypeToSignature implements TypeVisitor<String> {
public String visit(GenericRefType genericRefType) {
return genericRefType.getParsedName().replace(".", "/");
}
private Optional<GenericsGeneratorResult> getEqualTPHFromClassConstraints(List<GenericsGeneratorResult> listOfConstraints, String tph) {
return listOfConstraints.stream()
.filter(c -> c.getConstraint().getLeft().equals(tph) || c.getEqualsTPHs().contains(tph))
.findFirst();
}
}

View File

@ -144,11 +144,12 @@ public class GeneratedGenericsFinder implements ASTVisitor {
resolver = new Resolver(resultSet);
classOrInterface.accept(tphExtractor);
tphsClass = tphExtractor.tphsClass;
simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass);
if(!isVisited) {
simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass);
ggResult = GenericsGenerator.generateConstraints(className, tphExtractor, tphsClass,simplifiedConstraints);
isVisited = true;
} else {
}
// for(Constructor m : classOrInterface.getConstructors()) {