diff --git a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java index e3029932..a9e2162e 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java @@ -25,9 +25,11 @@ 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.LocalVar; import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; import de.dhbwstuttgart.syntaxtree.statement.MethodCall; +import de.dhbwstuttgart.syntaxtree.statement.Return; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -44,7 +46,12 @@ public class TPHExtractor extends AbstractASTWalker { // Alle TPHs der Felder werden iKopf der Klasse definiert // alle TPHs der Klasse: (TPH, is in Method?) public final SourceFile sf; + + /* + * ordnet den Methodennamen ihre jeweiligen Methodenrespraesentationen in der abstrakte Syntax zu + */ public final HashMap nameToMeth; + public final HashMap allTPHS = new HashMap<>(); public final List tphsClass = new ArrayList<>(); MethodAndTPH methodAndTph; @@ -193,7 +200,7 @@ public class TPHExtractor extends AbstractASTWalker { public void visit(Method method) { inMethod = true; String id = MethodUtility.createID(resolver,method); - methodAndTph = new MethodAndTPH(id); + methodAndTph = new MethodAndTPH(id, (TypePlaceholder)method.getReturnType()); inLocalOrParamOrReturn = true; method.getReturnType().accept(this); @@ -217,6 +224,24 @@ public class TPHExtractor extends AbstractASTWalker { inMethod = true; } + @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; @@ -228,20 +253,47 @@ public class TPHExtractor extends AbstractASTWalker { @Override public void visit(MethodCall methodCall) { super.visit(methodCall); + if (nameToMeth.keySet().contains(methodCall.name)) {//Method-call of a method in the actuial Sourcefile Iterator paraIt = nameToMeth.get(methodCall.name).getParameterList().iterator(); methodCall.getArgumentList() .getArguments() .forEach(x -> { - RefTypeOrTPHOrWildcardOrGeneric left = null; - RefTypeOrTPHOrWildcardOrGeneric right = null; - if ((left = x.getType()) instanceof TypePlaceholder) { - if ((right = paraIt.next().getType()) instanceof TypePlaceholder) { - methodAndTph.addPair(((TypePlaceholder)left).getName(), - ((TypePlaceholder)left).getName()); + RefTypeOrTPHOrWildcardOrGeneric left = resultSet.resolveType(x.getType()).resolvedType; + RefTypeOrTPHOrWildcardOrGeneric right = resultSet.resolveType(paraIt.next().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(Return aReturn) { + super.visit(aReturn); + 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/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index 3628ed1d..4859ab63 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -62,6 +62,7 @@ public class FamilyOfGeneratedGenerics { 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 = listOfMethodsAndTph. for (MethodConstraint cons: methodConstraints1) { if (!checkForDuplicates(cons, cs_m)) { cs_m.add(cons); @@ -249,7 +250,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 d8eed39c..de434ebb 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java @@ -15,9 +15,16 @@ 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 + */ + TypePlaceholder returnType; - public MethodAndTPH(String name) { + public MethodAndTPH(String name, TypePlaceholder returnType) { this.id = name; + this.returnType = returnType; } public ArrayList getTphs() { @@ -39,4 +46,12 @@ public class MethodAndTPH { return localTphs; } + public void addPair(ResultPair p) { + basePairs.add(p); + } + + public TypePlaceholder getReturnType() { + return returnType; + } + } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index b8c7cc5f..9d0d29b0 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -126,7 +126,6 @@ public abstract class AbstractASTWalker implements ASTVisitor{ public void visit(Assign assign) { assign.lefSide.accept(this); assign.rightSide.accept(this); - //assign.rightSide.getType().accept(this); } @Override @@ -187,11 +186,9 @@ public abstract class AbstractASTWalker implements ASTVisitor{ public void visit(MethodCall methodCall) { methodCall.receiver.accept(this); methodCall.getArgumentList().accept(this); - //methodCall.getArgumentList() - // .getArguments() - // .forEach(x -> x.getType().accept(this)); } + @Override public void visit(NewClass methodCall) { visit((MethodCall) methodCall); @@ -215,7 +212,6 @@ public abstract class AbstractASTWalker implements ASTVisitor{ @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); -// aReturn.retexpr.getType().accept(this); } @Override diff --git a/src/test/java/constraintSimplify/FamilyOfGenerics.java b/src/test/java/constraintSimplify/FamilyOfGenerics.java index e5c9ad13..01b204b6 100644 --- a/src/test/java/constraintSimplify/FamilyOfGenerics.java +++ b/src/test/java/constraintSimplify/FamilyOfGenerics.java @@ -24,7 +24,7 @@ public class FamilyOfGenerics { public void generateBC() throws Exception { SourceFile sf = generateAST(); PositionFinder.getPositionOfTPH(sf, null); - TPHExtractor tphExtractor = new TPHExtractor(); + TPHExtractor tphExtractor = new TPHExtractor(sf); List results = new ArrayList(); GeneratedGenericsFinder generatedGenericsFinder = new GeneratedGenericsFinder(sf, results); }