From a7720fbf20b3b8888e80dc7f8aceccffecc5600f Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 31 Jan 2023 13:49:19 +0100 Subject: [PATCH 1/6] Don't append new class files, overwrite them --- src/main/java/de/dhbwstuttgart/bytecode/Codegen.java | 6 +++--- src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index 9d196167f..ee6047791 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -979,7 +979,7 @@ public class Codegen { } private void generateConstructor(TargetConstructor constructor) { - MethodVisitor mv = cw.visitMethod(constructor.access() | ACC_PUBLIC, "", constructor.getDescriptor(), constructor.getSignature(), null); + MethodVisitor mv = cw.visitMethod(constructor.access() | ACC_PUBLIC, "", constructor.getDescriptor(), null, null); mv.visitCode(); var state = new State(null, mv, 1); for (var param: constructor.parameters()) @@ -1027,8 +1027,8 @@ public class Codegen { } public byte[] generate() { - cw.visit(V1_8, clazz.modifiers() | ACC_PUBLIC, clazz.qualifiedName(), - generateSignature(clazz), clazz.superType() != null ? clazz.superType().getInternalName(): "java/lang/Object", + cw.visit(V1_8, clazz.modifiers() | ACC_PUBLIC | ACC_SUPER, clazz.qualifiedName(), + null, clazz.superType() != null ? clazz.superType().getInternalName(): "java/lang/Object", clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new) ); clazz.fields().forEach(this::generateField); diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 67e553e65..ce312d01d 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -909,7 +909,7 @@ public class JavaTXCompiler { // "/testBytecode/generatedBC/" +name+".class")); File outputFile = new File(path, name.getClassName() + ".class"); outputFile.getAbsoluteFile().getParentFile().mkdirs(); - output = new FileOutputStream(outputFile); + output = new FileOutputStream(outputFile, false); output.write(bytecode); output.close(); System.out.println(name + ".class file generated"); From baec9999dbebcca003fef3afc63ec14015aab9e4 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 31 Jan 2023 14:21:16 +0100 Subject: [PATCH 2/6] Don't append new class files, overwrite them --- src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- src/test/java/targetast/TestCodegen.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index ce312d01d..67e553e65 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -909,7 +909,7 @@ public class JavaTXCompiler { // "/testBytecode/generatedBC/" +name+".class")); File outputFile = new File(path, name.getClassName() + ".class"); outputFile.getAbsoluteFile().getParentFile().mkdirs(); - output = new FileOutputStream(outputFile, false); + output = new FileOutputStream(outputFile); output.write(bytecode); output.close(); System.out.println(name + ".class file generated"); diff --git a/src/test/java/targetast/TestCodegen.java b/src/test/java/targetast/TestCodegen.java index c1c2a5ee7..acfa46d9f 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, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } public static Class generateClass(TargetClass clazz, IByteArrayClassLoader classLoader) throws IOException { From 21fb477ee1012b20d16ce819a799c2b7ff83b45a Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 31 Jan 2023 14:25:37 +0100 Subject: [PATCH 3/6] These are the defaults, actually --- src/test/java/targetast/TestCodegen.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/targetast/TestCodegen.java b/src/test/java/targetast/TestCodegen.java index acfa46d9f..39973a90a 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, StandardOpenOption.TRUNCATE_EXISTING); + Files.write(path.resolve(name + ".class"), code); } public static Class generateClass(TargetClass clazz, IByteArrayClassLoader classLoader) throws IOException { From 62fad15b6f38f74df9e800251232d25b7b2ec754 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 31 Jan 2023 14:53:02 +0100 Subject: [PATCH 4/6] We actually need the signature of the class --- src/main/java/de/dhbwstuttgart/bytecode/Codegen.java | 4 ++-- .../de/dhbwstuttgart/target/generate/ASTToTargetAST.java | 5 ++--- src/test/java/targetast/TestGenerics.java | 1 - 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index ee6047791..70810cbd6 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -979,7 +979,7 @@ public class Codegen { } private void generateConstructor(TargetConstructor constructor) { - MethodVisitor mv = cw.visitMethod(constructor.access() | ACC_PUBLIC, "", constructor.getDescriptor(), null, null); + MethodVisitor mv = cw.visitMethod(constructor.access() | ACC_PUBLIC, "", constructor.getDescriptor(), constructor.getSignature(), null); mv.visitCode(); var state = new State(null, mv, 1); for (var param: constructor.parameters()) @@ -1028,7 +1028,7 @@ public class Codegen { public byte[] generate() { cw.visit(V1_8, clazz.modifiers() | ACC_PUBLIC | ACC_SUPER, clazz.qualifiedName(), - null, clazz.superType() != null ? clazz.superType().getInternalName(): "java/lang/Object", + generateSignature(clazz), clazz.superType() != null ? clazz.superType().getInternalName(): "java/lang/Object", clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new) ); clazz.fields().forEach(this::generateField); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index a421a0878..28b059c8b 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); @@ -471,7 +470,7 @@ public class ASTToTargetAST { } typeVariablesOfSignature.addAll(findTypeVariables(method.getReturnType())); - eliminateInnerTypeVariables(typeVariablesOfSignature, result); + //eliminateInnerTypeVariables(typeVariablesOfSignature, result); System.out.println(method.name + ": " + result); return result; diff --git a/src/test/java/targetast/TestGenerics.java b/src/test/java/targetast/TestGenerics.java index 8dea939b7..1131e076a 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"); } From 5243b160f5fbc1ff61452991ed494b666523ec8a Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 31 Jan 2023 15:00:09 +0100 Subject: [PATCH 5/6] Didn't want to remove this --- .../java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 28b059c8b..14a3c7d53 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -470,7 +470,7 @@ public class ASTToTargetAST { } typeVariablesOfSignature.addAll(findTypeVariables(method.getReturnType())); - //eliminateInnerTypeVariables(typeVariablesOfSignature, result); + eliminateInnerTypeVariables(typeVariablesOfSignature, result); System.out.println(method.name + ": " + result); return result; From 64ec7989b477185e6291e20ae7805bec98250e66 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 31 Jan 2023 15:39:12 +0100 Subject: [PATCH 6/6] Continue with eliminating inner type variables --- .../target/generate/ASTToTargetAST.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 14a3c7d53..43e11469f 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -568,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)) { @@ -581,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) {