From 7cf46b02b7e68f61175f25f94c2eacd115b6bdf3 Mon Sep 17 00:00:00 2001 From: AluAli Date: Fri, 6 Nov 2020 11:24:21 +0100 Subject: [PATCH 1/3] modified: src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java --- .../FamilyOfGeneratedGenerics.java | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java index fa626c48..97d005fe 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java @@ -87,13 +87,16 @@ public class FamilyOfGeneratedGenerics { for(TPHConstraint allCons: allConstraints) { for(ClassConstraint cCons: cs_cl) { for(String tph: posOfTphs.keySet()) { - System.out.println("cCons: " + cCons); if( (posOfTphs.get(tph) == PositionFinder.Position.FIELD) || - (posOfTphs.containsKey(cCons.getLeft()) && cCons.getRight() != null) && - allCons.getRight() == null && - allCons.getRel()==Relation.EXTENDS && cCons.getRel()==Relation.EXTENDS) { + (posOfTphs.containsKey(cCons.getRight()) && cCons.getLeft() != null) && + (allCons.getLeft() == tph && allCons.getRight() == null) && + allCons.getRel()==Relation.EXTENDS && cCons.getRel()==Relation.EXTENDS && + cCons.getRight() == tph && allCons.getLeft() == tph) { System.out.println("C3: " + allCons); - tempCC.add(new ClassConstraint(allCons.getLeft(), "Object", Relation.EXTENDS)); + ClassConstraint consToAdd = new ClassConstraint(cCons.getRight(), "Object", Relation.EXTENDS); + if (!checkForDuplicates(consToAdd, tempCC)){ + tempCC.add(consToAdd); + } } } } @@ -109,7 +112,7 @@ public class FamilyOfGeneratedGenerics { * {T < .T' | T is a type variable in a type of the method/constructor m in cl_\sigma, (T <. T') \in cs} *//* - public List typeOfTheMethodInClSigma() { // cl_\sigma?? + public static List typeOfTheMethodInClSigma() { // cl_\sigma?? //TODO: return cs_m; } @@ -120,7 +123,7 @@ public class FamilyOfGeneratedGenerics { * {R' <. S | (R <. R'), (S <. S') \in cs_m and (R',S) is in the transitive closure of cs} *//* - public List firstTransitiveSubtypeForMethodTypes() { //transitive closure of cs + public static List firstTransitiveSubtypeForMethodTypes() { //transitive closure of cs //TODO: return cs_m; } @@ -131,7 +134,7 @@ public class FamilyOfGeneratedGenerics { * {R' <. S | (R <. R') \in cs_m, (S <. S') \in cs_cl and (R',S) is in the transitive closure of cs} *//* - public List secondTransitiveSubtypeForMethodTypes() { + public static List secondTransitiveSubtypeForMethodTypes() { //TODO: return cs_m; } @@ -143,7 +146,7 @@ public class FamilyOfGeneratedGenerics { * (\existsnot T': T <. T') \in cs)} *//* - public List hasNoSupertypeForMethodTypes() { + public static List hasNoSupertypeForMethodTypes() { //TODO: return cs_m; } @@ -151,14 +154,24 @@ public class FamilyOfGeneratedGenerics { */ /** * nimm die Menge cs_cl aus cs_m raus - *//* - - public List methodTypesWithoutClassTypes() { + */ +/* + public static List methodTypesWithoutClassTypes() { //TODO: return cs_m; } */ + public static boolean checkForDuplicates(TPHConstraint constraint, List list) { + List tempList = list; + for (TPHConstraint tphC: tempList) { + return (constraint.getLeft() == tphC.getLeft() && + constraint.getRight() == tphC.getRight() && + constraint.getRel() == tphC.getRel()); //constraint already in ArrayList if true + } + return false; + } + From e617a0911ab1229a1195067c4f4cc5ca60f06b6e Mon Sep 17 00:00:00 2001 From: AluAli Date: Fri, 6 Nov 2020 11:51:04 +0100 Subject: [PATCH 2/3] modified: src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java modified: src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java --- .../gGenericsAli/ClassConstraint.java | 2 +- .../FamilyOfGeneratedGenerics.java | 34 +++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java index 7f86e935..fd5ffcae 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java @@ -3,7 +3,7 @@ package de.dhbwstuttgart.bytecode.gGenericsAli; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; public class ClassConstraint extends TPHConstraint { - private TPHConstraint constraint; + //private TPHConstraint constraint; public ClassConstraint(String left, String right, Relation rel) { super(left, right, rel); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java index 97d005fe..96f2cb74 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java @@ -15,17 +15,24 @@ public class FamilyOfGeneratedGenerics { public static List getClassConstraints(List cs, HashMap posOfTphs) { //Inputparameter List constraintsSet weg List cs_cl = new ArrayList<>(); - System.out.println("0: " + cs_cl); List classConstraints1 = typeOfANodeOfAField(cs, cs_cl, posOfTphs); - cs_cl.addAll(classConstraints1); - System.out.println("1: " + cs_cl); + for (ClassConstraint cons: classConstraints1) { + if (!checkForDuplicates(cons, cs_cl)) { + cs_cl.add(cons); + } + } List classConstraints2 = transitiveSubtypeForClassTypes(cs, cs_cl); // in Klammer classConstraints1 oder constraintsSet? beides eher - cs_cl.addAll(classConstraints2); - System.out.println("2: " + cs_cl); + for (ClassConstraint cons: classConstraints2) { + if (!checkForDuplicates(cons, cs_cl)) { + cs_cl.add(cons); + } + } List classConstraints3 = hasNoSupertypeForClassTypes(cs, cs_cl, posOfTphs); - cs_cl.addAll(classConstraints3); - System.out.println("3: " + cs_cl); - + for (ClassConstraint cons: classConstraints3) { + if (!checkForDuplicates(cons, cs_cl)) { + cs_cl.add(cons); + } + } return cs_cl; } @@ -49,7 +56,10 @@ public class FamilyOfGeneratedGenerics { if(posOfTphs.containsKey(allCons.getLeft()) && allCons.getRight()!=null && allCons.getRel()==Relation.EXTENDS) { for(String tph: posOfTphs.keySet()) { if(tph == allCons.getLeft() && posOfTphs.get(tph) == PositionFinder.Position.FIELD) { - tempCC.add(new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel())); + ClassConstraint consToAdd = new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()); + if (!checkForDuplicates(consToAdd, tempCC)) { + tempCC.add(consToAdd); + } } } } @@ -68,7 +78,10 @@ public class FamilyOfGeneratedGenerics { if(cCons.getLeft() != null && cCons.getRel()==Relation.EXTENDS) { for(TPHConstraint allCons: allConstraints) { if(cCons.getRight() == allCons.getLeft() && allCons.getRight() != null && allCons.getRel()==Relation.EXTENDS){ - tempCC.add(new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel())); + ClassConstraint consToAdd = new ClassConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()); + if (!checkForDuplicates(consToAdd, tempCC)) { + tempCC.add(consToAdd); + } } } } @@ -92,7 +105,6 @@ public class FamilyOfGeneratedGenerics { (allCons.getLeft() == tph && allCons.getRight() == null) && allCons.getRel()==Relation.EXTENDS && cCons.getRel()==Relation.EXTENDS && cCons.getRight() == tph && allCons.getLeft() == tph) { - System.out.println("C3: " + allCons); ClassConstraint consToAdd = new ClassConstraint(cCons.getRight(), "Object", Relation.EXTENDS); if (!checkForDuplicates(consToAdd, tempCC)){ tempCC.add(consToAdd); From 70b6c35731aea244528520b375de8d55f7e1bfb8 Mon Sep 17 00:00:00 2001 From: AluAli Date: Fri, 6 Nov 2020 15:22:36 +0100 Subject: [PATCH 3/3] modified: src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java modified: src/test/java/insertGenerics/TestExample42.java new file: src/test/java/insertGenerics/TestTransitiveClosure.java --- .../FamilyOfGeneratedGenerics.java | 83 +++++++++++++------ .../java/insertGenerics/TestExample42.java | 7 +- .../insertGenerics/TestTransitiveClosure.java | 32 +++++++ 3 files changed, 97 insertions(+), 25 deletions(-) create mode 100644 src/test/java/insertGenerics/TestTransitiveClosure.java diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java index 96f2cb74..c8931c28 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java @@ -36,14 +36,17 @@ public class FamilyOfGeneratedGenerics { return cs_cl; } - public static List getMethodConstraints(List constraintsSet) { + public static List getMethodConstraints(List cs, HashMap posOfTphs) { //TODO: Regeln List cs_m = new ArrayList<>(); -// for(TPHConstraint cons: constraintsSet){ -// -// cs_m = -// } - return null; + List methodConstraints1 = typeOfTheMethodInClSigma(cs, cs_m, posOfTphs); + for (MethodConstraint cons: methodConstraints1) { + if (!checkForDuplicates(cons, cs_m)) { + cs_m.add(cons); + } + } + List methodConstraints2 = firstTransitiveSubtypeForMethodTypes(); + return cs_m; } /** @@ -72,7 +75,6 @@ public class FamilyOfGeneratedGenerics { * {T' <. T'' | \exists T: (T <. T') \in cs_cl, (T' <. T'') \in cs } */ public static List transitiveSubtypeForClassTypes(List allConstraints, List cs_cl) { - //TODO: List tempCC= new ArrayList<>(); for(ClassConstraint cCons: cs_cl) { if(cCons.getLeft() != null && cCons.getRel()==Relation.EXTENDS) { @@ -95,7 +97,6 @@ public class FamilyOfGeneratedGenerics { * or (\exists T~: (T~ <. T) \in cs_cl)) and (\existsnot T': T <. T') \in cs)} */ public static List hasNoSupertypeForClassTypes(List allConstraints, List cs_cl, HashMap posOfTphs) { - //TODO: List tempCC= new ArrayList<>(); for(TPHConstraint allCons: allConstraints) { for(ClassConstraint cCons: cs_cl) { @@ -122,57 +123,65 @@ public class FamilyOfGeneratedGenerics { /** * Def. FGG: erste Zeile von cs_m * {T < .T' | T is a type variable in a type of the method/constructor m in cl_\sigma, (T <. T') \in cs} - *//* + */ - public static List typeOfTheMethodInClSigma() { // cl_\sigma?? + public static List typeOfTheMethodInClSigma(List allConstraints, List cs_m, HashMap posOfTphs) { // cl_\sigma?? //TODO: - return cs_m; + List tempCC= new ArrayList<>(); + for(TPHConstraint allCons: allConstraints){ + if(posOfTphs.containsKey(allCons.getLeft()) && allCons.getRight()!=null && allCons.getRel()==Relation.EXTENDS) { + for(String tph: posOfTphs.keySet()) { + if(tph == allCons.getLeft() && (posOfTphs.get(tph) == PositionFinder.Position.METHOD || posOfTphs.get(tph) == PositionFinder.Position.CONSTRUCTOR)) { + MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()); + if (!checkForDuplicates(consToAdd, tempCC)) { + tempCC.add(consToAdd); + } + } + } + } + } + return tempCC; } - */ + /** * 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() { //transitive closure of cs //TODO: - return cs_m; + return null; } - */ /** * 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() { //TODO: - return cs_m; + return null; } - */ /** * Def. FGG: vierte Zeile von cs_m * {T <. Object | (T is a type variable in a type of a node of the method/constructor m in cl_\sigma), * (\existsnot T': T <. T') \in cs)} - *//* + */ public static List hasNoSupertypeForMethodTypes() { //TODO: - return cs_m; + return null; } - */ /** * nimm die Menge cs_cl aus cs_m raus */ -/* public static List methodTypesWithoutClassTypes() { //TODO: - return cs_m; + return null; } -*/ public static boolean checkForDuplicates(TPHConstraint constraint, List list) { List tempList = list; @@ -184,6 +193,32 @@ public class FamilyOfGeneratedGenerics { return false; } + public static List buildTransitiveClosure(List list) { + List iterList = list; + List runList = list; + List tcList = new ArrayList<>(); + boolean addedConToList = false; + for (TPHConstraint cons: iterList) { + for (TPHConstraint cons2: runList) { + if(cons.getRight() == cons2.getLeft()) { + for (TPHConstraint tcCons: tcList) { + if (!checkForDuplicates(tcCons,tcList)) { + tcList.add(new TPHConstraint(cons.getLeft(), cons2.getRight(), Relation.EXTENDS)); //Duplikate? dürfte nicht sein -> checken + addedConToList = true; + if (addedConToList) { + list.add(new TPHConstraint(cons.getLeft(), cons2.getRight(), Relation.EXTENDS)); + buildTransitiveClosure(list); + } + } + //TODO: über aktualisierte Liste laufen wegen Updates -> Rekursion? + } + } + } + } + return tcList; + } + + diff --git a/src/test/java/insertGenerics/TestExample42.java b/src/test/java/insertGenerics/TestExample42.java index aded8519..219e3fff 100644 --- a/src/test/java/insertGenerics/TestExample42.java +++ b/src/test/java/insertGenerics/TestExample42.java @@ -4,6 +4,7 @@ import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; import de.dhbwstuttgart.bytecode.gGenericsAli.ClassConstraint; import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics; +import de.dhbwstuttgart.bytecode.gGenericsAli.MethodConstraint; import de.dhbwstuttgart.bytecode.gGenericsAli.PositionFinder; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -42,6 +43,10 @@ public class TestExample42 { @Test public void genericTest() { List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs()); - System.out.println(classConstraints); + System.out.println("ClassConstraints: " + classConstraints); + List methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),fillPosOfTphs()); + System.out.println("MethodConstraints: " + methodConstraints); + + List testCons; } } diff --git a/src/test/java/insertGenerics/TestTransitiveClosure.java b/src/test/java/insertGenerics/TestTransitiveClosure.java new file mode 100644 index 00000000..e1bc022b --- /dev/null +++ b/src/test/java/insertGenerics/TestTransitiveClosure.java @@ -0,0 +1,32 @@ +package insertGenerics; + +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; +import de.dhbwstuttgart.bytecode.gGenericsAli.ClassConstraint; +import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics; +import de.dhbwstuttgart.bytecode.gGenericsAli.MethodConstraint; +import de.dhbwstuttgart.bytecode.gGenericsAli.PositionFinder; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class TestTransitiveClosure { + public List fillList() { + List list = new ArrayList<>(); + list.add(new TPHConstraint("A", "B", Relation.EXTENDS)); + list.add(new TPHConstraint("B", "C", Relation.EXTENDS)); + list.add(new TPHConstraint("C", "D", Relation.EXTENDS)); + + return list; + } + + @Test + public void genericTest() { + List testCons = FamilyOfGeneratedGenerics.buildTransitiveClosure(fillList()); + System.out.println(testCons); + + } +}