diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index b4f6978bd..14dd4afe3 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -121,6 +121,16 @@ public class ASTToTargetAST { input.add(pair); } + static void addToEquality(Map equality, TypePlaceholder from, TypePlaceholder to) { + for (var entry : new HashSet<>(equality.entrySet())) { + if (entry.getValue().equals(from)) { + equality.remove(entry.getKey()); + equality.put(entry.getKey(), to); + } + } + equality.put(from, to); + } + static Set> transitiveClosure(Set> generics) { Set> all = new HashSet<>(generics); HashSet> toAdd = new HashSet<>(); @@ -152,6 +162,7 @@ public class ASTToTargetAST { ) { // Type variables with bounds that are also type variables of the method for (var typeVariable : new HashSet<>(typeVariables)) { + typeVariable = equality.getOrDefault(typeVariable, typeVariable); if (classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, typeVariable)) continue; for (var pair : simplifiedConstraints) { @@ -573,7 +584,6 @@ public class ASTToTargetAST { } System.out.println(method.name + ": " + txResult + " & " + javaResult); - return generics; } @@ -621,20 +631,17 @@ public class ASTToTargetAST { findAllBounds(field.getType(), txResult, txEquality); } - { // Java Generics - var referenced = new HashSet(); - eliminateCycles(javaResult, equality, referenced); - eliminateInfima(javaResult, equality, referenced); - eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult, equality, referenced); - equalizeTypeVariables(javaResult, equality); - } - { // TX Generics - var referenced = new HashSet(); - eliminateInfima(txResult, txEquality, referenced); - eliminateInnerTypeVariablesOfClass(classOrInterface, txResult, txEquality, referenced); - } + var referenced = new HashSet(); + eliminateCycles(javaResult, equality, referenced); + eliminateInfima(javaResult, equality, referenced); + var txReferenced = new HashSet(); + eliminateInfima(txResult, txEquality, txReferenced); - System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult); + eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult, equality, referenced); + equalizeTypeVariables(javaResult, equality); + eliminateInnerTypeVariablesOfClass(classOrInterface, txResult, txEquality, txReferenced); + + System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult + ", " + javaResult); return generics; } @@ -642,7 +649,7 @@ public class ASTToTargetAST { for (var pair : new HashSet<>(input)) { if (pair instanceof PairTPHsmallerTPH ptph) { if (ptph.left.getVariance() == 1 && ptph.right.getVariance() == -1) { - equality.put(ptph.left, ptph.right); + addToEquality(equality, ptph.left, ptph.right); input.remove(ptph); for (var pair2 : new HashSet<>(input)) { if (pair2 instanceof PairTPHsmallerTPH ptph2 && ptph2.right.equals(ptph.left)) { @@ -735,7 +742,7 @@ public class ASTToTargetAST { var right = cycle.get(i + 1); var pair = new PairTPHsmallerTPH(left, right); input.remove(pair); - equality.put(left, newTph); + addToEquality(equality, left, newTph); } } } @@ -759,7 +766,7 @@ public class ASTToTargetAST { addToPairs(input, new PairTPHsmallerTPH(left, newTph)); input.removeAll(infima); for (var infimum : infima) { - equality.put(infimum.right, newTph); + addToEquality(equality, infimum.right, newTph); new HashSet<>(input).forEach(pair -> { if (pair.getLeft().equals(infimum.right)) { input.remove(pair); diff --git a/src/test/java/targetast/TestGenerics.java b/src/test/java/targetast/TestGenerics.java index 2679f6d4b..fdeaab9b9 100644 --- a/src/test/java/targetast/TestGenerics.java +++ b/src/test/java/targetast/TestGenerics.java @@ -51,6 +51,7 @@ public class TestGenerics { var anyMethod = result.findMethod("anyMethod"); var otherMethod = result.findMethod("otherMethod"); var a = result.findField("a"); + var b = result.findField("b"); var generics = result.genericsResults.get(0); assertEquals(1, generics.get(anyMethod).size()); @@ -60,7 +61,7 @@ public class TestGenerics { var ECK2 = generics.getBounds(otherMethod.getReturnType(), result.clazz, anyMethod); var ECKChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)); assertEquals(ECK1, ECK2); - assertEquals(ECK2, ECKChain); + assertEquals(ECK2, generics.getBounds(b.getType(), result.clazz)); var M = generics.getBounds(a.getType(), result.clazz); var MChain = new BoundsList(new Bound(false, TypePlaceholder.of("ECK")), new Bound(false, ASTToTargetAST.OBJECT));