From f2d6dae4c77dde63aec955459131ddf54d5c6916 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 16 Feb 2021 15:29:24 +0100 Subject: [PATCH] Letzer Stand mit Pair Bildug waehrend des Typvars sammeln --- .../dhbwstuttgart/bytecode/TPHExtractor.java | 47 ++++++++++++++----- .../GeneratedGenericsFinder.java | 16 ++++--- .../FamilyOfGeneratedGenerics.java | 3 +- .../bytecode/utilities/MethodAndTPH.java | 7 +-- .../syntaxtree/AbstractASTWalker.java | 3 ++ .../de/dhbwstuttgart/syntaxtree/Method.java | 10 +++- .../typeinference/typeAlgo/TYPE.java | 4 +- 7 files changed, 65 insertions(+), 25 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java index d13ef5f42..77d51b647 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java @@ -60,9 +60,17 @@ public class TPHExtractor extends AbstractASTWalker { /* - * ordnet den Methodennamen ihre jeweiligen Methodenrespraesentationen der abstrakte Syntax in der jeweiligen aktuellen Klasse zu + * 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; @@ -207,8 +215,9 @@ 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, (TypePlaceholder)method.getReturnType()); + methodAndTph = new MethodAndTPH(id, method.getReturnType()); inLocalOrParamOrReturn = true; method.getReturnType().accept(this); @@ -232,7 +241,7 @@ public class TPHExtractor extends AbstractASTWalker { .block .statements .stream() - .filter(s -> s instanceof Assign) + .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())) ; @@ -247,8 +256,10 @@ public class TPHExtractor extends AbstractASTWalker { @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); @@ -258,6 +269,12 @@ public class TPHExtractor extends AbstractASTWalker { @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; } @@ -267,7 +284,7 @@ public class TPHExtractor extends AbstractASTWalker { super.visit(assign); RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(assign.rightSide.getType()).resolvedType; RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(assign.lefSide.getType()).resolvedType; - if (left instanceof TypePlaceholder && methodAndTph.getLocalTphs().contains(((TypePlaceholder)left).getName())) { + if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) { if (right instanceof TypePlaceholder) { methodAndTph.addPair(allPairs.stream().filter( p -> (p.getLeft() instanceof TypePlaceholder && @@ -284,21 +301,25 @@ public class TPHExtractor extends AbstractASTWalker { 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 fieldOrMethName; + String fieldLocalvarOrMethName; super.visit(methodCall); - if (nameToMeth.keySet().contains(fieldOrMethName = methodCall.name) //Method-call of a method in the actuial Sourcefile + 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(fieldOrMethName = ((FieldVar)((ExpressionReceiver)methodCall.receiver).expr).fieldVarName) + nameToField.keySet().contains(fieldLocalvarOrMethName = ((FieldVar)((ExpressionReceiver)methodCall.receiver).expr).fieldVarName) || (((ExpressionReceiver)methodCall.receiver).expr instanceof LocalVar) && - nameToField.keySet().contains(fieldOrMethName = ((LocalVar)((ExpressionReceiver)methodCall.receiver).expr).name) + nameToLocalVar.keySet().contains(fieldLocalvarOrMethName = ((LocalVar)((ExpressionReceiver)methodCall.receiver).expr).name) ) //LOKALE VARIABLEN MUESSE NOCH IN nameToField EINGETRAGEN WERDEN ) @@ -306,8 +327,10 @@ public class TPHExtractor extends AbstractASTWalker { { Iterator paraIt = methodCall.name.equals("apply") - ? ((RefType)(resultSet.resolveType((nameToField.get(fieldOrMethName))).resolvedType)).getParaList().iterator() - : nameToMeth.get(methodCall.name) + ? (((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() @@ -318,7 +341,7 @@ public class TPHExtractor extends AbstractASTWalker { .forEach(x -> { RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(x.getType()).resolvedType; RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(paraIt.next()).resolvedType; - if (left instanceof TypePlaceholder && methodAndTph.getLocalTphs().contains(((TypePlaceholder)left).getName())) { + if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) { if (right instanceof TypePlaceholder) { methodAndTph.addPair(allPairs.stream().filter( p -> (p.getLeft() instanceof TypePlaceholder && @@ -339,7 +362,7 @@ public class TPHExtractor extends AbstractASTWalker { if (!inLambdaExpression) { RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(aReturn.retexpr.getType()).resolvedType; RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(methodAndTph.getReturnType()).resolvedType; - if (left instanceof TypePlaceholder && methodAndTph.getLocalTphs().contains(((TypePlaceholder)left).getName())) { + if (left instanceof TypePlaceholder && methodAndTph.getTphs().contains(((TypePlaceholder)left).getName())) { if (right instanceof TypePlaceholder) { methodAndTph.addPair(allPairs.stream().filter( p -> (p.getLeft() instanceof TypePlaceholder && diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java index 6a89bde3c..8366368fe 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java @@ -142,7 +142,8 @@ public class GeneratedGenericsFinder implements ASTVisitor { System.out.println("fogg.classConstraints: "+ fogg.classConstraints); System.out.println("fogg.methodConstraintsWithPosition: "+ fogg.methodConstraintsWithPosition); - /* + ///* + //Fayez Ansatz Anfang tphsClass = tphExtractor.tphsClass; //PL 2020-01-15 //Es muss ggResult aus fogg gebildet werden @@ -156,13 +157,15 @@ public class GeneratedGenericsFinder implements ASTVisitor { addMethodConstraints(simplifiedConstraints, ggResult, m); } - */ + if(ggResult != null) { //Hinzufuegen von Fayez ggResult - //generatedGenericsForSF.addGenericGeneratorResultClass(ggResult); + generatedGenericsForSF.addGenericGeneratorResultClass(ggResult); } - - + // Fayez Ansatz Ende + //*/ + + //Ali Ansatz Anfang List listOfClassCons = new ArrayList<>(); for(TPHConstraint clCons: fogg.classConstraints) { // ExtendsConstraint ec = new ExtendsConstraint(clCons.getLeft(), clCons.getRight()); @@ -186,10 +189,11 @@ public class GeneratedGenericsFinder implements ASTVisitor { ggResultAlternative = new GenericsGeneratorResultForClass(className, listOfClassCons, ggRfaM); if(ggResultAlternative != null) {//hinzufuegen von Alis ggResult - generatedGenericsForSF.addGenericGeneratorResultClass(ggResultAlternative); + //generatedGenericsForSF.addGenericGeneratorResultClass(ggResultAlternative); System.out.println(generatedGenericsForSF); } System.out.println(ggResultAlternative); + //Ali Ansatz Ende } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index 9de58c594..100dd1a27 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -69,7 +69,8 @@ public class FamilyOfGeneratedGenerics { ArrayList> methodConstraints1Pairs, List listOfMethodsAndTph) { List cs_m = new ArrayList<>(); - //List methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs); + //List methodConstraints1 = + typeOfTheMethodInClSigma(cs, posOfTphs); List methodConstraints1 = methodConstraints1Pairs.stream() diff --git a/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java b/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java index e26eca109..570ef7204 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java @@ -4,6 +4,7 @@ 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; @@ -20,9 +21,9 @@ public class MethodAndTPH { /* * Returntyp der zugehoerigen Methode in er abstrakten Syntax */ - TypePlaceholder returnType; + RefTypeOrTPHOrWildcardOrGeneric returnType; - public MethodAndTPH(String name, TypePlaceholder returnType) { + public MethodAndTPH(String name, RefTypeOrTPHOrWildcardOrGeneric returnType) { this.id = name; this.returnType = returnType; } @@ -50,7 +51,7 @@ public class MethodAndTPH { basePairs.add(p); } - public TypePlaceholder getReturnType() { + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { return returnType; } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 9d0d29b0d..0cbad8975 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -126,6 +126,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{ public void visit(Assign assign) { assign.lefSide.accept(this); assign.rightSide.accept(this); + assign.rightSide.getType().accept((ASTVisitor) this); } @Override @@ -186,6 +187,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{ public void visit(MethodCall methodCall) { methodCall.receiver.accept(this); methodCall.getArgumentList().accept(this); + methodCall.getArgumentList().getArguments().forEach(a -> a.getType().accept((ASTVisitor) this)); } @@ -212,6 +214,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{ @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); + aReturn.getType().accept((ASTVisitor) this); } @Override diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java b/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java index 13e36b7b3..412fbd976 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java @@ -31,8 +31,14 @@ public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope private ExceptionList exceptionlist; private GenericDeclarationList generics; private final RefTypeOrTPHOrWildcardOrGeneric returnType; - public final Boolean isInherited; - + public final Boolean isInherited; + + /* + * its Constraints + * eingefuegt PL 2021-02-15 + */ + public final ConstraintSet constraints = new ConstraintSet(); + 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/typeinference/typeAlgo/TYPE.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index c84fa1435..685ac9f62 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -32,8 +32,10 @@ public class TYPE { private ConstraintSet getConstraintsClass(ClassOrInterface cl, TypeInferenceInformation info) { ConstraintSet ret = new ConstraintSet(); + ConstraintSet methConstrains; for(Method m : cl.getMethods()){ - ret.addAll(getConstraintsMethod(m,info, cl)); + ret.addAll(methConstrains = getConstraintsMethod(m,info, cl)); + m.constraints.addAll(methConstrains); } for(Constructor m : cl.getConstructors()){ ret.addAll(getConstraintsConstructor(m,info, cl));