From 3ecb202a90fab727353d55e6e1a951aff8d1db80 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 31 Jul 2019 11:55:24 +0200 Subject: [PATCH] Generic generator algorithm v1 --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 2 +- .../bytecode/signature/Signature.java | 6 ++-- .../bytecode/signature/TypeToSignature.java | 36 ++++++++++++++++--- .../simplifyRes/GeneratedGenericsFinder.java | 5 +-- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java index cce7262c..71d71f99 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -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 += ";"; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java b/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java index bad62616..b92dc2c3 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -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())); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java b/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java index 24792550..e35573b0 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java @@ -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 { + private List constraints; + + public TypeToSignature() { + this.constraints = new ArrayList<>(); + } + + public TypeToSignature(List constraints) { + this.constraints = constraints; + } @Override public String visit(RefType refType) { @@ -33,7 +46,7 @@ public class TypeToSignature implements TypeVisitor { // } 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 { @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 { @Override public String visit(TypePlaceholder typePlaceholder) { // return typePlaceholder.toString().replace(".", "/"); - return "T" + typePlaceholder.getName() + "$"; + String name = typePlaceholder.getName(); + + if(!constraints.isEmpty()){ + Optional 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 { public String visit(GenericRefType genericRefType) { return genericRefType.getParsedName().replace(".", "/"); } - + + private Optional getEqualTPHFromClassConstraints(List listOfConstraints, String tph) { + return listOfConstraints.stream() + .filter(c -> c.getConstraint().getLeft().equals(tph) || c.getEqualsTPHs().contains(tph)) + .findFirst(); + } } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GeneratedGenericsFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GeneratedGenericsFinder.java index 372633e9..29e1aad8 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GeneratedGenericsFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GeneratedGenericsFinder.java @@ -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()) {