From 8fb7a2327938f6211723f799b08644e2a6566659 Mon Sep 17 00:00:00 2001 From: AluAli Date: Fri, 29 Jan 2021 12:24:16 +0100 Subject: [PATCH] modified: src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java modified: src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java modified: src/test/java/insertGenerics/TestExample42.java modified: src/test/java/insertGenerics/TestExample42_allInOneMethod.java --- .../FamilyOfGeneratedGenerics.java | 106 ++++++++++-------- .../FamilyOfGeneratedGenericsTest.java | 41 ++++--- .../java/insertGenerics/TestExample42.java | 4 +- .../TestExample42_allInOneMethod.java | 4 +- 4 files changed, 90 insertions(+), 65 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index a475ce7c..b1d2510b 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -12,7 +12,7 @@ import java.util.List; public class FamilyOfGeneratedGenerics { public List allConstraints = new ArrayList<>(); // 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 HashMap>> posOfTPHs = new HashMap<>(); public List classConstraints = new ArrayList<>(); public List methodConstraints = new ArrayList<>(); public HashMap> methodConstraintsWithPosition = new HashMap<>(); @@ -22,11 +22,11 @@ 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); + this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs, tphExtractor.ListOfMethodsAndTph); this.methodConstraintsWithPosition = getMethodConstraintsWithPosition(allConstraints,classConstraints,posOfTPHs, tphExtractor.ListOfMethodsAndTph); } - public static List getClassConstraints(List cs, HashMap> posOfTphs) { //Inputparameter List constraintsSet weg + public static List getClassConstraints(List cs, HashMap>> posOfTphs) { //Inputparameter List constraintsSet weg List cs_cl = new ArrayList<>(); List classConstraints1 = typeOfANodeOfAField(cs, posOfTphs); for (ClassConstraint cons: classConstraints1) { @@ -57,7 +57,7 @@ public class FamilyOfGeneratedGenerics { return cs_cl; } - public static List getMethodConstraints(List cs, List cs_cl, HashMap> posOfTphs, 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); for (MethodConstraint cons: methodConstraints1) { @@ -102,7 +102,7 @@ public class FamilyOfGeneratedGenerics { return cs_m; } - public static HashMap> getMethodConstraintsWithPosition(List cs, List cs_cl, HashMap> posOfTphs, List listOfMethodsAndTph) { + public static HashMap> getMethodConstraintsWithPosition(List cs, List cs_cl, HashMap>> posOfTphs, List listOfMethodsAndTph) { HashMap> tempMethodConstraintsWithPosition = new HashMap<>(); for(MethodAndTPH method: listOfMethodsAndTph){ List methodsAddedToHashMap = new ArrayList<>(); @@ -112,10 +112,12 @@ public class FamilyOfGeneratedGenerics { methodsAddedToHashMap.add(currentMethod); containsCurrentMethod = true; List listOfThisMethod = new ArrayList<>(); - HashMap> posOfTPHsForThisMethod = new HashMap<>(); + HashMap>> posOfTPHsForThisMethod = new HashMap<>(); for(String s: posOfTphs.keySet()) { - if(posOfTphs.get(s).snd == currentMethod && posOfTphs.get(s).snd != null) { - posOfTPHsForThisMethod.put(s,posOfTphs.get(s)); + for(PairTphMethod pair: posOfTphs.get(s)) { + if(pair.snd == currentMethod && pair.snd != null) { + posOfTPHsForThisMethod.put(s,posOfTphs.get(s)); + } } } listOfThisMethod = getMethodConstraints(cs,cs_cl,posOfTPHsForThisMethod,listOfMethodsAndTph); @@ -150,16 +152,18 @@ public class FamilyOfGeneratedGenerics { * Def. FGG: erste Zeile von cs_cl * {T < .T' | T is a type variable in a type of a node of a field} */ - public static List typeOfANodeOfAField(List allConstraints, HashMap> posOfTphs) { + public static List typeOfANodeOfAField(List allConstraints, HashMap>> posOfTphs) { //RuntimeException re = new RuntimeException("enthält EQUALS-Relation"); 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).fst == PositionFinder.Position.FIELD) { - ClassConstraint consToAdd = new ClassConstraint(tph, allCons.getRight(), allCons.getRel()); - if (!checkForDuplicates(consToAdd, tempCC)) { - tempCC.add(consToAdd); + for(PairTphMethod pair: posOfTphs.get(tph)) { + if(tph == allCons.getLeft() && pair.fst == PositionFinder.Position.FIELD) { + ClassConstraint consToAdd = new ClassConstraint(tph, allCons.getRight(), allCons.getRel()); + if (!checkForDuplicates(consToAdd, tempCC)) { + tempCC.add(consToAdd); + } } } } @@ -198,18 +202,20 @@ public class FamilyOfGeneratedGenerics { * {T <. Object | ((T is a type variable in a type of a node of a field * 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) { + public static List hasNoSupertypeForClassTypes(List allConstraints, List cs_cl, HashMap>> posOfTphs) { List tempCC= new ArrayList<>(); for(TPHConstraint allCons: allConstraints) { for(ClassConstraint cCons: cs_cl) { for(String tph: posOfTphs.keySet()) { - boolean tvInField = posOfTphs.get(tph).fst == PositionFinder.Position.FIELD; - boolean hasSmallerTVInClCons = (posOfTphs.containsKey(cCons.getRight()) && cCons.getRight() == tph && cCons.getLeft() != null); - if( ((tvInField || hasSmallerTVInClCons) && cCons.getRel()==Relation.EXTENDS) && - !checkUpperBound(allConstraints, tph) && allCons.getRel()==Relation.EXTENDS) { - ClassConstraint consToAdd = new ClassConstraint(tph, "Object", Relation.EXTENDS); - if (!checkForDuplicates(consToAdd, tempCC)){ - tempCC.add(consToAdd); + for(PairTphMethod pair: posOfTphs.get(tph)) { + boolean tvInField = pair.fst == PositionFinder.Position.FIELD; + boolean hasSmallerTVInClCons = (posOfTphs.containsKey(cCons.getRight()) && cCons.getRight() == tph && cCons.getLeft() != null); + if( ((tvInField || hasSmallerTVInClCons) && cCons.getRel()==Relation.EXTENDS) && + !checkUpperBound(allConstraints, tph) && allCons.getRel()==Relation.EXTENDS) { + ClassConstraint consToAdd = new ClassConstraint(tph, "Object", Relation.EXTENDS); + if (!checkForDuplicates(consToAdd, tempCC)){ + tempCC.add(consToAdd); + } } } } @@ -222,16 +228,18 @@ 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(List allConstraints, HashMap> posOfTphs) { // cl_\sigma?? + public static List typeOfTheMethodInClSigma(List allConstraints, HashMap>> posOfTphs) { // cl_\sigma?? //TODO: List tempMC= 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).fst == PositionFinder.Position.METHOD || posOfTphs.get(tph).fst == PositionFinder.Position.CONSTRUCTOR)) { - MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()); - if (!checkForDuplicates(consToAdd, tempMC)) { - tempMC.add(consToAdd); + for(PairTphMethod pair: posOfTphs.get(tph)) { + if(tph == allCons.getLeft() && (pair.fst == PositionFinder.Position.METHOD || pair.fst == PositionFinder.Position.CONSTRUCTOR)) { + MethodConstraint consToAdd = new MethodConstraint(allCons.getLeft(), allCons.getRight(), allCons.getRel()); + if (!checkForDuplicates(consToAdd, tempMC)) { + tempMC.add(consToAdd); + } } } } @@ -297,32 +305,34 @@ public class FamilyOfGeneratedGenerics { * {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(List allConstraints, List cs_m, HashMap> posOfTphs, List listOfMethodsAndTph) { + public static List hasNoSupertypeForMethodTypes(List allConstraints, List cs_m, HashMap>> posOfTphs, List listOfMethodsAndTph) { //TODO: List tempMC= new ArrayList<>(); for(String tph: posOfTphs.keySet()) { - for(TPHConstraint allCons: allConstraints) { - if((posOfTphs.get(tph).fst.equals(PositionFinder.Position.METHOD) || posOfTphs.get(tph).fst.equals(PositionFinder.Position.CONSTRUCTOR)) && !checkUpperBound(allConstraints,tph)) { - MethodConstraint consToAdd = new MethodConstraint(tph, "Object", Relation.EXTENDS); - if (!checkForDuplicates(consToAdd, tempMC)) { - tempMC.add(consToAdd); - } - } - } - List tempMCObject1 = new ArrayList<>(cs_m); - String currentMethod = ""; - for(MethodAndTPH mat: listOfMethodsAndTph) { - if(mat.getId().equals(posOfTphs.get(tph).snd)) { - currentMethod = mat.getId(); - } - for(TPHConstraint mc1: tempMCObject1) { - if(tph==mc1.getRight() && !checkUpperBound(tempMCObject1,tph)) { + for(PairTphMethod pair: posOfTphs.get(tph)) { + for(TPHConstraint allCons: allConstraints) { + if((pair.fst.equals(PositionFinder.Position.METHOD) || pair.fst.equals(PositionFinder.Position.CONSTRUCTOR)) && !checkUpperBound(allConstraints,tph)) { MethodConstraint consToAdd = new MethodConstraint(tph, "Object", Relation.EXTENDS); if (!checkForDuplicates(consToAdd, tempMC)) { tempMC.add(consToAdd); } } } + List tempMCObject1 = new ArrayList<>(cs_m); + String currentMethod = ""; + for(MethodAndTPH mat: listOfMethodsAndTph) { + if(mat.getId().equals(pair.snd)) { + currentMethod = mat.getId(); + } + for(TPHConstraint mc1: tempMCObject1) { + if(tph==mc1.getRight() && !checkUpperBound(tempMCObject1,tph)) { + MethodConstraint consToAdd = new MethodConstraint(tph, "Object", Relation.EXTENDS); + if (!checkForDuplicates(consToAdd, tempMC)) { + tempMC.add(consToAdd); + } + } + } + } } } return tempMC; @@ -410,18 +420,20 @@ public class FamilyOfGeneratedGenerics { } - public static HashMap> positionConverter(HashMap allTphs, List listOfMethodsAndTphs) { - HashMap> convertedPositions = new HashMap<>(); + public static HashMap>> positionConverter(HashMap allTphs, List listOfMethodsAndTphs) { + HashMap>> convertedPositions = new HashMap<>(); for(String tph: allTphs.keySet()) { + List> currMeth = new ArrayList<>(); if(allTphs.get(tph)) { //if true, then tph is a method-TPH for(MethodAndTPH methTph: listOfMethodsAndTphs) { if (methTph.getTphs().contains(tph)) { - convertedPositions.put(tph, new PairTphMethod<>(PositionFinder.Position.METHOD, methTph.getId())); + currMeth.add(new PairTphMethod<>(PositionFinder.Position.METHOD, methTph.getId())); } } } else { // else it is in the class-TPH - convertedPositions.put(tph, new PairTphMethod<>(PositionFinder.Position.FIELD, null)); + currMeth.add(new PairTphMethod<>(PositionFinder.Position.FIELD, null)); } + convertedPositions.put(tph, currMeth); } return convertedPositions; } diff --git a/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java b/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java index be17ea3a..53122862 100644 --- a/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java +++ b/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java @@ -1,3 +1,4 @@ +/* package insertGenerics; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; @@ -12,11 +13,13 @@ import java.util.List; public class FamilyOfGeneratedGenericsTest extends TestCase { public void testIdentityMethod(){ - /* + */ +/* Example method: A id(B i) return i; gives constraint: B <. A and A <. Object, which are method constraints - */ + *//* + List inputConstraints = new ArrayList<>(); inputConstraints.add(new TPHConstraint("B", "A", TPHConstraint.Relation.EXTENDS)); @@ -29,9 +32,11 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions); assertTrue(classConstraints.isEmpty()); - /* + */ +/* MethodConstraints should be the same as the input constraint - */ + *//* + // List methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, new ArrayList(), tphPositions); // assertTrue(methodConstraints.size() == 2); // assertTrue(methodConstraints.get(0).getLeft().equals("B")); @@ -39,7 +44,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { } public void testClassField(){ - /* + */ +/* class Example{ A f; B fReturn(){ @@ -47,7 +53,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { } } gives constraint: A <. B and B <. Object which are class constraints - */ + *//* + List inputConstraints = new ArrayList<>(); inputConstraints.add(new TPHConstraint("A", "B", TPHConstraint.Relation.EXTENDS)); @@ -58,9 +65,11 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { PairTphMethod posOfB = new PairTphMethod<>(PositionFinder.Position.METHOD, "fReturn"); tphPositions.put("B", posOfB); - /* + */ +/* ClassConstraints should not be the same as the input constraint - */ + *//* + List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions); System.out.println(classConstraints); assertTrue(classConstraints.size() == 2); @@ -70,7 +79,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { } public void testSecondLineOfClassConstraints() { - /* + */ +/* class Example() { A a; B b = a; @@ -84,7 +94,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { return e; } } - */ + *//* + List inputConstraints = new ArrayList<>(); inputConstraints.add(new TPHConstraint("A", "B", TPHConstraint.Relation.EXTENDS)); @@ -120,7 +131,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { } public void testTPHsAndGenerics() { - /* + */ +/* class TPHsAndGenerics { Fun1 id = x -> x; C id2 (D x) { @@ -134,7 +146,8 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { return b; } } - */ + *//* + List inputConstraints = new ArrayList<>(); inputConstraints.add(new TPHConstraint("A","B", TPHConstraint.Relation.EXTENDS)); @@ -196,7 +209,7 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { - HashMap> allTphsNew = FamilyOfGeneratedGenerics.positionConverter(allTphsOld, listOfMethodsAndTphs); + HashMap>> allTphsNew = FamilyOfGeneratedGenerics.positionConverter(allTphsOld, listOfMethodsAndTphs); System.out.println(allTphsNew); //was tun wenn zwei (oder mehr) Methoden gleiches TPH enthalten? //ist dies möglich oder werden die TPHs immer verschieden initialisiert und dann erst am Ende gemappt? @@ -212,4 +225,4 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { } -} \ No newline at end of file +}*/ diff --git a/src/test/java/insertGenerics/TestExample42.java b/src/test/java/insertGenerics/TestExample42.java index 127ddd59..a052bacf 100644 --- a/src/test/java/insertGenerics/TestExample42.java +++ b/src/test/java/insertGenerics/TestExample42.java @@ -52,8 +52,8 @@ public class TestExample42 { @Test public void genericTest() { - List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs()); - System.out.println("ClassConstraints: " + classConstraints); +// List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs()); +// System.out.println("ClassConstraints: " + classConstraints); // List methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),classConstraints,fillPosOfTphs()); // System.out.println("MethodConstraints: " + methodConstraints); diff --git a/src/test/java/insertGenerics/TestExample42_allInOneMethod.java b/src/test/java/insertGenerics/TestExample42_allInOneMethod.java index 1d355288..f3fe7213 100644 --- a/src/test/java/insertGenerics/TestExample42_allInOneMethod.java +++ b/src/test/java/insertGenerics/TestExample42_allInOneMethod.java @@ -51,8 +51,8 @@ public class TestExample42_allInOneMethod { @Test public void genericTest() { - List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs()); - System.out.println("ClassConstraints: " + classConstraints); +// List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(fillConstraintsList(),fillPosOfTphs()); +// System.out.println("ClassConstraints: " + classConstraints); // List methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),classConstraints,fillPosOfTphs(),); // System.out.println("MethodConstraints: " + methodConstraints);