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 += ";"; des += ";";
System.out.println(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); System.out.println(sig);
if (sig.charAt(sig.length() - 1) != (";").charAt(0)) { if (sig.charAt(sig.length() - 1) != (";").charAt(0)) {
sig += ";"; sig += ";";

View File

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

View File

@ -1,7 +1,11 @@
package de.dhbwstuttgart.bytecode.signature; package de.dhbwstuttgart.bytecode.signature;
import java.util.ArrayList;
import java.util.Iterator; 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.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
@ -12,6 +16,15 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.type.TypeVisitor; import de.dhbwstuttgart.syntaxtree.type.TypeVisitor;
public class TypeToSignature implements TypeVisitor<String> { 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 @Override
public String visit(RefType refType) { public String visit(RefType refType) {
@ -33,7 +46,7 @@ public class TypeToSignature implements TypeVisitor<String> {
// } else { // } else {
// params += "L"+param.toString().replace(".", "/"); // params += "L"+param.toString().replace(".", "/");
// } // }
params += param.acceptTV(new TypeToSignature()); params += param.acceptTV(new TypeToSignature(constraints));
if(param instanceof TypePlaceholder) if(param instanceof TypePlaceholder)
params += ";"; params += ";";
@ -48,7 +61,7 @@ public class TypeToSignature implements TypeVisitor<String> {
@Override @Override
public String visit(SuperWildcardType superWildcardType) { public String visit(SuperWildcardType superWildcardType) {
// throw new NotImplementedException(); // throw new NotImplementedException();
String sig = "-" + superWildcardType.getInnerType().acceptTV(new TypeToSignature()); String sig = "-" + superWildcardType.getInnerType().acceptTV(new TypeToSignature(constraints));
if(superWildcardType.getInnerType() instanceof TypePlaceholder) if(superWildcardType.getInnerType() instanceof TypePlaceholder)
sig += ";"; sig += ";";
return sig; return sig;
@ -57,13 +70,21 @@ public class TypeToSignature implements TypeVisitor<String> {
@Override @Override
public String visit(TypePlaceholder typePlaceholder) { public String visit(TypePlaceholder typePlaceholder) {
// return typePlaceholder.toString().replace(".", "/"); // 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 @Override
public String visit(ExtendsWildcardType extendsWildcardType) { public String visit(ExtendsWildcardType extendsWildcardType) {
// throw new NotImplementedException(); // throw new NotImplementedException();
String sig = "+" + extendsWildcardType.getInnerType().acceptTV(new TypeToSignature()); String sig = "+" + extendsWildcardType.getInnerType().acceptTV(new TypeToSignature(constraints));
if(extendsWildcardType.getInnerType() instanceof TypePlaceholder) if(extendsWildcardType.getInnerType() instanceof TypePlaceholder)
sig += ";"; sig += ";";
return sig; return sig;
@ -73,5 +94,10 @@ public class TypeToSignature implements TypeVisitor<String> {
public String visit(GenericRefType genericRefType) { public String visit(GenericRefType genericRefType) {
return genericRefType.getParsedName().replace(".", "/"); 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); resolver = new Resolver(resultSet);
classOrInterface.accept(tphExtractor); classOrInterface.accept(tphExtractor);
tphsClass = tphExtractor.tphsClass; tphsClass = tphExtractor.tphsClass;
simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass);
if(!isVisited) { if(!isVisited) {
simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass);
ggResult = GenericsGenerator.generateConstraints(className, tphExtractor, tphsClass,simplifiedConstraints); ggResult = GenericsGenerator.generateConstraints(className, tphExtractor, tphsClass,simplifiedConstraints);
isVisited = true; isVisited = true;
} else {
} }
// for(Constructor m : classOrInterface.getConstructors()) { // for(Constructor m : classOrInterface.getConstructors()) {