diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index 9d196167..70810cbd 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -1027,7 +1027,7 @@ public class Codegen { } public byte[] generate() { - cw.visit(V1_8, clazz.modifiers() | ACC_PUBLIC, clazz.qualifiedName(), + cw.visit(V1_8, clazz.modifiers() | ACC_PUBLIC | ACC_SUPER, clazz.qualifiedName(), generateSignature(clazz), clazz.superType() != null ? clazz.superType().getInternalName(): "java/lang/Object", clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new) ); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index a421a087..43e11469 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -135,7 +135,7 @@ public class ASTToTargetAST { typeVariablesOfFields.addAll(findTypeVariables(field.getType())); } - //findTypeVariables(method.getReturnType(), typeVariables); + typeVariables.addAll(findTypeVariables(method.getReturnType())); for (var arg : method.getParameterList().getFormalparalist()) { typeVariables.addAll(findTypeVariables(arg.getType())); } @@ -459,7 +459,6 @@ public class ASTToTargetAST { addToPairs(result, new PairTPHequalRefTypeOrWildcardType(pair.right, OBJECT)); } } - eliminateCyclesAndInfima(result); usedTPHsOfMethods.put(method, typeVariables); @@ -569,6 +568,8 @@ public class ASTToTargetAST { } void eliminateInnerTypeVariables(Set referenced, Set> input) { + // Remove type variables that are part of a relation A < B < C where B is not in referenced + // Add new pair A < C var oldInput = new HashSet<>(input); for (var pair : oldInput) { if (pair instanceof PairTPHsmallerTPH ptph && !referenced.contains(ptph.left)) { @@ -582,6 +583,21 @@ public class ASTToTargetAST { } } } + + // Remove lone type variables, A = RefType where A is in no relation X < A or A < X and A is not in referenced + oldInput = new HashSet<>(input); + outer: + for (var pair : oldInput) { + if (pair instanceof PairTPHequalRefTypeOrWildcardType ptph) { + if (!referenced.contains(ptph.left)) { + for (var pair2 : oldInput) { + if (pair != pair2 && (pair2.getLeft().equals(ptph.left) || pair2.getRight().equals(ptph.left))) + continue outer; + } + input.remove(pair); + } + } + } } void eliminateCycles(Set> input) { diff --git a/src/test/java/targetast/TestCodegen.java b/src/test/java/targetast/TestCodegen.java index c1c2a5ee..39973a90 100644 --- a/src/test/java/targetast/TestCodegen.java +++ b/src/test/java/targetast/TestCodegen.java @@ -31,7 +31,7 @@ public class TestCodegen { private static void writeClassFile(String name, byte[] code) throws IOException { var path = Path.of(System.getProperty("user.dir"), "src/test/resources/target/"); Files.createDirectories(path); - Files.write(path.resolve(name + ".class"), code, StandardOpenOption.CREATE); + Files.write(path.resolve(name + ".class"), code); } public static Class generateClass(TargetClass clazz, IByteArrayClassLoader classLoader) throws IOException { diff --git a/src/test/java/targetast/TestGenerics.java b/src/test/java/targetast/TestGenerics.java index 8dea939b..1131e076 100644 --- a/src/test/java/targetast/TestGenerics.java +++ b/src/test/java/targetast/TestGenerics.java @@ -226,7 +226,6 @@ public class TestGenerics { } @Test - @Ignore("This doesn't work properly") public void testThreeArgs() throws Exception { var result = computeGenerics("TestThreeArgs.jav"); }