diff --git a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java index 77d51b64..9b895199 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java @@ -9,7 +9,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint; @@ -25,18 +24,8 @@ import de.dhbwstuttgart.syntaxtree.Field; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.ParameterList; -import de.dhbwstuttgart.syntaxtree.SourceFile; -import de.dhbwstuttgart.syntaxtree.statement.Assign; -import de.dhbwstuttgart.syntaxtree.statement.AssignToField; -import de.dhbwstuttgart.syntaxtree.statement.Expression; -import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; -import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver; -import de.dhbwstuttgart.syntaxtree.statement.FieldVar; import de.dhbwstuttgart.syntaxtree.statement.LocalVar; import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; -import de.dhbwstuttgart.syntaxtree.statement.MethodCall; -import de.dhbwstuttgart.syntaxtree.statement.Return; -import de.dhbwstuttgart.syntaxtree.statement.Statement; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -52,32 +41,12 @@ import de.dhbwstuttgart.typeinference.result.ResultSet; public class TPHExtractor extends AbstractASTWalker { // Alle TPHs der Felder werden iKopf der Klasse definiert // alle TPHs der Klasse: (TPH, is in Method?) - - /* - * ordnet den Methodennamen ihre jeweiligen Methodenrespraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu - */ - public HashMap nameToMeth; - - - /* - * ordnet den Fieldnamen ihre jeweiligen Lambda-respraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu - */ - public HashMap nameToField; - - /* - * ordnet den Variablennamen ihre jeweiligen Lambda-Respraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu - * wird in jeder Methode neu initialisiert - * Einträge werden nicht geloescht, da das Java-Programm korrekt ist und in umgebenden Bloecken nicht die gleichen Variablen vorkommen können - */ - public HashMap nameToLocalVar; - public final HashMap allTPHS = new HashMap<>(); public final List tphsClass = new ArrayList<>(); MethodAndTPH methodAndTph; Boolean inMethod = false; boolean inLocalOrParamOrReturn = false; - Boolean inLambdaExpression = false; public final ArrayList ListOfMethodsAndTph = new ArrayList<>(); final ArrayList> allPairs = new ArrayList<>(); @@ -86,7 +55,7 @@ public class TPHExtractor extends AbstractASTWalker { private Resolver resolver; public TPHExtractor() { - + } public void setResultSet(ResultSet resultSet) { @@ -215,9 +184,8 @@ public class TPHExtractor extends AbstractASTWalker { @Override public void visit(Method method) { inMethod = true; - this.nameToLocalVar = new HashMap<>(); String id = MethodUtility.createID(resolver,method); - methodAndTph = new MethodAndTPH(id, method.getReturnType()); + methodAndTph = new MethodAndTPH(id); inLocalOrParamOrReturn = true; method.getReturnType().accept(this); @@ -231,152 +199,23 @@ public class TPHExtractor extends AbstractASTWalker { ListOfMethodsAndTph.add(methodAndTph); } - @Override - public void visit(ClassOrInterface classOrInterface) { - Optional optInitialConstructor = classOrInterface.getfieldInitializations(); - this.nameToField = new HashMap<>(); - if (optInitialConstructor.isPresent()) { - classOrInterface.getfieldInitializations() - .get() - .block - .statements - .stream() - .filter(s -> s instanceof Assign && ((Assign)s).rightSide instanceof LambdaExpression) - .forEach(as -> - this.nameToField.put(((AssignToField)(((Assign)as).lefSide)).field.fieldVarName, - ((AssignToField)(((Assign)as).lefSide)).getType())) ; - } - - this.nameToMeth = new HashMap<>(); - for(Method m : classOrInterface.getMethods()) { - this.nameToMeth.put(m.getName(), m); - } - super.visit(classOrInterface); - } - @Override public void visit(Constructor cons) { - this.nameToLocalVar = new HashMap<>(); inMethod = false; //super.visit(cons); - cons.getParameterList().accept(this); if(cons.block != null) cons.block.accept(this); inMethod = true; } - @Override - public void visit(LambdaExpression lambdaExpression) { - inLambdaExpression = true; - lambdaExpression.params.getFormalparalist().forEach(param -> { - RefTypeOrTPHOrWildcardOrGeneric varType = resultSet.resolveType(param.getType()).resolvedType; - if (varType instanceof RefType && ((RefType)varType).getName().getClassName().endsWith("$$")) { - this.nameToLocalVar.put(param.getName(), param.getType()); - } - }); - super.visit(lambdaExpression); - inLambdaExpression = false; - } - - @Override - public void visit(Assign assign) { - super.visit(assign); - RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(assign.rightSide.getType()).resolvedType; - RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(assign.lefSide.getType()).resolvedType; - if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) { - if (right instanceof TypePlaceholder) { - methodAndTph.addPair(allPairs.stream().filter( - p -> (p.getLeft() instanceof TypePlaceholder && - p.getRight() instanceof TypePlaceholder) && - ((TypePlaceholder)p.getLeft()).getName().equals(((TypePlaceholder)left).getName()) && - ((TypePlaceholder)p.getRight()).getName().equals(((TypePlaceholder)right).getName()) - ).findFirst() - .get()); - } - } - } - @Override public void visit(LocalVarDecl localVarDecl) { // inLocalOrParamOrReturn = inMethod; super.visit(localVarDecl); - RefTypeOrTPHOrWildcardOrGeneric varType = resultSet.resolveType(localVarDecl.getType()).resolvedType; - if (varType instanceof RefType && ((RefType)varType).getName().getClassName().endsWith("$$")) { - this.nameToLocalVar.put(localVarDecl.getName(), localVarDecl.getType()); - } // inLocalOrParamOrReturn = false; } - - @Override - public void visit(MethodCall methodCall) { - String fieldLocalvarOrMethName; - super.visit(methodCall); - if (nameToMeth.keySet().contains(fieldLocalvarOrMethName = methodCall.name) //Method-call of a method in the actuial Sourcefile - || (methodCall.name.equals("apply") && - (methodCall.receiver instanceof ExpressionReceiver) && - ((((ExpressionReceiver)methodCall.receiver).expr instanceof FieldVar) && - nameToField.keySet().contains(fieldLocalvarOrMethName = ((FieldVar)((ExpressionReceiver)methodCall.receiver).expr).fieldVarName) - || (((ExpressionReceiver)methodCall.receiver).expr instanceof LocalVar) && - nameToLocalVar.keySet().contains(fieldLocalvarOrMethName = ((LocalVar)((ExpressionReceiver)methodCall.receiver).expr).name) - ) - //LOKALE VARIABLEN MUESSE NOCH IN nameToField EINGETRAGEN WERDEN - ) - ) - { - Iterator paraIt = - methodCall.name.equals("apply") - ? (((ExpressionReceiver)methodCall.receiver).expr instanceof FieldVar) - ? ((RefType)(resultSet.resolveType((nameToField.get(fieldLocalvarOrMethName))).resolvedType)).getParaList().iterator() - : ((RefType)(resultSet.resolveType((nameToLocalVar.get(fieldLocalvarOrMethName))).resolvedType)).getParaList().iterator() - : nameToMeth.get(fieldLocalvarOrMethName) - .getParameterList() - .getFormalparalist() - .stream() - .map(p -> p.getType()) - .iterator(); - methodCall.getArgumentList() - .getArguments() - .forEach(x -> { - RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(x.getType()).resolvedType; - RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(paraIt.next()).resolvedType; - if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) { - if (right instanceof TypePlaceholder) { - methodAndTph.addPair(allPairs.stream().filter( - p -> (p.getLeft() instanceof TypePlaceholder && - p.getRight() instanceof TypePlaceholder) && - ((TypePlaceholder)p.getLeft()).getName().equals(((TypePlaceholder)left).getName()) && - ((TypePlaceholder)p.getRight()).getName().equals(((TypePlaceholder)right).getName()) - ).findFirst() - .get()); - } - } - }); - } - } - - @Override - public void visit(Return aReturn) { - super.visit(aReturn); - if (!inLambdaExpression) { - RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(aReturn.retexpr.getType()).resolvedType; - RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(methodAndTph.getReturnType()).resolvedType; - if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) { - if (right instanceof TypePlaceholder) { - methodAndTph.addPair(allPairs.stream().filter( - p -> (p.getLeft() instanceof TypePlaceholder && - p.getRight() instanceof TypePlaceholder) && - ((TypePlaceholder)p.getLeft()).getName().equals(((TypePlaceholder)left).getName()) && - ((TypePlaceholder)p.getRight()).getName().equals(((TypePlaceholder)right).getName()) - ).findFirst() - .get()); - } - } - } - } - - @Override public void visit(LocalVar localVar) { // inLocalOrParamOrReturn = inMethod; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java index 8366368f..a73d2b3c 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java @@ -66,7 +66,7 @@ import de.dhbwstuttgart.typeinference.result.ResultSet; * */ public class GeneratedGenericsFinder implements ASTVisitor { - private final TPHExtractor tphExtractor; + private final TPHExtractor tphExtractor = new TPHExtractor(); private Collection listOfResultSets; private SourceFile sf; private List tphsClass; @@ -86,7 +86,6 @@ public class GeneratedGenericsFinder implements ASTVisitor { public GeneratedGenericsFinder(SourceFile sf, Collection listOfResultSets) { this.sf = sf; this.listOfResultSets = listOfResultSets; - this.tphExtractor = new TPHExtractor(); } public GenericGenratorResultForSourceFile findGeneratedGenerics() { diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index 100dd1a2..3628ed1d 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -4,14 +4,10 @@ import de.dhbwstuttgart.bytecode.TPHExtractor; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.typeinference.result.ResultPair; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.stream.Collectors; - import org.objectweb.asm.Type; public class FamilyOfGeneratedGenerics { @@ -19,7 +15,7 @@ public class FamilyOfGeneratedGenerics { // HashMap speichert ob TPH in einer Methode oder in der Klasse ist; und wenn es in der Methode ist, in welcher Methode public HashMap>> posOfTPHs = new HashMap<>(); public List classConstraints = new ArrayList<>(); - //public List methodConstraints = new ArrayList<>(); + public List methodConstraints = new ArrayList<>(); public HashMap> methodConstraintsWithPosition = new HashMap<>(); private static final String objectType = Type.getInternalName(Object.class); @@ -28,7 +24,7 @@ public class FamilyOfGeneratedGenerics { this.allConstraints = tphExtractor.allCons; this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph); this.classConstraints = getClassConstraints(allConstraints,posOfTPHs); - //this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs, null, tphExtractor.ListOfMethodsAndTph); + this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs, tphExtractor.ListOfMethodsAndTph); this.methodConstraintsWithPosition = getMethodConstraintsWithPosition(allConstraints,classConstraints,posOfTPHs, tphExtractor.ListOfMethodsAndTph); } @@ -63,20 +59,9 @@ public class FamilyOfGeneratedGenerics { return cs_cl; } - public static List getMethodConstraints(List cs, - List cs_cl, - HashMap>> posOfTphs, - ArrayList> methodConstraints1Pairs, - List listOfMethodsAndTph) { + public static List getMethodConstraints(List cs, List cs_cl, HashMap>> posOfTphs, List listOfMethodsAndTph) { List cs_m = new ArrayList<>(); - //List methodConstraints1 = - typeOfTheMethodInClSigma(cs, posOfTphs); - - List methodConstraints1 = - methodConstraints1Pairs.stream() - .map(p -> new MethodConstraint(p.getLeft().getName(), p.getRight().getName(), Relation.EXTENDS)) - .collect(Collectors.toList()); - + List methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs); for (MethodConstraint cons: methodConstraints1) { if (!checkForDuplicates(cons, cs_m)) { cs_m.add(cons); @@ -137,7 +122,7 @@ public class FamilyOfGeneratedGenerics { } } } - listOfThisMethod = getMethodConstraints(cs,cs_cl,posOfTPHsForThisMethod, method.getBasePairs(),listOfMethodsAndTph); + listOfThisMethod = getMethodConstraints(cs,cs_cl,posOfTPHsForThisMethod,listOfMethodsAndTph); tempMethodConstraintsWithPosition.put(currentMethod, listOfThisMethod); } } @@ -264,7 +249,7 @@ public class FamilyOfGeneratedGenerics { } return tempMC; } - + /** * Def. FGG: zweite Zeile von cs_m diff --git a/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java b/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java index 570ef720..d8eed39c 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java @@ -4,7 +4,6 @@ import java.util.ArrayList; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.result.GenericInsertPair; import de.dhbwstuttgart.typeinference.result.ResultPair; @@ -16,16 +15,9 @@ public class MethodAndTPH { private final ArrayList> pairs = new ArrayList<>(); // tphs of local variables and parameters private final ArrayList localTphs = new ArrayList<>(); - private final ArrayList> basePairs = new ArrayList<>(); - - /* - * Returntyp der zugehoerigen Methode in er abstrakten Syntax - */ - RefTypeOrTPHOrWildcardOrGeneric returnType; - public MethodAndTPH(String name, RefTypeOrTPHOrWildcardOrGeneric returnType) { + public MethodAndTPH(String name) { this.id = name; - this.returnType = returnType; } public ArrayList getTphs() { @@ -47,16 +39,4 @@ public class MethodAndTPH { return localTphs; } - public void addPair(ResultPair p) { - basePairs.add(p); - } - - public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { - return returnType; - } - - public ArrayList> getBasePairs() { - return basePairs; - } - } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java b/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java index 412fbd97..13e36b7b 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java @@ -31,14 +31,8 @@ public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope private ExceptionList exceptionlist; private GenericDeclarationList generics; private final RefTypeOrTPHOrWildcardOrGeneric returnType; - public final Boolean isInherited; - - /* - * its Constraints - * eingefuegt PL 2021-02-15 - */ - public final ConstraintSet constraints = new ConstraintSet(); - + public final Boolean isInherited; + public Method(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block block, GenericDeclarationList gtvDeclarations, Token offset) { super(offset); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java index f9ec340b..4a22147b 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -45,12 +45,6 @@ public class SourceFile extends SyntaxTreeNode{ public List getClasses() { return KlassenVektor; } - - public List getAllMethods() { - List ret = new ArrayList<>(); - getClasses().forEach(cl -> ret.addAll(cl.getMethods())); - return ret; - } @Override public void accept(ASTVisitor visitor) { diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 685ac9f6..c84fa143 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -32,10 +32,8 @@ public class TYPE { private ConstraintSet getConstraintsClass(ClassOrInterface cl, TypeInferenceInformation info) { ConstraintSet ret = new ConstraintSet(); - ConstraintSet methConstrains; for(Method m : cl.getMethods()){ - ret.addAll(methConstrains = getConstraintsMethod(m,info, cl)); - m.constraints.addAll(methConstrains); + ret.addAll(getConstraintsMethod(m,info, cl)); } for(Constructor m : cl.getConstructors()){ ret.addAll(getConstraintsConstructor(m,info, cl));