diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 4416a4f5f..408ed60da 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -36,7 +36,7 @@ public class ASTToTargetAST { return all.stream().map(GenericsResult::new).toList(); } - record Generics(Set> javaGenerics, Set> txGenerics) {} + record Generics(Set> javaGenerics, Set txGenerics) {} class Sigma { Map computedGenericsOfMethods = new HashMap<>(); @@ -142,10 +142,11 @@ public class ASTToTargetAST { Set> txResult = new HashSet<>(); Set> javaResult = new HashSet<>(); - var generics = new Generics(javaResult, txResult); + Set generatedTxGenerics = new HashSet<>(); + var generics = new Generics(javaResult, generatedTxGenerics); computedGenericsOfMethods.put(method, generics); - var genericsOfClass = generics(owner).txGenerics(); + var genericsOfClass = generics(owner).javaGenerics(); var simplifiedConstraints = new HashSet<>(this.simplifiedConstraints); HashSet typeVariables = new HashSet<>(); @@ -225,7 +226,7 @@ public class ASTToTargetAST { if (optMethod.isEmpty()) return; var method = optMethod.get(); - var generics = generics(owner, method).txGenerics(); + var generics = generics(owner, method).javaGenerics(); // transitive and var all = transitiveClosure(generics); @@ -535,6 +536,9 @@ public class ASTToTargetAST { referenced.addAll(typeVariablesOfClass); eliminateInnerTypeVariables(referenced, txResult); + + // Generate generics with current equality + generatedTxGenerics.addAll(convert(txResult)); } equality = storedEq; @@ -579,7 +583,8 @@ public class ASTToTargetAST { Set> txResult = new HashSet<>(); Set> javaResult = new HashSet<>(); - var generics = new Generics(javaResult, txResult); + Set generatedTxGenerics = new HashSet<>(); + var generics = new Generics(javaResult, generatedTxGenerics); for (var field : classOrInterface.getFieldDecl()) { findAllBounds(field.getType(), txResult); @@ -587,12 +592,23 @@ public class ASTToTargetAST { computedGenericsOfClasses.put(classOrInterface, generics); javaResult.addAll(txResult); - eliminateCycles(javaResult); - eliminateInfima(txResult); - eliminateInfima(javaResult); - eliminateInnerTypeVariablesOfClass(classOrInterface, txResult); - eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult); - equalizeTypeVariables(javaResult); + + var eq = new HashMap<>(equality); + { // Java Generics + eliminateCycles(javaResult); + eliminateInfima(javaResult); + eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult); + equalizeTypeVariables(javaResult); + } + var storedEq = equality; //TODO Hack: pass equality as parameter instead + equality = eq; + { + eliminateInfima(txResult); + eliminateInnerTypeVariablesOfClass(classOrInterface, txResult); + } + equality = storedEq; + + generatedTxGenerics.addAll(convert(txResult)); System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult); return generics; @@ -928,7 +944,7 @@ public class ASTToTargetAST { // Generate generics only if there are no user defined ones var generics = sigma.generics(input); javaGenerics = convert(generics.javaGenerics()); - txGenerics = convert(generics.txGenerics()); + txGenerics = generics.txGenerics(); } TargetBlock fieldInitializer = null; @@ -954,8 +970,8 @@ public class ASTToTargetAST { return generics.stream().anyMatch(g -> g.name().equals(type.getParsedName())); } - private Set collectMethodGenerics(Set> generics, Method input) { - var convertedGenerics = new HashSet<>(convert(generics)); + private Set collectMethodGenerics(Set generics, Method input) { + var convertedGenerics = new HashSet<>(generics); outer: for (GenericTypeVar typeVar : input.getGenerics()) { for (var classGeneric : currentClass.getGenerics()) { @@ -989,7 +1005,7 @@ public class ASTToTargetAST { var generics = sigma.generics(currentClass, input); List params = convert(input.getParameterList()); if (parameterSet.stream().noneMatch(p -> p.equals(params))) { - var javaGenerics = collectMethodGenerics(generics.javaGenerics(), input); + var javaGenerics = collectMethodGenerics(convert(generics.javaGenerics()), input); var txGenerics = collectMethodGenerics(generics.txGenerics(), input); result.add(new TargetConstructor(input.modifier, javaGenerics, txGenerics, params, convert(input.block), fieldInitializer)); @@ -1010,7 +1026,7 @@ public class ASTToTargetAST { var generics = sigma.generics(currentClass, input); List params = convert(input.getParameterList()); if (parameterSet.stream().noneMatch(p -> p.equals(params))) { - var javaGenerics = collectMethodGenerics(generics.javaGenerics(), input); + var javaGenerics = collectMethodGenerics(convert(generics.javaGenerics()), input); var txGenerics = collectMethodGenerics(generics.txGenerics(), input); result.add(new TargetMethod( diff --git a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java index e0b736c94..384b0614e 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java @@ -16,16 +16,17 @@ public class GenericsResult { } public Set> get(ClassOrInterface clazz) { - var generics = this.sigma.computedGenericsOfClasses.get(clazz); + var generics = this.sigma.computedGenericsOfClasses.get(clazz); if (generics == null) return Set.of(); - return generics.txGenerics(); + return generics.javaGenerics(); } // TODO Compute generics if not present? + // TODO Maybe use tx Generics here (two equalities) public Set> get(Method method) { var generics = this.sigma.computedGenericsOfMethods.get(method); if (generics == null) return Set.of(); - return generics.txGenerics(); + return generics.javaGenerics(); } public BoundsList getBounds(RefTypeOrTPHOrWildcardOrGeneric type, ClassOrInterface clazz) {