From 9abda637a14a58c5a58d2f1ca9083950ed3aa735 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Thu, 29 Aug 2019 10:49:18 +0200 Subject: [PATCH] Generic generator algorithm - fixed bug --- .../genericsGenerator/GenericsGenerator.java | 49 +++++++++++++++++-- .../GenericsGeneratorUtility.java | 1 + .../java/bytecode/FieldTphConsMethTest.java | 2 +- .../bytecode/javFiles/FieldTphConsMeth.jav | 23 +++++++-- src/test/resources/bytecode/javFiles/Tph2.jav | 7 +++ 5 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java index 077bd11d..4b964d5c 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java @@ -24,6 +24,11 @@ import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; * */ public class GenericsGenerator { + /*TODO: When generating generics for a class if we have the following: + tphClass < tphMeth1 < tphMeth2 + then we have to convert tphMeth1, tphMeth2 to tph class and generate the following + class constraints: tphClass < tphMeth1, tphMeth1 < tphMeth2, tphMeth2 < Object + */ public static GenericsGeneratorResultForClass generateConstraints(final String className, final TPHExtractor tphExtractor, final List tphsClass, final ConstraintsSimplierResult simplifiedConstraints) { @@ -78,6 +83,9 @@ public class GenericsGenerator { } private static void createConstraintsForClassTphs(ConstraintsSimplierResult simplifiedConstraints, List tphsClass, List constraints, List allCons, Set visitedTPHs, List methodTPHs) { + List classAndMethodTphs = new ArrayList<>(tphsClass); + classAndMethodTphs.addAll(methodTPHs); + for (String tph : tphsClass) { if (visitedTPHs.contains(tph)) @@ -85,13 +93,46 @@ public class GenericsGenerator { final LinkedList tphsInRel = GenericsGeneratorUtility .createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph); - if (!tphsInRel.isEmpty()) { - GenericsGeneratorResult constraint = generateGGResultForClass(tphsInRel, simplifiedConstraints, tphsClass); - constraints.add(constraint); - } + + generateConstraintsForClassFromList(tphsInRel,simplifiedConstraints,classAndMethodTphs,constraints); } } + private static void generateConstraintsForClassFromList(LinkedList tphsInRel, ConstraintsSimplierResult simplifiedConstraints, List classAndMethodTphs, List constraints) { + + if(tphsInRel.isEmpty()) + return; + + List resultConstraints = new ArrayList<>(); + String subType = tphsInRel.getFirst(); + String superType = GenericsGeneratorUtility.getNextClassTph(classAndMethodTphs, tphsInRel); + + int idxOfSuper = tphsInRel.indexOf(superType); + int size = tphsInRel.size(); + while (size > 2 && idxOfSuper < size-1){ + GenericsGeneratorResult genericsGeneratorResult = getGenericsGeneratorResultForClass(simplifiedConstraints, subType, superType); + constraints.add(genericsGeneratorResult); + + tphsInRel = new LinkedList<>(tphsInRel.subList(idxOfSuper, size)); + subType = tphsInRel.getFirst(); + superType = GenericsGeneratorUtility.getNextClassTph(classAndMethodTphs, tphsInRel); + + idxOfSuper = tphsInRel.indexOf(superType); + size = tphsInRel.size(); + } + GenericsGeneratorResult genericsGeneratorResult = getGenericsGeneratorResultForClass(simplifiedConstraints, subType, superType); + constraints.add(genericsGeneratorResult); + } + + private static GenericsGeneratorResult getGenericsGeneratorResultForClass(ConstraintsSimplierResult simplifiedConstraints, String subType, String superType) { + TPHConstraint constraint = new ExtendsConstraint(subType, superType); + + Set equalSet = GenericsGeneratorUtility + .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType); + + return new GenericsGeneratorResult(constraint, equalSet); + } + /* TODO Remove this method*/ private static GenericsGeneratorResult generateGGResultForClass(LinkedList tphsInRel, ConstraintsSimplierResult simplifiedConstraints, List tphsClass) { String subType = tphsInRel.getFirst(); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java index 321d8382..f909734f 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java @@ -544,6 +544,7 @@ public class GenericsGeneratorUtility { public static LinkedList createLinkedListForTPHsInRelationClass(List allCons, List tphsClass, List methodTPHs, Set visitedTPHs, String tph) { final LinkedList tphsInRel = new LinkedList<>(); + boolean isNextSuperTypeFound = findNextSuperTyp(allCons,tphsClass ,visitedTPHs, tph, tphsInRel); if(!tphsInRel.isEmpty() && !isNextSuperTypeFound && !methodTPHs.contains(tphsInRel.getLast())) findNextSubType(allCons, tphsClass,visitedTPHs, tph, tphsInRel); diff --git a/src/test/java/bytecode/FieldTphConsMethTest.java b/src/test/java/bytecode/FieldTphConsMethTest.java index 9f01a3d3..18bb7f2b 100644 --- a/src/test/java/bytecode/FieldTphConsMethTest.java +++ b/src/test/java/bytecode/FieldTphConsMethTest.java @@ -40,7 +40,7 @@ public class FieldTphConsMethTest { Field a = classToTest.getDeclaredField("a"); a.setAccessible(true); - Method m = classToTest.getDeclaredMethod("m", Object.class); + Method m = classToTest.getDeclaredMethod("id", Object.class); Object result = m.invoke(instanceOfClass, 42); assertEquals(42,result); diff --git a/src/test/resources/bytecode/javFiles/FieldTphConsMeth.jav b/src/test/resources/bytecode/javFiles/FieldTphConsMeth.jav index e749bb4f..5a550337 100644 --- a/src/test/resources/bytecode/javFiles/FieldTphConsMeth.jav +++ b/src/test/resources/bytecode/javFiles/FieldTphConsMeth.jav @@ -1,11 +1,26 @@ public class FieldTphConsMeth { a; - public FieldTphConsMeth(c) { - a = m(c); - } + /*public FieldTphConsMeth(c) { + a = id(c); + }*/ - m(b) { + id(b) { return b; } + + setA(x) { + a = x; + return a; + } + + m(x,y) { + x = id(y); + } + + m2(x,y) { + x = setA(y); + return x; + } + } \ No newline at end of file diff --git a/src/test/resources/bytecode/javFiles/Tph2.jav b/src/test/resources/bytecode/javFiles/Tph2.jav index c957eae6..760e4fa7 100644 --- a/src/test/resources/bytecode/javFiles/Tph2.jav +++ b/src/test/resources/bytecode/javFiles/Tph2.jav @@ -1,6 +1,13 @@ public class Tph2 { + id = x->x; + id3 (x) { + return id.apply(x); + } + + m(a,b){ var c = m2(a,b); + //m2(a,b); return a; }