diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index d8df6cc1..fbd66778 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -16,13 +16,15 @@ public class FamilyOfGeneratedGenerics { public HashMap> posOfTPHs = new HashMap<>(); public List classConstraints = new ArrayList<>(); public List methodConstraints = new ArrayList<>(); + public HashMap methodsWithPosition = new HashMap<>(); public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) { this.allConstraints = tphExtractor.allCons; this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph); this.classConstraints = getClassConstraints(allConstraints,posOfTPHs); -// this.methodConstraints = +// this.methodConstraints = getMethodConstraints(allConstraints,classConstraints,posOfTPHs); + } public static List getClassConstraints(List cs, HashMap> posOfTphs) { //Inputparameter List constraintsSet weg @@ -48,38 +50,72 @@ public class FamilyOfGeneratedGenerics { return cs_cl; } - public static List getMethodConstraints(List cs, List cs_cl, HashMap> posOfTphs) { +// public static List getMethodConstraints(List cs, List cs_cl, HashMap> posOfTphs) { +// //TODO: Regeln +// List cs_m = new ArrayList<>(); +// List methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs); +// for (MethodConstraint cons: methodConstraints1) { +// if (!checkForDuplicates(cons, cs_m)) { +// cs_m.add(cons); +// } +// } +// List methodConstraints2 = firstTransitiveSubtypeForMethodTypes(cs, cs_m); +// for (MethodConstraint cons: methodConstraints2) { +// if (!checkForDuplicates(cons, cs_m)) { +// cs_m.add(cons); +// } +// } +// List methodConstraints3 = secondTransitiveSubtypeForMethodTypes(cs, cs_cl, cs_m); +// for (MethodConstraint cons: methodConstraints3) { +// if (!checkForDuplicates(cons, cs_m)) { +// cs_m.add(cons); +// } +// } +// List methodConstraints4 = hasNoSupertypeForMethodTypes(cs, posOfTphs); +// for (MethodConstraint cons: methodConstraints4) { +// if (!checkForDuplicates(cons, cs_m)) { +// cs_m.add(cons); +// } +// } +// List methodConstraints5 = methodTypesWithoutClassTypes(cs_cl, cs_m); +// cs_m = methodConstraints5; +// return cs_m; +// } + + public static HashMap getMethodConstraints(List cs, List cs_cl, HashMap> posOfTphs) { //TODO: Regeln - List cs_m = new ArrayList<>(); - List methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs); - for (MethodConstraint cons: methodConstraints1) { - if (!checkForDuplicates(cons, cs_m)) { + HashMap cs_m = new HashMap<>(); + HashMap methodConstraints1 = typeOfTheMethodInClSigma(cs, posOfTphs); + for (MethodConstraint cons: methodConstraints1.keySet()) { + if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) { + cs_m.put(cons,); + } + } + HashMap methodConstraints2 = firstTransitiveSubtypeForMethodTypes(cs, cs_m); + for (MethodConstraint cons: methodConstraints2.keySet()) { + if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) { cs_m.add(cons); } } - List methodConstraints2 = firstTransitiveSubtypeForMethodTypes(cs, cs_m); - for (MethodConstraint cons: methodConstraints2) { - if (!checkForDuplicates(cons, cs_m)) { + HashMap methodConstraints3 = secondTransitiveSubtypeForMethodTypes(cs, cs_cl, cs_m); + for (MethodConstraint cons: methodConstraints3.keySet()) { + if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) { cs_m.add(cons); } } - List methodConstraints3 = secondTransitiveSubtypeForMethodTypes(cs, cs_cl, cs_m); - for (MethodConstraint cons: methodConstraints3) { - if (!checkForDuplicates(cons, cs_m)) { + HashMap methodConstraints4 = hasNoSupertypeForMethodTypes(cs, posOfTphs); + for (MethodConstraint cons: methodConstraints4.keySet()) { + if (!checkForDuplicatesInMethods(cons, cs_m, posOfTphs)) { cs_m.add(cons); } } - List methodConstraints4 = hasNoSupertypeForMethodTypes(cs, posOfTphs); - for (MethodConstraint cons: methodConstraints4) { - if (!checkForDuplicates(cons, cs_m)) { - cs_m.add(cons); - } - } - List methodConstraints5 = methodTypesWithoutClassTypes(cs_cl, cs_m); + HashMap methodConstraints5 = methodTypesWithoutClassTypes(cs_cl, cs_m); cs_m = methodConstraints5; return cs_m; } + + /** * Def. FGG: erste Zeile von cs_cl * {T < .T' | T is a type variable in a type of a node of a field} @@ -155,16 +191,16 @@ 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 HashMap typeOfTheMethodInClSigma(List allConstraints, HashMap> posOfTphs) { // cl_\sigma?? //TODO: - List tempMC= new ArrayList<>(); + HashMap tempMC= new HashMap<>(); 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); + if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) { + tempMC.put(consToAdd, posOfTphs.get(tph).snd); } } } @@ -178,19 +214,19 @@ 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 static HashMap firstTransitiveSubtypeForMethodTypes(List allConstraints, HashMap cs_m, HashMap> posOfTphs) { //transitive closure of cs //TODO: - List tempMC= new ArrayList<>(); + HashMap tempMC= new HashMap<>(); List tcOfCs = buildTransitiveClosure(allConstraints); - for(MethodConstraint mC1 : cs_m) { //(R <. R') - for(MethodConstraint mC2 : cs_m) { //(S <. S') + for(MethodConstraint mC1 : cs_m.keySet()) { //(R <. R') + for(MethodConstraint mC2 : cs_m.keySet()) { //(S <. S') String lSide = mC1.getRight(); //R' String rSide = mC2.getLeft(); //S for(TPHConstraint tphC : tcOfCs) { if(tphC.getLeft().equals(lSide)&&tphC.getRight().equals(rSide)) { //is it (R',S) MethodConstraint consToAdd = new MethodConstraint(lSide, rSide, tphC.getRel()); //create (R'<.S) - if (!checkForDuplicates(consToAdd, tempMC)) { - tempMC.add(consToAdd); + if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) { + tempMC.put(consToAdd, posOfTphs.get()); } } } @@ -203,20 +239,20 @@ 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 static HashMap secondTransitiveSubtypeForMethodTypes(List allConstraints, List cs_cl, HashMap cs_m, HashMap> posOfTphs) { //TODO: - List tempMC= new ArrayList<>(); + HashMap tempMC= new HashMap<>(); List tcOfCs = buildTransitiveClosure(allConstraints); for(ClassConstraint cC : cs_cl) { - for(MethodConstraint mC : cs_m) { + for(MethodConstraint mC : cs_m.keySet()) { String leftSide = mC.getRight(); String rightSide = cC.getLeft(); for(TPHConstraint tphC : tcOfCs) { if(tphC.getLeft().equals(leftSide)&&tphC.getRight().equals(rightSide)) { MethodConstraint consToAdd = new MethodConstraint(tphC.getLeft(), tphC.getRight(), tphC.getRel()); - if (!checkForDuplicates(consToAdd, tempMC)) { - tempMC.add(consToAdd); - System.out.println(consToAdd); + if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) { + tempMC.put(consToAdd, posOfTphs.get()); +// System.out.println(consToAdd); } } @@ -231,16 +267,16 @@ 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, HashMap> posOfTphs) { + public static HashMap hasNoSupertypeForMethodTypes(List allConstraints, HashMap> posOfTphs) { //TODO: - List tempMC= new ArrayList<>(); + HashMap tempMC= new HashMap<>(); 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); - System.out.println(consToAdd); + if (!checkForDuplicatesInMethods(consToAdd, tempMC, posOfTphs)) { + tempMC.put(consToAdd, posOfTphs.get(tph).snd); +// System.out.println(consToAdd); } } } @@ -251,7 +287,7 @@ public class FamilyOfGeneratedGenerics { /** * nimm die Menge cs_cl aus cs_m raus */ - public static List methodTypesWithoutClassTypes(List cs_cl, List cs_m) { + public static HashMap methodTypesWithoutClassTypes(List cs_cl, HashMap cs_m) { //TODO: List tempCC = new ArrayList<>(); for(ClassConstraint cc: cs_cl) { @@ -259,7 +295,7 @@ public class FamilyOfGeneratedGenerics { tempCC.add(tphC); } List tempMC = new ArrayList<>(); - for(MethodConstraint mc: cs_m) { + for(MethodConstraint mc: cs_m.keySet()) { TPHConstraint tphC = new TPHConstraint(mc.getLeft(), mc.getRight(), mc.getRel()); tempMC.add(tphC); } @@ -294,6 +330,20 @@ public class FamilyOfGeneratedGenerics { return false; } + public static boolean checkForDuplicatesInMethods(TPHConstraint constraint, HashMap hashMap, HashMap> posOfTphs) { + HashMap tempMap = hashMap; + boolean hasSame = false; + for (TPHConstraint tphC: tempMap.keySet()) { + hasSame = constraint.getLeft() == tphC.getLeft() && + constraint.getRight() == tphC.getRight() && + constraint.getRel() == tphC.getRel() && + posOfTphs.get(); //constraint already in ArrayList if true + if (hasSame) + return true; + } + return false; + } + public static List buildTransitiveClosure(List list) { List iterList = new ArrayList<>(list); List runList = new ArrayList<>(list); diff --git a/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java b/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java index 77a7d997..9d7689a0 100644 --- a/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java +++ b/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java @@ -114,7 +114,7 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { assertFalse(classConstraints.isEmpty()); assertTrue(classConstraints.size() == 6); assertFalse(methodConstraints.isEmpty()); - assertTrue(methodConstraints.size() == 5); + assertTrue(methodConstraints.size() == 2); } diff --git a/src/test/java/insertGenerics/TestExample42.java b/src/test/java/insertGenerics/TestExample42.java index 8d291a2d..57dad7b7 100644 --- a/src/test/java/insertGenerics/TestExample42.java +++ b/src/test/java/insertGenerics/TestExample42.java @@ -56,6 +56,9 @@ public class TestExample42 { System.out.println("ClassConstraints: " + classConstraints); List methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(fillConstraintsList(),classConstraints,fillPosOfTphs()); System.out.println("MethodConstraints: " + methodConstraints); + for(MethodConstraint mC: methodConstraints) { + System.out.println(mC); + } List testCons; }