forked from JavaTX/JavaCompilerCore
Generic generator algorithm - fixed bug
This commit is contained in:
parent
a41e9804a0
commit
9abda637a1
@ -24,6 +24,11 @@ import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class GenericsGenerator {
|
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,
|
public static GenericsGeneratorResultForClass generateConstraints(final String className, final TPHExtractor tphExtractor,
|
||||||
final List<String> tphsClass, final ConstraintsSimplierResult simplifiedConstraints) {
|
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) {
|
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) {
|
for (String tph : tphsClass) {
|
||||||
|
|
||||||
if (visitedTPHs.contains(tph))
|
if (visitedTPHs.contains(tph))
|
||||||
@ -85,13 +93,46 @@ public class GenericsGenerator {
|
|||||||
|
|
||||||
final LinkedList<String> tphsInRel = GenericsGeneratorUtility
|
final LinkedList<String> tphsInRel = GenericsGeneratorUtility
|
||||||
.createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph);
|
.createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph);
|
||||||
if (!tphsInRel.isEmpty()) {
|
|
||||||
GenericsGeneratorResult constraint = generateGGResultForClass(tphsInRel, simplifiedConstraints, tphsClass);
|
generateConstraintsForClassFromList(tphsInRel,simplifiedConstraints,classAndMethodTphs,constraints);
|
||||||
constraints.add(constraint);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
private static GenericsGeneratorResult generateGGResultForClass(LinkedList<String> tphsInRel,
|
||||||
ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass) {
|
ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass) {
|
||||||
String subType = tphsInRel.getFirst();
|
String subType = tphsInRel.getFirst();
|
||||||
|
@ -544,6 +544,7 @@ public class GenericsGeneratorUtility {
|
|||||||
public static LinkedList<String> createLinkedListForTPHsInRelationClass(List<TPHConstraint> allCons,
|
public static LinkedList<String> createLinkedListForTPHsInRelationClass(List<TPHConstraint> allCons,
|
||||||
List<String> tphsClass, List<String> methodTPHs, Set<String> visitedTPHs, String tph) {
|
List<String> tphsClass, List<String> methodTPHs, Set<String> visitedTPHs, String tph) {
|
||||||
final LinkedList<String> tphsInRel = new LinkedList<>();
|
final LinkedList<String> tphsInRel = new LinkedList<>();
|
||||||
|
|
||||||
boolean isNextSuperTypeFound = findNextSuperTyp(allCons,tphsClass ,visitedTPHs, tph, tphsInRel);
|
boolean isNextSuperTypeFound = findNextSuperTyp(allCons,tphsClass ,visitedTPHs, tph, tphsInRel);
|
||||||
if(!tphsInRel.isEmpty() && !isNextSuperTypeFound && !methodTPHs.contains(tphsInRel.getLast()))
|
if(!tphsInRel.isEmpty() && !isNextSuperTypeFound && !methodTPHs.contains(tphsInRel.getLast()))
|
||||||
findNextSubType(allCons, tphsClass,visitedTPHs, tph, tphsInRel);
|
findNextSubType(allCons, tphsClass,visitedTPHs, tph, tphsInRel);
|
||||||
|
@ -40,7 +40,7 @@ public class FieldTphConsMethTest {
|
|||||||
Field a = classToTest.getDeclaredField("a");
|
Field a = classToTest.getDeclaredField("a");
|
||||||
a.setAccessible(true);
|
a.setAccessible(true);
|
||||||
|
|
||||||
Method m = classToTest.getDeclaredMethod("m", Object.class);
|
Method m = classToTest.getDeclaredMethod("id", Object.class);
|
||||||
Object result = m.invoke(instanceOfClass, 42);
|
Object result = m.invoke(instanceOfClass, 42);
|
||||||
|
|
||||||
assertEquals(42,result);
|
assertEquals(42,result);
|
||||||
|
@ -1,11 +1,26 @@
|
|||||||
public class FieldTphConsMeth {
|
public class FieldTphConsMeth {
|
||||||
|
|
||||||
a;
|
a;
|
||||||
public FieldTphConsMeth(c) {
|
/*public FieldTphConsMeth(c) {
|
||||||
a = m(c);
|
a = id(c);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
m(b) {
|
id(b) {
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setA(x) {
|
||||||
|
a = x;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
m(x,y) {
|
||||||
|
x = id(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
m2(x,y) {
|
||||||
|
x = setA(y);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,6 +1,13 @@
|
|||||||
public class Tph2 {
|
public class Tph2 {
|
||||||
|
id = x->x;
|
||||||
|
id3 (x) {
|
||||||
|
return id.apply(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
m(a,b){
|
m(a,b){
|
||||||
var c = m2(a,b);
|
var c = m2(a,b);
|
||||||
|
//m2(a,b);
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user