Generic generator algorithm - fixed bug

This commit is contained in:
Fayez Abu Alia 2019-08-29 10:49:18 +02:00
parent a41e9804a0
commit 9abda637a1
5 changed files with 73 additions and 9 deletions

View File

@ -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<String> tphsClass, final ConstraintsSimplierResult simplifiedConstraints) {
@ -78,6 +83,9 @@ public class GenericsGenerator {
}
private static void createConstraintsForClassTphs(ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass, List<GenericsGeneratorResult> constraints, List<TPHConstraint> allCons, Set<String> visitedTPHs, List<String> methodTPHs) {
List<String> classAndMethodTphs = new ArrayList<>(tphsClass);
classAndMethodTphs.addAll(methodTPHs);
for (String tph : tphsClass) {
if (visitedTPHs.contains(tph))
@ -85,13 +93,46 @@ public class GenericsGenerator {
final LinkedList<String> 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<String> tphsInRel, ConstraintsSimplierResult simplifiedConstraints, List<String> classAndMethodTphs, List<GenericsGeneratorResult> constraints) {
if(tphsInRel.isEmpty())
return;
List<GenericsGeneratorResult> 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<String> equalSet = GenericsGeneratorUtility
.createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType);
return new GenericsGeneratorResult(constraint, equalSet);
}
/* TODO Remove this method*/
private static GenericsGeneratorResult generateGGResultForClass(LinkedList<String> tphsInRel,
ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass) {
String subType = tphsInRel.getFirst();

View File

@ -544,6 +544,7 @@ public class GenericsGeneratorUtility {
public static LinkedList<String> createLinkedListForTPHsInRelationClass(List<TPHConstraint> allCons,
List<String> tphsClass, List<String> methodTPHs, Set<String> visitedTPHs, String tph) {
final LinkedList<String> 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);

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}