From a04316f62905b58305e6d52b7cc6cd9478e28ed6 Mon Sep 17 00:00:00 2001 From: AluAli Date: Tue, 29 Dec 2020 00:22:36 +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 | 79 +++++++++++++------ .../FamilyOfGeneratedGenericsTest.java | 14 +++- .../java/insertGenerics/TestExample42.java | 9 ++- .../TestExample42_allInOneMethod.java | 9 ++- 4 files changed, 75 insertions(+), 36 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index cc68e6e0..94ff8b98 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -4,7 +4,6 @@ import com.ibm.icu.text.CurrencyMetaInfo; import de.dhbwstuttgart.bytecode.TPHExtractor; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; import java.util.ArrayList; @@ -18,6 +17,7 @@ public class FamilyOfGeneratedGenerics { public List classConstraints = new ArrayList<>(); public List methodConstraints = new ArrayList<>(); + public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) { this.allConstraints = tphExtractor.allCons; this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph); @@ -75,6 +75,8 @@ public class FamilyOfGeneratedGenerics { cs_m.add(cons); } } + List methodConstraints5 = methodTypesWithoutClassTypes(cs_cl, cs_m); +// cs_m = methodConstraints5; return cs_m; } @@ -155,20 +157,20 @@ public class FamilyOfGeneratedGenerics { */ public static List typeOfTheMethodInClSigma(List allConstraints, HashMap> posOfTphs) { // cl_\sigma?? //TODO: - List tempCC= new ArrayList<>(); + 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, tempCC)) { - tempCC.add(consToAdd); + if (!checkForDuplicates(consToAdd, tempMC)) { + tempMC.add(consToAdd); } } } } } - return tempCC; + return tempMC; } @@ -178,7 +180,7 @@ public class FamilyOfGeneratedGenerics { */ public static List firstTransitiveSubtypeForMethodTypes(List allConstraints, List cs_m) { //transitive closure of cs //TODO: - List tempCC= new ArrayList<>(); + List tempMC= new ArrayList<>(); List tcOfCs = buildTransitiveClosure(allConstraints); for(MethodConstraint mC1 : cs_m) { //(R <. R') for(MethodConstraint mC2 : cs_m) { //(S <. S') @@ -187,14 +189,14 @@ public class FamilyOfGeneratedGenerics { 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, tempCC)) { - tempCC.add(consToAdd); + if (!checkForDuplicates(consToAdd, tempMC)) { + tempMC.add(consToAdd); } } } } } - return tempCC; + return tempMC; } /** @@ -203,24 +205,25 @@ public class FamilyOfGeneratedGenerics { */ public static List secondTransitiveSubtypeForMethodTypes(List allConstraints, List cs_cl, List cs_m) { //TODO: - List tempCC= new ArrayList<>(); + List tempMC= new ArrayList<>(); List tcOfCs = buildTransitiveClosure(allConstraints); for(ClassConstraint cC : cs_cl) { for(MethodConstraint mC : cs_m) { - String rightSide = mC.getRight(); - String leftSide = cC.getLeft(); + String leftSide = mC.getRight(); + String rightSide = cC.getLeft(); for(TPHConstraint tphC : tcOfCs) { if(tphC.getLeft().equals(leftSide)&&tphC.getRight().equals(rightSide)) { - if (!checkForDuplicates(tphC, tempCC)) { - tempCC.add((MethodConstraint) tphC); - System.out.println(tphC); + MethodConstraint consToAdd = new MethodConstraint(tphC.getLeft(), tphC.getRight(), tphC.getRel()); + if (!checkForDuplicates(consToAdd, tempMC)) { + tempMC.add(consToAdd); + System.out.println(consToAdd); } } } } } - return tempCC; + return tempMC; } /** @@ -230,26 +233,52 @@ public class FamilyOfGeneratedGenerics { */ public static List hasNoSupertypeForMethodTypes(List allConstraints, HashMap> posOfTphs) { //TODO: - List tempCC= new ArrayList<>(); - for(TPHConstraint allCons: allConstraints) { - for(TPHConstraint allCons2: allConstraints) { - if(posOfTphs.containsKey(allCons.getRight()) && (posOfTphs.get(allCons.getRight()).fst.equals(PositionFinder.Position.METHOD) ||posOfTphs.get(allCons.getRight()).fst.equals(PositionFinder.Position.CONSTRUCTOR)) && allCons.getRight()!=allCons2.getLeft()) { - MethodConstraint consToAdd = new MethodConstraint(allCons.getRight(), "Object", Relation.EXTENDS); - if (!checkForDuplicates(consToAdd, tempCC)) { - tempCC.add(consToAdd); + 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); System.out.println(consToAdd); } } } } - return tempCC; + return tempMC; } /** * nimm die Menge cs_cl aus cs_m raus */ - public static List methodTypesWithoutClassTypes() { + public static List methodTypesWithoutClassTypes(List cs_cl, List cs_m) { //TODO: + /*List tempMCToReturn = new ArrayList<>(); + List tempMC = new ArrayList(); + for (int i=0; i < cs_m.size(); i++) { + TPHConstraint tphC = (TPHConstraint) cs_m.get(i); + tempMC.add((TPHConstraint) tphC); + } + List tempCC = new ArrayList(cs_cl); + for (TPHConstraint cc: cs_cl) { + TPHConstraint tphC = TPHConstraint.class.cast(cc); + tempCC.add(tphC); + } + + for (TPHConstraint cc: tempCC) { + if(tempMC.contains(cc)) { + tempMC.remove(cc); + } + } + tempMCToReturn = List.class.cast(tempMC); + return tempMCToReturn;*/ + + List tempMC = new ArrayList<>(); + List tempTphC = new ArrayList<>(); + for(int i=0; i inputConstraints = new ArrayList<>(); @@ -33,7 +33,7 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { MethodConstraints should be the same as the input constraint */ List methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, new ArrayList(), tphPositions); - assertTrue(methodConstraints.size() == 1); + assertTrue(methodConstraints.size() == 2); assertTrue(methodConstraints.get(0).getLeft().equals("B")); assertTrue(methodConstraints.get(0).getRight().equals("A")); } @@ -46,7 +46,7 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { return f; } } - gives constraint: A <. B, which is a class constraint + gives constraint: A <. B and B <. Object which are class constraints */ List inputConstraints = new ArrayList<>(); @@ -109,8 +109,12 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions); System.out.println(classConstraints); + List methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, classConstraints, tphPositions); + System.out.println(methodConstraints); assertFalse(classConstraints.isEmpty()); assertTrue(classConstraints.size() == 6); + assertFalse(methodConstraints.isEmpty()); + assertTrue(methodConstraints.size() == 5); } @@ -165,9 +169,13 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { List classConstraints = FamilyOfGeneratedGenerics.getClassConstraints(inputConstraints, tphPositions); System.out.println(classConstraints); + List methodConstraints = FamilyOfGeneratedGenerics.getMethodConstraints(inputConstraints, classConstraints, tphPositions); + System.out.println(methodConstraints); assertFalse(classConstraints.isEmpty()); assertTrue(classConstraints.size() == 3); + assertFalse(methodConstraints.isEmpty()); + assertTrue(methodConstraints.size()==11); } diff --git a/src/test/java/insertGenerics/TestExample42.java b/src/test/java/insertGenerics/TestExample42.java index 911c2b5a..8d291a2d 100644 --- a/src/test/java/insertGenerics/TestExample42.java +++ b/src/test/java/insertGenerics/TestExample42.java @@ -25,24 +25,25 @@ public class TestExample42 { public HashMap> fillPosOfTphs() { HashMap> posOfTphs = new HashMap<>(); + // TPHs "U" und "L" auskommentiert, da nach Vorgaben L zu Z umbenannt und U als void interpretiert wird PairTphMethod posOfK = new PairTphMethod<>(PositionFinder.Position.FIELD, null); - PairTphMethod posOfL = new PairTphMethod<>(PositionFinder.Position.METHOD, "id"); +// PairTphMethod posOfL = new PairTphMethod<>(PositionFinder.Position.METHOD, "id"); PairTphMethod posOfM = new PairTphMethod<>(PositionFinder.Position.METHOD, "id"); PairTphMethod posOfN = new PairTphMethod<>(PositionFinder.Position.METHOD, "id"); PairTphMethod posOfP = new PairTphMethod<>(PositionFinder.Position.METHOD, "setA"); PairTphMethod posOfQ = new PairTphMethod<>(PositionFinder.Position.METHOD, "setA"); - PairTphMethod posOfU = new PairTphMethod<>(PositionFinder.Position.METHOD, "m"); +// PairTphMethod posOfU = new PairTphMethod<>(PositionFinder.Position.METHOD, "m"); PairTphMethod posOfV = new PairTphMethod<>(PositionFinder.Position.METHOD, "m"); PairTphMethod posOfW = new PairTphMethod<>(PositionFinder.Position.METHOD, "m"); PairTphMethod posOfZ = new PairTphMethod<>(PositionFinder.Position.METHOD, "m"); posOfTphs.put("K", posOfK); - posOfTphs.put("L", posOfL); +// posOfTphs.put("L", posOfL); posOfTphs.put("M", posOfM); posOfTphs.put("N", posOfN); posOfTphs.put("P", posOfP); posOfTphs.put("Q", posOfQ); - posOfTphs.put("U", posOfU); +// posOfTphs.put("U", posOfU); posOfTphs.put("V", posOfV); posOfTphs.put("W", posOfW); posOfTphs.put("Z", posOfZ); diff --git a/src/test/java/insertGenerics/TestExample42_allInOneMethod.java b/src/test/java/insertGenerics/TestExample42_allInOneMethod.java index d5783eda..f6fa8d7f 100644 --- a/src/test/java/insertGenerics/TestExample42_allInOneMethod.java +++ b/src/test/java/insertGenerics/TestExample42_allInOneMethod.java @@ -24,24 +24,25 @@ public class TestExample42_allInOneMethod { public HashMap> fillPosOfTphs() { HashMap> posOfTphs = new HashMap<>(); + // TPHs "U" und "L" auskommentiert, da nach Vorgaben L zu Z umbenannt und U als void interpretiert wird PairTphMethod posOfK = new PairTphMethod<>(PositionFinder.Position.FIELD, null); - PairTphMethod posOfL = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); +// PairTphMethod posOfL = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); PairTphMethod posOfM = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); PairTphMethod posOfN = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); PairTphMethod posOfP = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); PairTphMethod posOfQ = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); - PairTphMethod posOfU = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); +// PairTphMethod posOfU = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); PairTphMethod posOfV = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); PairTphMethod posOfW = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); PairTphMethod posOfZ = new PairTphMethod<>(PositionFinder.Position.METHOD, "sameMethod"); posOfTphs.put("K", posOfK); - posOfTphs.put("L", posOfL); +// posOfTphs.put("L", posOfL); posOfTphs.put("M", posOfM); posOfTphs.put("N", posOfN); posOfTphs.put("P", posOfP); posOfTphs.put("Q", posOfQ); - posOfTphs.put("U", posOfU); +// posOfTphs.put("U", posOfU); posOfTphs.put("V", posOfV); posOfTphs.put("W", posOfW); posOfTphs.put("Z", posOfZ);