diff --git a/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java index 6cded807..0533a028 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java @@ -1,5 +1,10 @@ package de.dhbwstuttgart.bytecode.constraint; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + public class TPHConstraint { protected String left; protected String right; @@ -9,6 +14,12 @@ public class TPHConstraint { EXTENDS, EQUAL } + public TPHConstraint(Pair p) { + this.left = ((TypePlaceholder)p.TA1).getName(); + this.right = ((TypePlaceholder)p.TA2).getName(); + this.rel = p.GetOperator().equals(PairOperator.SMALLERDOT) ? Relation.EXTENDS : Relation.EQUAL; + } + public TPHConstraint(String left, String right, Relation rel) { this.left = left; this.right = right; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index 5f691554..c361f154 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -21,6 +21,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<>(); + private HashMap mapMethodsAndTph = new HashMap<>(); public List methodConstraints = new ArrayList<>(); public HashMap> methodConstraintsWithPosition = new HashMap<>(); public ResultSet resSet; @@ -34,6 +35,7 @@ public class FamilyOfGeneratedGenerics { this.allConstraints = tphExtractor.allCons; this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph); this.classConstraints = getClassConstraints(allConstraints,posOfTPHs); + tphExtractor.ListOfMethodsAndTph.forEach(matph -> this.mapMethodsAndTph.put(matph.getId(), matph)); this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs, tphExtractor.ListOfMethodsAndTph); this.methodConstraintsWithPosition = getMethodConstraintsWithPositionNew(allConstraints,classConstraints,posOfTPHs, tphExtractor.ListOfMethodsAndTph, resSet, oldCons); } @@ -69,7 +71,7 @@ public class FamilyOfGeneratedGenerics { return cs_cl; } - public static List getMethodConstraints(List cs, List cs_cl, HashMap>> posOfTphs, List listOfMethodsAndTph) { + public List getMethodConstraints(List cs, List cs_cl, HashMap>> posOfTphs, List listOfMethodsAndTph) { List cs_m = new ArrayList<>(); List methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs); for (MethodConstraint cons: methodConstraints1) { @@ -114,7 +116,7 @@ public class FamilyOfGeneratedGenerics { return cs_m; } - public static HashMap> getMethodConstraintsWithPosition(List cs, List cs_cl, HashMap>> posOfTphs, List listOfMethodsAndTph, ResultSet resSet, Set oldCons) { + public HashMap> getMethodConstraintsWithPosition(List cs, List cs_cl, HashMap>> posOfTphs, List listOfMethodsAndTph, ResultSet resSet, Set oldCons) { HashMap> tempMethodConstraintsWithPosition = new HashMap<>(); for(MethodAndTPH method: listOfMethodsAndTph){ List methodsAddedToHashMap = new ArrayList<>(); @@ -281,7 +283,7 @@ public class FamilyOfGeneratedGenerics { * Def. FGG: zweite Zeile von cs_m * {R' <. S | (R <. R'), (S <. S') \in cs_m and (R',S) is in the transitive closure of cs} */ - public static List firstTransitiveSubtypeForMethodTypes(List allConstraints, List cs_m) { //transitive closure of cs + public List firstTransitiveSubtypeForMethodTypes(List allConstraints, List cs_m) { //transitive closure of cs //TODO: List tempMC= new ArrayList<>(); List tcOfCs = buildTransitiveClosure(allConstraints); @@ -306,7 +308,7 @@ public class FamilyOfGeneratedGenerics { * Def. FGG: dritte Zeile von cs_m * {R' <. S | (R <. R') \in cs_m, (S <. S') \in cs_cl and (R',S) is in the transitive closure of cs} */ - public static List secondTransitiveSubtypeForMethodTypes(List allConstraints, List cs_cl, List cs_m) { + public List secondTransitiveSubtypeForMethodTypes(List allConstraints, List cs_cl, List cs_m) { //TODO: List tempMC= new ArrayList<>(); List tcOfCs = buildTransitiveClosure(allConstraints); @@ -369,7 +371,7 @@ public class FamilyOfGeneratedGenerics { /** * nimm die Menge cs_cl aus cs_m raus */ - public static List methodTypesWithoutClassTypes(List cs_cl, List cs_m) { + public List methodTypesWithoutClassTypes(List cs_cl, List cs_m) { // erstelle Kopie der Liste cs_cl List tempCC = new ArrayList<>(); for(ClassConstraint cc: cs_cl) { @@ -415,7 +417,7 @@ public class FamilyOfGeneratedGenerics { return false; } - public static List buildTransitiveClosure(List list) { + public List buildTransitiveClosure(List list) { List iterList = new ArrayList<>(list); List runList = new ArrayList<>(list); List tcList = new ArrayList<>(list); @@ -511,7 +513,7 @@ public class FamilyOfGeneratedGenerics { return tempMC; } - public static List secondLineMethodDefinition(List allConstraints, HashMap>> posOfTphs, MethodAndTPH methodAndTPH, ResultSet resSet, Set oldCons) { + public List secondLineMethodDefinition(List allConstraints, HashMap>> posOfTphs, MethodAndTPH methodAndTPH, ResultSet resSet, Set oldCons) { List tempMC = new ArrayList<>(); //für Ergebnisse basierend auf der neuen Datenstruktur (Ali) Set tempSet = new HashSet<>(); //für Ergebnisse des ersten Teils der Bedinung basierend auf der alten Datenstruktur Set tempSet2 = new HashSet<>(); //für Ergebnisse des zweiten Teils der Bedingung basierend auf der alten Datenstruktur @@ -572,6 +574,7 @@ public class FamilyOfGeneratedGenerics { // Constraint extendsSetAllOfOr = new Constraint(); // Constraint equalSetAllOfOr = new Constraint(); + // Berechnung des ersten Teils der Bedingung der Regel for(int i=0; i extendsSet = new Constraint(); @@ -660,9 +663,39 @@ public class FamilyOfGeneratedGenerics { Pair pairExtends2 = itExtends2.next(); Iterator itEqual2 = equalSet.iterator(); while (itEqual2.hasNext()) { + boolean transClo = false; Pair pairEqual2 = itEqual2.next(); //TODO: Auf trans.FamilyOfGeneratedGenerics Huelle pruefen + Pair newPairOld = new Pair(pairExtends2.TA2, pairEqual2.TA1); Pair newPair2 = new Pair(resSet.resolveType((TypePlaceholder) (pairExtends2.TA2)).resolvedType, resSet.resolveType((TypePlaceholder) (pairEqual2.TA1)).resolvedType, PairOperator.SMALLERDOT); + TPHConstraint newPairTPHConstraint = new TPHConstraint(newPair2); + //if (!tcOfoldConsSet.contains(newPairOld)) { continue; } + Iterator itTC = tcOfCs.iterator(); //tcOfoldConsSet.iterator(); + /* + buildTransitiveClosureForCP( + mapMethodsAndTph.get(posOfTPHs.get(((TypePlaceholder)pairExtends2.TA2) + .getName()).get(0).snd) + .constraints.getAll(), + resSet).iterator(); + */ + //while(itTC.hasNext()) {//ueberpruefen ob newPair in transitiv closure + //Pair pairTC = itTC.next(); + //if //((pairTC.TA1.equals(newPairOld.TA1)) + // ((resSet.resolveType((TypePlaceholder)(pairTC.TA1)).resolvedType.equals(newPair2.TA1)) + // && resSet.resolveType((TypePlaceholder)(pairTC.TA2)).resolvedType.equals(newPair2.TA2) + // //&& pairTC.OperatorSmallerDot() + // //&& newPair2.OperatorSmallerDot() + // ) + //TPHConstraint tPHCons = itTC.next(); + //if newPairTPHConstraint + //{ //erst hier bei pairTC resolvedType, damit Pairs aus oldCons in der Methode zur Berechnung von der transitiven Hülle nicht in eine Endlosschleife kommen (Cycles!) + // transClo = true; + // break; + //} + //} + if (tcOfCs.contains(newPairTPHConstraint)) { + transClo = true; + } TypePlaceholder tphR = (TypePlaceholder) pairEqual2.TA2; Iterator itUndCons = undCons.iterator(); while (itUndCons.hasNext()) { @@ -686,9 +719,9 @@ public class FamilyOfGeneratedGenerics { } - if (rEqExRtilde || isPairInTExTapostrophe) { - if (newPair2.TA1 != newPair2.TA2) { //eliminieren der Fälle wie AA<.AA - if (!checkForDuplicatesForSets(newPair2, tempSet2)) + if ((rEqExRtilde || isPairInTExTapostrophe)) { + if (!newPair2.TA1.equals(newPair2.TA2)) { //eliminieren der Fälle wie AA<.AA + if (transClo && !checkForDuplicatesForSets(newPair2, tempSet2)) tempSet2.add(newPair2); } if (!checkForDuplicatesForSets(pairExtends2, tempSet2)) @@ -837,7 +870,7 @@ public class FamilyOfGeneratedGenerics { - public static HashMap> getMethodConstraintsWithPositionNew(List cs, List cs_cl, HashMap>> posOfTphs, List listOfMethodsAndTph, ResultSet resSet, Set oldCons) { + public HashMap> getMethodConstraintsWithPositionNew(List cs, List cs_cl, HashMap>> posOfTphs, List listOfMethodsAndTph, ResultSet resSet, Set oldCons) { HashMap> tempMethodConstraintsWithPosition = new HashMap<>(); List newMCList = new ArrayList<>(); List newMCList2 = new ArrayList<>(); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java b/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java index 892b0894..c5a93f4f 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/utilities/MethodAndTPH.java @@ -47,4 +47,9 @@ public class MethodAndTPH { return localTphs; } + @Override + public String toString() { + return id; + } + } diff --git a/src/test/java/insertGenerics/TestContraVariant.java b/src/test/java/insertGenerics/TestContraVariant.java index 0be39f5c..f220f3d5 100644 --- a/src/test/java/insertGenerics/TestContraVariant.java +++ b/src/test/java/insertGenerics/TestContraVariant.java @@ -57,7 +57,8 @@ public class TestContraVariant { lmc.add(new MethodConstraint("O", "java/lang/Object", Relation.EXTENDS)); methodConstraintsWithPositionTest.put("TPH Rm(TPH O)", lmc); lmc = new HashSet<>(); - lmc.add(new MethodConstraint("S", "java/lang/Object", Relation.EXTENDS)); + lmc.add(new MethodConstraint("S", "O", Relation.EXTENDS)); + lmc.add(new MethodConstraint("O", "java/lang/Object", Relation.EXTENDS)); lmc.add(new MethodConstraint("R", "java/lang/Object", Relation.EXTENDS)); methodConstraintsWithPositionTest.put("TPH Rmain(TPH S)", lmc); diff --git a/src/test/java/insertGenerics/TestLocalVarLambda.java b/src/test/java/insertGenerics/TestLocalVarLambda.java index 530ef318..93ff3415 100644 --- a/src/test/java/insertGenerics/TestLocalVarLambda.java +++ b/src/test/java/insertGenerics/TestLocalVarLambda.java @@ -66,13 +66,17 @@ public class TestLocalVarLambda { lmc.add(new MethodConstraint("O", "ALU", Relation.EXTENDS)); lmc.add(new MethodConstraint("DIU", "java/lang/Object", Relation.EXTENDS)); lmc.add(new MethodConstraint("ALU", "DIU", Relation.EXTENDS)); + lmc.add(new MethodConstraint("SY", "java/lang/Object", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH DIUm(TPH O)", lmc); } else { lmc.add(new MethodConstraint("O", "DIU", Relation.EXTENDS)); lmc.add(new MethodConstraint("N", "java/lang/Object", Relation.EXTENDS)); lmc.add(new MethodConstraint("DIU", "N", Relation.EXTENDS)); + lmc.add(new MethodConstraint("SY", "java/lang/Object", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH Nm(TPH O)", lmc); } - methodConstraintsWithPositionTest.put("TPH DIUm(TPH O)", lmc); + Set computedClassCons = new HashSet<>(fogg.classConstraints); Set expectedClassCons = new HashSet<>(classConstraintsTest); diff --git a/src/test/java/insertGenerics/TestTransitiveClosure.java b/src/test/java/insertGenerics/TestTransitiveClosure.java index 95c6a0e2..4eaf0527 100644 --- a/src/test/java/insertGenerics/TestTransitiveClosure.java +++ b/src/test/java/insertGenerics/TestTransitiveClosure.java @@ -21,8 +21,8 @@ public class TestTransitiveClosure { @Test public void genericTest() { - List testCons = FamilyOfGeneratedGenerics.buildTransitiveClosure(fillList()); - System.out.println(testCons); + //List testCons = FamilyOfGeneratedGenerics.buildTransitiveClosure(fillList()); + //System.out.println(testCons); } }