From 2dff7909ed9c07a661b8f1d7a962277991b7fbbf Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 21 Mar 2023 14:00:58 +0100 Subject: [PATCH 01/16] Remove test file --- Test.java | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 Test.java diff --git a/Test.java b/Test.java deleted file mode 100644 index b0e1ed48..00000000 --- a/Test.java +++ /dev/null @@ -1,7 +0,0 @@ -public interface Test extends Base { - -} - -interface Base { - -} From 6b767bc09b98c81817c7f8f6ed79fda8be420de2 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 21 Mar 2023 15:06:17 +0100 Subject: [PATCH 02/16] Fix using the wrong constructor --- resources/insertGenericsJav/TestAssign.jav | 0 src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 resources/insertGenericsJav/TestAssign.jav diff --git a/resources/insertGenericsJav/TestAssign.jav b/resources/insertGenericsJav/TestAssign.jav new file mode 100644 index 00000000..e69de29b diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 11f915b4..fc1b0000 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -880,7 +880,7 @@ public class JavaTXCompiler { path = new File(outputPath ,sf.getPkgName().replace(".","/")); //add package path to root path } - var converter = new ASTToTargetAST(typeinferenceResult); + var converter = new ASTToTargetAST(typeinferenceResult, sf, classLoader); var generatedClasses = new HashMap(); for (var clazz : sf.getClasses()) { var codegen = new Codegen(converter.convert(clazz)); From b55fc718117dbf94e36ff590c5e4134d11711e5e Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Fri, 24 Mar 2023 13:27:29 +0100 Subject: [PATCH 03/16] Let generateClassFiles accept multiple arguments --- resources/insertGenericsJav/TestAssign.jav | 9 ++ .../target/generate/ASTToTargetAST.java | 39 ++++++--- src/test/java/targetast/GreaterEqualTest.java | 2 +- src/test/java/targetast/GreaterThanTest.java | 2 +- src/test/java/targetast/InheritTest.java | 12 ++- src/test/java/targetast/InheritTest2.java | 10 +-- src/test/java/targetast/LessEqualTest.java | 2 +- src/test/java/targetast/LessThanTest.java | 2 +- src/test/java/targetast/OLTest.java | 2 +- src/test/java/targetast/PostIncTest.java | 2 +- src/test/java/targetast/PreIncTest.java | 2 +- src/test/java/targetast/PutTest.java | 2 +- src/test/java/targetast/TestCodegen.java | 42 ++++++---- src/test/java/targetast/TestComplete.java | 84 +++++++++---------- src/test/java/targetast/TestGenerics.java | 6 ++ src/test/java/targetast/TphTest.java | 2 +- src/test/java/targetast/WhileTest.java | 2 +- 17 files changed, 127 insertions(+), 95 deletions(-) diff --git a/resources/insertGenericsJav/TestAssign.jav b/resources/insertGenericsJav/TestAssign.jav index e69de29b..d401641e 100644 --- a/resources/insertGenericsJav/TestAssign.jav +++ b/resources/insertGenericsJav/TestAssign.jav @@ -0,0 +1,9 @@ +class TestAssign { + assign(x, y) { + x = y; + } + + assign2(x, y) { + assign(x, y); + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 9a7d9ccf..81cb295e 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -36,7 +36,11 @@ public class ASTToTargetAST { return all.stream().map(GenericsResult::new).toList(); } - record Generics(Set> javaGenerics, Set> txGenerics) {} + record Generics(Set> javaGenerics, Set> txGenerics, Set userDefinedGenerics) { + Generics(Set> javaGenerics, Set> txGenerics) { + this(javaGenerics, txGenerics, Set.of()); + } + } class Sigma { Map computedGenericsOfMethods = new HashMap<>(); @@ -140,14 +144,16 @@ public class ASTToTargetAST { private void methodFindConstraints( ClassOrInterface owner, Method method, Set simplifiedConstraints, - HashSet typeVariables, - HashSet typeVariablesOfClass, + Set typeVariables, + Set userDefinedGenericsOfClass, + Set typeVariablesOfClass, Set> result, Map equality ) { // Type variables with bounds that are also type variables of the method for (var typeVariable : new HashSet<>(typeVariables)) { - if (typeVariablesOfClass.contains(typeVariable)) continue; + if (classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, typeVariable)) + continue; for (var pair : simplifiedConstraints) { if (pair.left.equals(typeVariable) && typeVariables.contains(pair.right)) { addToPairs(result, new PairTPHsmallerTPH(pair.left, equality.getOrDefault(pair.right, pair.right))); @@ -192,7 +198,6 @@ public class ASTToTargetAST { var optMethod = findMethod(owner, methodCall.name, methodCall.getArgumentList()); if (optMethod.isEmpty()) return; var method = optMethod.get(); - var generics = generics(owner, method).javaGenerics(); // transitive and @@ -448,7 +453,8 @@ public class ASTToTargetAST { // All unbounded type variables (bounds not in method) outer: for (var typeVariable : typeVariables) { - if (typeVariablesOfClass.contains(typeVariable)) continue; + if (classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, typeVariable)) + continue; for (var pair : result) { if (pair.getLeft().equals(typeVariable)) continue outer; @@ -463,12 +469,16 @@ public class ASTToTargetAST { if (pair.right.equals(pair2.left)) continue outer; } - if (!typeVariablesOfClass.contains(pair.right) && typeVariables.contains(pair.right)) { + if (!classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, pair.right) && typeVariables.contains(pair.right)) { addToPairs(result, new PairTPHequalRefTypeOrWildcardType(pair.right, OBJECT)); } } } + private boolean classHasGeneric(Set userDefinedGenericsOfClass, Set typeVariablesOfClass, TypePlaceholder typeVariable) { + return typeVariablesOfClass.contains(typeVariable) || userDefinedGenericsOfClass.stream().anyMatch(g -> g.getName().equals(typeVariable.getName())); + } + private void methodFindTypeVariables( Method method, Set> genericsOfClass, @@ -514,7 +524,8 @@ public class ASTToTargetAST { var generics = new Generics(javaResult, txResult); computedGenericsOfMethods.put(method, generics); - var genericsOfClass = generics(owner).javaGenerics(); + var classGenerics = generics(owner); + var genericsOfClass = classGenerics.javaGenerics(); var simplifiedConstraints = new HashSet<>(this.simplifiedConstraints); HashSet txTypeVariables = new HashSet<>(); @@ -525,8 +536,8 @@ public class ASTToTargetAST { methodFindTypeVariables(method, genericsOfClass, javaTypeVariablesOfClass, javaTypeVariables, equality); methodFindTypeVariables(method, genericsOfClass, txTypeVariablesOfClass, txTypeVariables, txEquality); - methodFindConstraints(owner, method, simplifiedConstraints, javaTypeVariables, javaTypeVariablesOfClass, javaResult, equality); - methodFindConstraints(owner, method, simplifiedConstraints, txTypeVariables, txTypeVariablesOfClass, txResult, txEquality); + methodFindConstraints(owner, method, simplifiedConstraints, javaTypeVariables, classGenerics.userDefinedGenerics, javaTypeVariablesOfClass, javaResult, equality); + methodFindConstraints(owner, method, simplifiedConstraints, txTypeVariables, classGenerics.userDefinedGenerics, txTypeVariablesOfClass, txResult, txEquality); { // Java Generics var referenced = new HashSet(); @@ -949,9 +960,11 @@ public class ASTToTargetAST { var genericsIter = input.getGenerics().iterator(); if (genericsIter.hasNext()) { // Add empty set of generics to cache so that it doesn't try to calculate it later - sigma.computedGenericsOfClasses.put(input, new Generics(new HashSet<>(), new HashSet<>())); + var userDefinedGenerics = new HashSet(); + sigma.computedGenericsOfClasses.put(input, new Generics(new HashSet<>(), new HashSet<>(), userDefinedGenerics)); while (genericsIter.hasNext()) { var next = genericsIter.next(); + userDefinedGenerics.add(next); javaGenerics.addAll(convert(next)); } } else { @@ -995,7 +1008,7 @@ public class ASTToTargetAST { } convertedGenerics.addAll(convert(typeVar)); } - var returnType = sigma.getType(input.getReturnType(), equality); + /*var returnType = sigma.getType(input.getReturnType(), equality); if ((returnType instanceof GenericRefType refType) && !hasGeneric(convertedGenerics, refType)) { convertedGenerics.add(new TargetGeneric(refType.getParsedName(), convert(OBJECT))); } @@ -1004,7 +1017,7 @@ public class ASTToTargetAST { if (type instanceof GenericRefType refType && !hasGeneric(convertedGenerics, refType)) { convertedGenerics.add(new TargetGeneric(refType.getParsedName(), convert(OBJECT))); } - } + }*/ return convertedGenerics; } diff --git a/src/test/java/targetast/GreaterEqualTest.java b/src/test/java/targetast/GreaterEqualTest.java index c104433f..1edda1d3 100644 --- a/src/test/java/targetast/GreaterEqualTest.java +++ b/src/test/java/targetast/GreaterEqualTest.java @@ -16,7 +16,7 @@ public class GreaterEqualTest { @BeforeClass public static void beforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("GreaterEqual.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "GreaterEqual.jav"); classToTest = classFiles.get("GreaterEqual"); instance = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/GreaterThanTest.java b/src/test/java/targetast/GreaterThanTest.java index fd1d654a..4174492d 100644 --- a/src/test/java/targetast/GreaterThanTest.java +++ b/src/test/java/targetast/GreaterThanTest.java @@ -15,7 +15,7 @@ public class GreaterThanTest { @BeforeClass public static void beforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("GreaterThan.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "GreaterThan.jav"); classToTest = classFiles.get("GreaterThan"); instance = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/InheritTest.java b/src/test/java/targetast/InheritTest.java index 9e9fff71..9a8de6fd 100644 --- a/src/test/java/targetast/InheritTest.java +++ b/src/test/java/targetast/InheritTest.java @@ -17,14 +17,12 @@ public class InheritTest { @BeforeClass public static void setUpBeforeClass() throws Exception { var classLoader = new ByteArrayClassLoader(); - // TODO Box is loaded and has a method called m so it is generating overloads for it - classLoader.loadClass(Path.of(System.getProperty("user.dir"), "/src/test/resources/target/Box.class")); - classToTest = TestCodegen.generateClassFiles("Inherit.jav", classLoader).get("Inherit"); - classToTestAA = TestCodegen.generateClassFiles("AA.jav", classLoader).get("AA"); - classToTestBB = TestCodegen.generateClassFiles("BB.jav", classLoader).get("BB"); - classToTestCC = TestCodegen.generateClassFiles("CC.jav", classLoader).get("CC"); - classToTestDD = TestCodegen.generateClassFiles("DD.jav", classLoader).get("DD"); + classToTestAA = TestCodegen.generateClassFiles(classLoader, "AA.jav").get("AA"); + classToTestBB = TestCodegen.generateClassFiles(classLoader, "BB.jav").get("BB"); + classToTestCC = TestCodegen.generateClassFiles(classLoader, "CC.jav").get("CC"); + classToTestDD = TestCodegen.generateClassFiles(classLoader, "DD.jav").get("DD"); + classToTest = TestCodegen.generateClassFiles(classLoader, "Inherit.jav").get("Inherit"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); instanceOfClassAA = classToTestAA.getDeclaredConstructor().newInstance(); diff --git a/src/test/java/targetast/InheritTest2.java b/src/test/java/targetast/InheritTest2.java index 87e052f1..d1bad977 100644 --- a/src/test/java/targetast/InheritTest2.java +++ b/src/test/java/targetast/InheritTest2.java @@ -16,11 +16,11 @@ public class InheritTest2 { @BeforeClass public static void setUpBeforeClass() throws Exception { var classLoader = new ByteArrayClassLoader(); - classToTest = TestCodegen.generateClassFiles("Inherit2.jav", classLoader).get("Inherit2"); - classToTestAA = TestCodegen.generateClassFiles("AA.jav", classLoader).get("AA"); - classToTestBB = TestCodegen.generateClassFiles("BB.jav", classLoader).get("BB"); - classToTestCC = TestCodegen.generateClassFiles("CC.jav", classLoader).get("CC"); - classToTestDD = TestCodegen.generateClassFiles("DD.jav", classLoader).get("DD"); + classToTest = TestCodegen.generateClassFiles(classLoader, "Inherit2.jav").get("Inherit2"); + classToTestAA = TestCodegen.generateClassFiles(classLoader, "AA.jav").get("AA"); + classToTestBB = TestCodegen.generateClassFiles(classLoader, "BB.jav").get("BB"); + classToTestCC = TestCodegen.generateClassFiles(classLoader, "CC.jav").get("CC"); + classToTestDD = TestCodegen.generateClassFiles(classLoader,"DD.jav").get("DD"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); instanceOfClassAA = classToTestAA.getDeclaredConstructor().newInstance(); diff --git a/src/test/java/targetast/LessEqualTest.java b/src/test/java/targetast/LessEqualTest.java index af5237cc..bb1fe888 100644 --- a/src/test/java/targetast/LessEqualTest.java +++ b/src/test/java/targetast/LessEqualTest.java @@ -15,7 +15,7 @@ public class LessEqualTest { @BeforeClass public static void beforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("LessEqual.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "LessEqual.jav"); classToTest = classFiles.get("LessEqual"); instance = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/LessThanTest.java b/src/test/java/targetast/LessThanTest.java index 7b0d45c8..75eeb1b0 100644 --- a/src/test/java/targetast/LessThanTest.java +++ b/src/test/java/targetast/LessThanTest.java @@ -15,7 +15,7 @@ public class LessThanTest { @BeforeClass public static void beforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("LessThan.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "LessThan.jav"); classToTest = classFiles.get("LessThan"); instance = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/OLTest.java b/src/test/java/targetast/OLTest.java index 8afcc669..44f72411 100644 --- a/src/test/java/targetast/OLTest.java +++ b/src/test/java/targetast/OLTest.java @@ -16,7 +16,7 @@ public class OLTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("OL.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "OL.jav"); classToTest = classFiles.get("OL"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); classToTest1 = classFiles.get("OLMain"); diff --git a/src/test/java/targetast/PostIncTest.java b/src/test/java/targetast/PostIncTest.java index d0d0a02d..98fafc8e 100644 --- a/src/test/java/targetast/PostIncTest.java +++ b/src/test/java/targetast/PostIncTest.java @@ -14,7 +14,7 @@ public class PostIncTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("PostIncDec.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "PostIncDec.jav"); classToTest = classFiles.get("PostIncDec"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/PreIncTest.java b/src/test/java/targetast/PreIncTest.java index d946b0e3..8c23ea82 100644 --- a/src/test/java/targetast/PreIncTest.java +++ b/src/test/java/targetast/PreIncTest.java @@ -14,7 +14,7 @@ public class PreIncTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("PreInc.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "PreInc.jav"); classToTest = classFiles.get("PreInc"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/PutTest.java b/src/test/java/targetast/PutTest.java index 66600337..8a4b8811 100644 --- a/src/test/java/targetast/PutTest.java +++ b/src/test/java/targetast/PutTest.java @@ -16,7 +16,7 @@ public class PutTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("Put.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "Put.jav"); classToTest = classFiles.get("Put"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/TestCodegen.java b/src/test/java/targetast/TestCodegen.java index 73d3dfdf..3d949f5d 100644 --- a/src/test/java/targetast/TestCodegen.java +++ b/src/test/java/targetast/TestCodegen.java @@ -20,6 +20,8 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -27,11 +29,11 @@ import java.util.stream.Collectors; public class TestCodegen { + static final Path outputPath = Path.of(System.getProperty("user.dir"), "src/test/resources/target/"); 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); + Files.createDirectories(outputPath); + Files.write(outputPath.resolve(name + ".class"), code); } public static Class generateClass(TargetClass clazz, IByteArrayClassLoader classLoader) throws IOException { @@ -41,25 +43,29 @@ public class TestCodegen { return classLoader.loadClass(code); } - public static Map> generateClassFiles(String filename, IByteArrayClassLoader classLoader) throws IOException, ClassNotFoundException { - var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/", filename).toFile(); - var compiler = new JavaTXCompiler(List.of(file), List.of(file.getParentFile())); + public static Map> generateClassFiles(IByteArrayClassLoader classLoader, String... files) throws IOException, ClassNotFoundException { + var path = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/"); + var filenames = Arrays.stream(files).map(filename -> Path.of(path.toString(), filename).toFile()).toList(); + var compiler = new JavaTXCompiler(filenames, List.of(path.toFile(), outputPath.toFile())); var resultSet = compiler.typeInference(); - var sourceFile = compiler.sourceFiles.get(file); - var converter = new ASTToTargetAST(resultSet, sourceFile, classLoader); - var classes = compiler.sourceFiles.get(file).getClasses(); + var result = new HashMap>(); + for (var file : filenames) { + var sourceFile = compiler.sourceFiles.get(file); + var converter = new ASTToTargetAST(resultSet, sourceFile, classLoader); + var classes = compiler.sourceFiles.get(file).getClasses(); - var result = classes.stream().map(cli -> { - try { - return generateClass(converter.convert(cli), classLoader); - } catch (IOException exception) { - throw new RuntimeException(exception); + result.putAll(classes.stream().map(cli -> { + try { + return generateClass(converter.convert(cli), classLoader); + } catch (IOException exception) { + throw new RuntimeException(exception); + } + }).collect(Collectors.toMap(Class::getName, Function.identity()))); + + for (var entry : converter.auxiliaries.entrySet()) { + writeClassFile(entry.getKey(), entry.getValue()); } - }).collect(Collectors.toMap(Class::getName, Function.identity())); - - for (var entry : converter.auxiliaries.entrySet()) { - writeClassFile(entry.getKey(), entry.getValue()); } return result; diff --git a/src/test/java/targetast/TestComplete.java b/src/test/java/targetast/TestComplete.java index b1773e15..1b39618d 100644 --- a/src/test/java/targetast/TestComplete.java +++ b/src/test/java/targetast/TestComplete.java @@ -16,7 +16,7 @@ import static targetast.TestCodegen.generateClassFiles; public class TestComplete { @Test public void applyLambdaTest() throws Exception { - var classFiles = generateClassFiles("applyLambda.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "applyLambda.jav"); var applyLambda = classFiles.get("applyLambda"); var instance = applyLambda.getDeclaredConstructor().newInstance(); var m = applyLambda.getDeclaredMethod("m"); @@ -27,7 +27,7 @@ public class TestComplete { @Test public void binaryTest() throws Exception { - var classFiles = generateClassFiles("BinaryInMeth.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "BinaryInMeth.jav"); var binaryInMeth = classFiles.get("BinaryInMeth"); var instance = binaryInMeth.getDeclaredConstructor().newInstance(); @@ -40,13 +40,13 @@ public class TestComplete { @Test public void classGenLamTest() throws Exception { - var classFiles = generateClassFiles("ClassGenLam.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "ClassGenLam.jav"); classFiles.get("ClassGenLam").getDeclaredConstructor().newInstance(); } @Test public void facTest() throws Exception { - var classFiles = generateClassFiles("Fac.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Fac.jav"); var fac = classFiles.get("Fac"); var instance = fac.getDeclaredConstructor().newInstance(); @@ -56,7 +56,7 @@ public class TestComplete { @Test public void facultyTest() throws Exception { - var classFiles = generateClassFiles("Faculty.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Faculty.jav"); var fac = classFiles.get("Faculty"); var constructor = fac.getDeclaredConstructor(); @@ -69,7 +69,7 @@ public class TestComplete { @Test public void fieldTest() throws Exception { - var classFiles = generateClassFiles("Field.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Field.jav"); var field = classFiles.get("Field"); var instance = field.getDeclaredConstructor().newInstance(); assertEquals(1, field.getFields().length); @@ -77,7 +77,7 @@ public class TestComplete { @Test public void fieldTph2Test() throws Exception { - var classFiles = generateClassFiles("FieldTph2.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "FieldTph2.jav"); var fieldtph2 = classFiles.get("FieldTph2"); var instance = fieldtph2.getDeclaredConstructor().newInstance(); @@ -91,7 +91,7 @@ public class TestComplete { @Test public void fieldTphConsMethTest() throws Exception { - var classFiles = generateClassFiles("FieldTphConsMeth.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "FieldTphConsMeth.jav"); var fieldTphConsMeth = classFiles.get("FieldTphConsMeth"); var ctor = fieldTphConsMeth.getDeclaredConstructor(Object.class); @@ -105,7 +105,7 @@ public class TestComplete { @Test public void fieldTphMMethTest() throws Exception { - var classFiles = generateClassFiles("FieldTphMMeth.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "FieldTphMMeth.jav"); var fieldTphMMeth = classFiles.get("FieldTphMMeth"); var ctor = fieldTphMMeth.getDeclaredConstructor(Object.class, Object.class, Boolean.class); @@ -122,37 +122,37 @@ public class TestComplete { @Test public void genTest() throws Exception { - var classFiles = generateClassFiles("Gen.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Gen.jav"); } @Test public void idTest() throws Exception { - var classFiles = generateClassFiles("Id.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Id.jav"); var instance = classFiles.get("Id").getDeclaredConstructor().newInstance(); } @Test public void infTest() throws Exception { - var classFiles = generateClassFiles("Inf.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Inf.jav"); var instance = classFiles.get("Inf").getDeclaredConstructor().newInstance(); // TODO check generics } @Test public void kompTphTest() throws Exception { - var classFiles = generateClassFiles("KompTph.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "KompTph.jav"); var instance = classFiles.get("KompTph").getDeclaredConstructor().newInstance(); } @Test public void lambdaCaptureTest() throws Exception { - var classFiles = generateClassFiles("LambdaCapture.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "LambdaCapture.jav"); var instance = classFiles.get("LambdaCapture").getDeclaredConstructor().newInstance(); } @Test public void lambdaTest() throws Exception { - var classFiles = generateClassFiles("Lambda.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Lambda.jav"); var classToTest = classFiles.get("Lambda"); var instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); @@ -167,21 +167,21 @@ public class TestComplete { @Test public void mathStrucInteger() throws Exception { - var classFiles = generateClassFiles("mathStrucInteger.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "mathStrucInteger.jav"); var mathStrucInteger = classFiles.get("mathStrucInteger"); mathStrucInteger.getDeclaredConstructor(Integer.class).newInstance(10); } @Test public void mathStruc() throws Exception { - var classFiles = generateClassFiles("mathStruc.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "mathStruc.jav"); var mathStruc = classFiles.get("mathStruc"); mathStruc.getDeclaredConstructor(Object.class).newInstance("A"); } @Test public void matrixOpTest() throws Exception { - var classFiles = generateClassFiles("MatrixOP.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "MatrixOP.jav"); var matrixOP = classFiles.get("MatrixOP"); Vector> vv = new Vector<>(); @@ -234,7 +234,7 @@ public class TestComplete { @Test public void matrixTest() throws Exception { - var classFiles = generateClassFiles("Matrix.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Matrix.jav"); var matrix = classFiles.get("Matrix"); Vector> vv = new Vector<>(); @@ -281,19 +281,19 @@ public class TestComplete { @Test public void mergeTest() throws Exception { - var classFiles = generateClassFiles("Merge.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Merge.jav"); var instance = classFiles.get("Merge").getDeclaredConstructor().newInstance(); } @Test public void overloadingSortingTest() throws Exception { - var classFiles = generateClassFiles("Sorting.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Sorting.jav"); var instance = classFiles.get("Sorting").getDeclaredConstructor().newInstance(); } @Test public void overloadingTest() throws Exception { - var classFiles = generateClassFiles("Overloading.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Overloading.jav"); var overloading = classFiles.get("Overloading"); var overloading2 = classFiles.get("Overloading2"); var instance1 = overloading.getDeclaredConstructor().newInstance(); @@ -307,7 +307,7 @@ public class TestComplete { @Test public void plusTest() throws Exception { - var classFiles = generateClassFiles("Plus.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Plus.jav"); var plus = classFiles.get("Plus"); var instance = plus.getDeclaredConstructor().newInstance(); @@ -320,7 +320,7 @@ public class TestComplete { @Test public void relOpsTest() throws Exception { - var classFiles = generateClassFiles("RelOps.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "RelOps.jav"); var relOps = classFiles.get("RelOps"); var instance = relOps.getDeclaredConstructor().newInstance(); @@ -330,26 +330,26 @@ public class TestComplete { @Test public void simpleCyclesTest() throws Exception { - var classFiles = generateClassFiles("SimpleCycle.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SimpleCycle.jav"); var instance = classFiles.get("SimpleCycle").getDeclaredConstructor().newInstance(); } @Test public void subMatTest() throws Exception { - var classFiles = generateClassFiles("SubMatrix.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SubMatrix.jav"); var instance = classFiles.get("SubMatrix").getDeclaredConstructor().newInstance(); } @Test public void tphTest() throws Exception { - var classFiles = generateClassFiles("Tph.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph.jav"); var tph = classFiles.get("Tph"); var instance = tph.getDeclaredConstructor().newInstance(); } @Test public void tph2Test() throws Exception { - var classFiles = generateClassFiles("Tph2.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph2.jav"); var tph2 = classFiles.get("Tph2"); var instance = tph2.getDeclaredConstructor().newInstance(); @@ -376,7 +376,7 @@ public class TestComplete { @Test public void tph3Test() throws Exception { - var classFiles = generateClassFiles("Tph3.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph3.jav"); var tph3 = classFiles.get("Tph3"); var instance = tph3.getDeclaredConstructor().newInstance(); var m1 = tph3.getDeclaredMethod("m1", Object.class, Object.class); @@ -402,7 +402,7 @@ public class TestComplete { @Test public void tph4Test() throws Exception { - var classFiles = generateClassFiles("Tph4.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph4.jav"); var tph4 = classFiles.get("Tph4"); var instance = tph4.getDeclaredConstructor().newInstance(); var m = tph4.getDeclaredMethod("m", Object.class, Object.class); @@ -433,7 +433,7 @@ public class TestComplete { @Test public void tph5Test() throws Exception { - var classFiles = generateClassFiles("Tph5.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph5.jav"); var tph5 = classFiles.get("Tph5"); var instance = tph5.getDeclaredConstructor().newInstance(); var m = tph5.getDeclaredMethod("m", Object.class, Object.class); @@ -461,7 +461,7 @@ public class TestComplete { @Test public void tph6Test() throws Exception { - var classFiles = generateClassFiles("Tph6.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Tph6.jav"); var tph5 = classFiles.get("Tph6"); var instance = tph5.getDeclaredConstructor().newInstance(); var m = tph5.getDeclaredMethod("m", Object.class, Object.class); @@ -490,7 +490,7 @@ public class TestComplete { @Test public void Tph7Test() throws Exception { - var classFiles = TestCodegen.generateClassFiles("Tph7.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "Tph7.jav"); var classToTest = classFiles.get("Tph7"); var instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); @@ -552,56 +552,56 @@ public class TestComplete { @Test public void typedIdTest() throws Exception { - var classFiles = generateClassFiles("TypedID.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "TypedID.jav"); var instance = classFiles.get("TypedID").getDeclaredConstructor().newInstance(); } @Test public void vectorAddTest() throws Exception { - var classFiles = generateClassFiles("VectorAdd.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "VectorAdd.jav"); var instance = classFiles.get("VectorAdd").getDeclaredConstructor().newInstance(); } @Test public void vectorSuperTest() throws Exception { - var classFiles = generateClassFiles("VectorSuper.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "VectorSuper.jav"); var instance = classFiles.get("VectorSuper").getDeclaredConstructor().newInstance(); } @Test public void yTest() throws Exception { - var classFiles = generateClassFiles("Y.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Y.jav"); var instance = classFiles.get("Y").getDeclaredConstructor().newInstance(); } @Test @Ignore("This one isn't working") public void boxTest() throws Exception { - var classFiles = generateClassFiles("Box.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Box.jav"); var instance = classFiles.get("Box_Main").getDeclaredConstructor().newInstance(); } @Test public void cycleTest() throws Exception { - var classFiles = generateClassFiles("Cycle.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Cycle.jav"); var instance = classFiles.get("Cycle").getDeclaredConstructor().newInstance(); } @Test public void olFunTest() throws Exception { - var classFiles = generateClassFiles("OLFun.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "OLFun.jav"); var instance = classFiles.get("OLFun").getDeclaredConstructor().newInstance(); } @Test public void olFun2Test() throws Exception { - var classFiles = generateClassFiles("OLFun2.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "OLFun2.jav"); var instance = classFiles.get("OLFun2").getDeclaredConstructor().newInstance(); } @Test public void pairTest() throws Exception { - var classFiles = generateClassFiles("Pair.jav", new ByteArrayClassLoader()); + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Pair.jav"); var instance = classFiles.get("Pair").getDeclaredConstructor().newInstance(); } } diff --git a/src/test/java/targetast/TestGenerics.java b/src/test/java/targetast/TestGenerics.java index 1131e076..2679f6d4 100644 --- a/src/test/java/targetast/TestGenerics.java +++ b/src/test/java/targetast/TestGenerics.java @@ -361,4 +361,10 @@ public class TestGenerics { public void testVoidMeth() throws Exception { var result = computeGenerics("TestVoidMeth.jav"); } + + @Test + public void testAssign() throws Exception { + // TODO Check generics + var result = computeGenerics("TestAssign.jav"); + } } diff --git a/src/test/java/targetast/TphTest.java b/src/test/java/targetast/TphTest.java index b902b155..3fd6fa2c 100644 --- a/src/test/java/targetast/TphTest.java +++ b/src/test/java/targetast/TphTest.java @@ -15,7 +15,7 @@ public class TphTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("Tph.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "Tph.jav"); classToTest = classFiles.get("Tph"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } diff --git a/src/test/java/targetast/WhileTest.java b/src/test/java/targetast/WhileTest.java index 7b496f15..50371d19 100644 --- a/src/test/java/targetast/WhileTest.java +++ b/src/test/java/targetast/WhileTest.java @@ -14,7 +14,7 @@ public class WhileTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - var classFiles = TestCodegen.generateClassFiles("While.jav", new ByteArrayClassLoader()); + var classFiles = TestCodegen.generateClassFiles(new ByteArrayClassLoader(), "While.jav"); classToTest = classFiles.get("While"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } From cf2d612870eed3e284e022fc8dfa20501cf8da45 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Fri, 24 Mar 2023 14:51:57 +0100 Subject: [PATCH 04/16] Fix TX Signature by adding additional prameters to every method and constructor --- .../de/dhbwstuttgart/bytecode/Codegen.java | 22 +++++---- .../target/generate/ASTToTargetAST.java | 47 +++++++++++-------- .../target/tree/TargetClass.java | 4 +- .../target/tree/TargetConstructor.java | 5 +- .../target/tree/TargetMethod.java | 10 ++-- 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index efa8def2..3d94be1c 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -717,10 +717,14 @@ public class Codegen { .toList()); impl = new TargetMethod( - 0, name, Set.of(), Set.of(), - parameters, - lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType(), - lambda.block() + 0, name, + lambda.block(), + new TargetMethod.Signature( + Set.of(), + parameters, + lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType() + ), + null ); generateMethod(impl); lambdas.put(lambda, impl); @@ -758,7 +762,7 @@ public class Codegen { mv.visitInvokeDynamicInsn("apply", descriptor, bootstrap, Type.getType(desugared), handle, - Type.getType(TargetMethod.getDescriptor(impl.returnType(), lambda.params().stream().map(MethodParameter::type).toArray(TargetType[]::new))) + Type.getType(TargetMethod.getDescriptor(impl.signature().returnType(), lambda.params().stream().map(MethodParameter::type).toArray(TargetType[]::new))) ); } @@ -1022,17 +1026,17 @@ public class Codegen { private void generateMethod(TargetMethod method) { // TODO The older codegen has set ACC_PUBLIC for all methods, good for testing but bad for everything else MethodVisitor mv = cw.visitMethod(method.access() | ACC_PUBLIC, method.name(), method.getDescriptor(), method.getSignature(), null); - if (!method.txGenerics().isEmpty()) + if (method.txSignature() != null) mv.visitAttribute(new JavaTXSignatureAttribute(method.getTXSignature())); System.out.println(method.getDescriptor()); System.out.println(method.getSignature()); mv.visitCode(); - var state = new State(method.returnType(), mv, method.isStatic() ? 0 : 1); - for (var param: method.parameters()) + var state = new State(method.signature().returnType(), mv, method.isStatic() ? 0 : 1); + for (var param: method.signature().parameters()) state.createVariable(param.name(), param.type()); generate(state, method.block()); - if (method.returnType() == null) + if (method.signature().returnType() == null) mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 81cb295e..b4f6978b 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -933,22 +933,22 @@ public class ASTToTargetAST { return true; } - Set convert(Set> result) { + Set convert(Set> result, Map equality) { return result.stream().map(p -> { if (p instanceof PairTPHsmallerTPH pair) { - return new TargetGeneric(pair.left.getName(), convert(pair.right)); + return new TargetGeneric(pair.left.getName(), convert(pair.right, equality)); } else if (p instanceof PairTPHequalRefTypeOrWildcardType pair) { - return new TargetGeneric(pair.left.getName(), convert(pair.right)); + return new TargetGeneric(pair.left.getName(), convert(pair.right, equality)); } else { throw new IllegalArgumentException(); } }).collect(Collectors.toSet()); } - public List convert(GenericTypeVar typeVar) { + public List convert(GenericTypeVar typeVar, Map equality) { var ret = new ArrayList(); for (var bound : typeVar.getBounds()) { - ret.add(new TargetGeneric(typeVar.getName(), convert(bound))); + ret.add(new TargetGeneric(typeVar.getName(), convert(bound, equality))); } return ret; } @@ -965,13 +965,13 @@ public class ASTToTargetAST { while (genericsIter.hasNext()) { var next = genericsIter.next(); userDefinedGenerics.add(next); - javaGenerics.addAll(convert(next)); + javaGenerics.addAll(convert(next, sigma.equality)); } } else { // Generate generics only if there are no user defined ones var generics = sigma.generics(input); - javaGenerics = convert(generics.javaGenerics()); - txGenerics = convert(generics.txGenerics()); + javaGenerics = convert(generics.javaGenerics(), sigma.equality); + txGenerics = convert(generics.txGenerics(), sigma.txEquality); } TargetBlock fieldInitializer = null; @@ -988,9 +988,9 @@ public class ASTToTargetAST { ); } - private List convert(ParameterList input) { + private List convert(ParameterList input, Map equality) { return input.getFormalparalist().stream() - .map(param -> new MethodParameter(convert(param.getType()), param.getName())).toList(); + .map(param -> new MethodParameter(convert(param.getType(), equality), param.getName())).toList(); } private boolean hasGeneric(Set generics, GenericRefType type) { @@ -998,7 +998,7 @@ public class ASTToTargetAST { } private Set collectMethodGenerics(Set> generics, Map equality, Method input) { - var convertedGenerics = new HashSet<>(convert(generics)); + var convertedGenerics = new HashSet<>(convert(generics, equality)); outer: for (GenericTypeVar typeVar : input.getGenerics()) { for (var classGeneric : currentClass.getGenerics()) { @@ -1006,7 +1006,7 @@ public class ASTToTargetAST { continue outer; } } - convertedGenerics.addAll(convert(typeVar)); + convertedGenerics.addAll(convert(typeVar, equality)); } /*var returnType = sigma.getType(input.getReturnType(), equality); if ((returnType instanceof GenericRefType refType) && !hasGeneric(convertedGenerics, refType)) { @@ -1030,12 +1030,13 @@ public class ASTToTargetAST { for (var s : all) { sigma = s; var generics = sigma.generics(currentClass, input); - List params = convert(input.getParameterList()); + List params = convert(input.getParameterList(), sigma.equality); if (parameterSet.stream().noneMatch(p -> p.equals(params))) { + List txParams = convert(input.getParameterList(), sigma.txEquality); var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input); var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, input); - result.add(new TargetConstructor(input.modifier, javaGenerics, txGenerics, params, convert(input.block), fieldInitializer)); + result.add(new TargetConstructor(input.modifier, javaGenerics, txGenerics, params, txParams, convert(input.block), fieldInitializer)); parameterSet.add(params); } } @@ -1051,16 +1052,18 @@ public class ASTToTargetAST { for (var s : all) { sigma = s; var generics = sigma.generics(currentClass, input); - List params = convert(input.getParameterList()); + List params = convert(input.getParameterList(), sigma.equality); if (parameterSet.stream().noneMatch(p -> p.equals(params))) { + List txParams = convert(input.getParameterList(), sigma.txEquality); + var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input); var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, input); result.add(new TargetMethod( input.modifier, - input.name, javaGenerics, txGenerics, params, - convert(input.getReturnType()), - convert(input.block) + input.name, convert(input.block), + new TargetMethod.Signature(javaGenerics, params, convert(input.getReturnType(), sigma.equality)), + new TargetMethod.Signature(txGenerics, txParams, convert(input.getReturnType(), sigma.txEquality)) )); parameterSet.add(params); } @@ -1092,7 +1095,11 @@ public class ASTToTargetAST { public Map auxiliaries = new HashMap<>(); - protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input) { + protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input) { + return convert(input, sigma.equality); + } + + protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input, Map equality) { return input.acceptTV(new TypeVisitor<>() { @Override public TargetType visit(RefType refType) { @@ -1126,7 +1133,7 @@ public class ASTToTargetAST { @Override public TargetType visit(TypePlaceholder typePlaceholder) { - return sigma.getTargetType(typePlaceholder, sigma.equality); + return sigma.getTargetType(typePlaceholder, equality); } @Override diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java index 781df996..3118a1d8 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java @@ -25,7 +25,7 @@ public record TargetClass(int modifiers, String qualifiedName, TargetType superT } public void addMethod(int access, String name, Set generics, List parameterTypes, TargetType returnType, TargetBlock block) { - this.methods.add(new TargetMethod(access, name, generics, Set.of(), parameterTypes, returnType, block)); + this.methods.add(new TargetMethod(access, name, block, new TargetMethod.Signature(generics, parameterTypes, returnType), null)); } public void addMethod(int access, String name, List parameterTypes, TargetType returnType, TargetBlock block) { @@ -33,7 +33,7 @@ public record TargetClass(int modifiers, String qualifiedName, TargetType superT } public void addConstructor(int access, Set generics, List paramterTypes, TargetBlock block) { - this.constructors.add(new TargetConstructor(access, generics, Set.of(), paramterTypes, block, null)); + this.constructors.add(new TargetConstructor(access, generics, Set.of(), paramterTypes, List.of(), block, null)); } public void addConstructor(int access, List paramterTypes, TargetBlock block) { diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java index 7d3aa389..1da5dc93 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java @@ -1,12 +1,13 @@ package de.dhbwstuttgart.target.tree; +import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.target.tree.expression.TargetBlock; import de.dhbwstuttgart.target.tree.type.TargetType; import java.util.List; import java.util.Set; -public record TargetConstructor(int access, Set generics, Set txGenerics, List parameters, TargetBlock block, TargetBlock fieldInitializer) { +public record TargetConstructor(int access, Set generics, Set txGenerics, List parameters, List txParameters, TargetBlock block, TargetBlock fieldInitializer) { public String getDescriptor() { return TargetMethod.getDescriptor(null, parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); @@ -17,7 +18,7 @@ public record TargetConstructor(int access, Set generics, Set generics, Set txGenerics, List parameters, TargetType returnType, TargetBlock block) { +public record TargetMethod(int access, String name, TargetBlock block, Signature signature, Signature txSignature) { + public record Signature(Set generics, List parameters, TargetType returnType) { } + public static String getDescriptor(TargetType returnType, TargetType... parameters) { String ret = "("; for (var parameterType : parameters) { @@ -35,15 +37,15 @@ public record TargetMethod(int access, String name, Set generics, } public String getDescriptor() { - return getDescriptor(returnType, parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); + return getDescriptor(signature.returnType, signature.parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); } public String getSignature() { - return getSignature(generics, parameters, returnType); + return getSignature(signature.generics, signature.parameters, signature.returnType); } public String getTXSignature() { - return getSignature(txGenerics, parameters, returnType); + return getSignature(txSignature.generics, txSignature.parameters, txSignature.returnType); } public boolean isStatic() { From b9f19cc10597cf8f6f4fe31caccb8f5594d33449 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Fri, 24 Mar 2023 15:14:17 +0100 Subject: [PATCH 05/16] Write tx signature to constant pool --- .../java/de/dhbwstuttgart/bytecode/Codegen.java | 13 +++++++------ .../bytecode/JavaTXSignatureAttribute.java | 11 ++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index 3d94be1c..8c60ac42 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -999,8 +999,8 @@ public class Codegen { private void generateConstructor(TargetConstructor constructor) { MethodVisitor mv = cw.visitMethod(constructor.access() | ACC_PUBLIC, "", constructor.getDescriptor(), constructor.getSignature(), null); - if (!constructor.txGenerics().isEmpty()) - mv.visitAttribute(new JavaTXSignatureAttribute(constructor.getTXSignature())); + if (constructor.txGenerics() != null) + mv.visitAttribute(new JavaTXSignatureAttribute(cw.newConst(constructor.getTXSignature()))); mv.visitCode(); var state = new State(null, mv, 1); @@ -1026,8 +1026,9 @@ public class Codegen { private void generateMethod(TargetMethod method) { // TODO The older codegen has set ACC_PUBLIC for all methods, good for testing but bad for everything else MethodVisitor mv = cw.visitMethod(method.access() | ACC_PUBLIC, method.name(), method.getDescriptor(), method.getSignature(), null); - if (method.txSignature() != null) - mv.visitAttribute(new JavaTXSignatureAttribute(method.getTXSignature())); + if (method.txSignature() != null) { + mv.visitAttribute(new JavaTXSignatureAttribute(cw.newConst(method.getTXSignature()))); + } System.out.println(method.getDescriptor()); System.out.println(method.getSignature()); @@ -1058,8 +1059,8 @@ public class Codegen { generateSignature(clazz, clazz.generics()), clazz.superType() != null ? clazz.superType().getInternalName(): "java/lang/Object", clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new) ); - if (!clazz.txGenerics().isEmpty()) - cw.visitAttribute(new JavaTXSignatureAttribute(generateSignature(clazz, clazz.txGenerics()))); + if (clazz.txGenerics() != null) + cw.visitAttribute(new JavaTXSignatureAttribute(cw.newConst(generateSignature(clazz, clazz.txGenerics())))); clazz.fields().forEach(this::generateField); clazz.constructors().forEach(this::generateConstructor); diff --git a/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java b/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java index cc9d1487..bd5f6028 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/JavaTXSignatureAttribute.java @@ -2,13 +2,10 @@ package de.dhbwstuttgart.bytecode; import org.objectweb.asm.*; -import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; - public class JavaTXSignatureAttribute extends Attribute { - final String signature; + final int signature; - protected JavaTXSignatureAttribute(String signature) { + protected JavaTXSignatureAttribute(int signature) { super("JavaTXSignature"); this.signature = signature; } @@ -17,13 +14,13 @@ public class JavaTXSignatureAttribute extends Attribute { protected Attribute read(ClassReader classReader, int offset, int length, char[] charBuffer, int codeAttributeOffset, Label[] labels) { var data = new byte[length]; System.arraycopy(classReader.b, offset, data, 0, length); - return new JavaTXSignatureAttribute(new String(data, StandardCharsets.UTF_8)); + return new JavaTXSignatureAttribute(data[0] << 8 | data[1]); } @Override protected ByteVector write(ClassWriter classWriter, byte[] code, int codeLength, int maxStack, int maxLocals) { var data = new ByteVector(); - data.putUTF8(this.signature); + data.putShort(this.signature); return data; } } From 3963baa9ced4803100d49ddfd709f631a083d8b2 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 28 Mar 2023 11:46:30 +0200 Subject: [PATCH 06/16] Fix equality --- .../target/generate/ASTToTargetAST.java | 41 +++++++++++-------- src/test/java/targetast/TestGenerics.java | 3 +- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index b4f6978b..14dd4afe 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 2679f6d4..fdeaab9b 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)); From 1ad5540500b7f40d2c90829e9257c2f86dcfd0d3 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 28 Mar 2023 13:28:05 +0200 Subject: [PATCH 07/16] Fix wrong class generics being used --- .../java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java | 3 ++- 1 file changed, 2 insertions(+), 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 14dd4afe..3fb42ee1 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -537,6 +537,7 @@ public class ASTToTargetAST { var classGenerics = generics(owner); var genericsOfClass = classGenerics.javaGenerics(); + var txGenericsOfClass = classGenerics.txGenerics(); var simplifiedConstraints = new HashSet<>(this.simplifiedConstraints); HashSet txTypeVariables = new HashSet<>(); @@ -545,7 +546,7 @@ public class ASTToTargetAST { HashSet javaTypeVariablesOfClass = new HashSet<>(); methodFindTypeVariables(method, genericsOfClass, javaTypeVariablesOfClass, javaTypeVariables, equality); - methodFindTypeVariables(method, genericsOfClass, txTypeVariablesOfClass, txTypeVariables, txEquality); + methodFindTypeVariables(method, txGenericsOfClass, txTypeVariablesOfClass, txTypeVariables, txEquality); methodFindConstraints(owner, method, simplifiedConstraints, javaTypeVariables, classGenerics.userDefinedGenerics, javaTypeVariablesOfClass, javaResult, equality); methodFindConstraints(owner, method, simplifiedConstraints, txTypeVariables, classGenerics.userDefinedGenerics, txTypeVariablesOfClass, txResult, txEquality); From f7b60214fa8831647f363dbcc25b776e175cb5e0 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 28 Mar 2023 16:40:06 +0200 Subject: [PATCH 08/16] Change tests --- .../target/generate/ASTToTargetAST.java | 11 +++++---- .../target/generate/GenericsResult.java | 4 ++-- src/test/java/targetast/TestGenerics.java | 24 ++++++++++--------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 3fb42ee1..b28bfb41 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -647,20 +647,23 @@ public class ASTToTargetAST { } void equalizeTypeVariables(Set> input, Map equality) { + System.out.println(input); for (var pair : new HashSet<>(input)) { if (pair instanceof PairTPHsmallerTPH ptph) { + System.out.println(pair + " " + ptph.left.getVariance() + " " + ptph.right.getVariance()); if (ptph.left.getVariance() == 1 && ptph.right.getVariance() == -1) { 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)) { - input.remove(pair2); - addToPairs(input, new PairTPHsmallerTPH(ptph2.left, ptph.right)); + for (var pair2 : new HashSet<>(simplifiedConstraints)) { + if (pair2.right.equals(ptph.left)) { + simplifiedConstraints.remove(pair2); + simplifiedConstraints.add(new PairTPHsmallerTPH(pair2.left, ptph.right)); } } } } } + System.out.println(input); } void findTphs(RefTypeOrTPHOrWildcardOrGeneric type, Set tphs, Map equality) { diff --git a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java index e350707e..5f0008c0 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java @@ -18,14 +18,14 @@ public class GenericsResult { public Set> get(ClassOrInterface 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? 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) { diff --git a/src/test/java/targetast/TestGenerics.java b/src/test/java/targetast/TestGenerics.java index fdeaab9b..23c8b152 100644 --- a/src/test/java/targetast/TestGenerics.java +++ b/src/test/java/targetast/TestGenerics.java @@ -127,26 +127,22 @@ public class TestGenerics { assertEquals(R, RChain); var O = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); - var OChain = new BoundsList(new Bound(true, TypePlaceholder.of("AB")), new Bound(true, ASTToTargetAST.OBJECT)); - assertEquals(O, OChain); var AB = generics.getBounds(id.getReturnType(), result.clazz, id); + assertEquals(O, AB); assertEquals(AB, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); var S = generics.getBounds(setA.getParameterList().getParameterAt(0).getType(), result.clazz, setA); - var SChain = new BoundsList(new Bound(true, TypePlaceholder.of("R")), new Bound(false, ASTToTargetAST.OBJECT)); - assertEquals(S, SChain); + assertEquals(S, RChain); assertEquals(generics.getBounds(setA.getReturnType(), result.clazz, setA), RChain); var X = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); var Y = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); var XChain = new BoundsList(new Bound(true, ASTToTargetAST.OBJECT)); - var YChain = new BoundsList(new Bound(true, TypePlaceholder.of("Y")), new Bound(true, ASTToTargetAST.OBJECT)); assertEquals(X, XChain); - assertEquals(Y, YChain); + assertEquals(Y, X); } @Test - @Ignore("Not implemented") public void testLocalVarLambda() throws Exception { var result = computeGenerics("TestLocalVarLambda.jav"); // TODO Generics of lambdas @@ -221,9 +217,10 @@ public class TestGenerics { var Q = generics.getBounds(anyMethod.getReturnType(), result.clazz, anyMethod); assertEquals(Q, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + System.out.println(otherMethod.getReturnType()); var DYX2 = generics.getBounds(otherMethod.getReturnType(), result.clazz, otherMethod); assertEquals(DYX, DYX2); - assertEquals(DYX2, DYXChain); + assertEquals(DYX, generics.getBounds(otherMethod.getReturnType(), result.clazz, otherMethod)); } @Test @@ -247,7 +244,7 @@ public class TestGenerics { var AA = generics.getBounds(m.getReturnType(), result.clazz, m); var AC = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); assertEquals(AA, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); - assertEquals(AC, new BoundsList(new Bound(true, TypePlaceholder.of("AD")), new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(AC, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); var AH = generics.getBounds(m2.getReturnType(), result.clazz, m2); var AL = generics.getBounds(m2.getParameterList().getParameterAt(0).getType(), result.clazz, m2); @@ -298,9 +295,9 @@ public class TestGenerics { } @Test - @Ignore("main changes in between runs") public void testTwoArgs2() throws Exception { var result = computeGenerics("TestTwoArgs2.jav"); + // TODO Test generics } @Test @@ -327,6 +324,7 @@ public class TestGenerics { public void testVector() throws Exception { var result = computeGenerics("TestVector.jav"); var m = result.findMethod("m"); + var id = result.findMethod("id"); var generics = result.genericsResults.get(0); var par1 = generics.resolve(m.getParameterList().getParameterAt(0).getType()); @@ -334,8 +332,12 @@ public class TestGenerics { var S = generics.getBounds(((RefType) par1).getParaList().get(0), result.clazz, m); var ACM = generics.getBounds(((RefType) par2).getParaList().get(0), result.clazz, m); - assertEquals(S, new BoundsList(new Bound(true, TypePlaceholder.of("V")), new Bound(true, TypePlaceholder.of("ACM")), new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(S, new BoundsList(new Bound(true, TypePlaceholder.of("V")), new Bound(true, ASTToTargetAST.OBJECT))); assertEquals(ACM, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + + var Y = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); + assertEquals(Y, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(Y, generics.getBounds(id.getReturnType(), result.clazz, id)); } @Test From e55b9938dce7a6f352b7bbcaece91f4d339f0e12 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 4 Apr 2023 10:30:38 +0200 Subject: [PATCH 09/16] modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java Returntyp: Equaldot durch SMALLERDOT ersetzt --- .../de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 198b0f4c..7108975b 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -610,8 +610,8 @@ public class TYPEStmt implements StatementVisitor{ //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ENDE - methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT)); - extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT)); + methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT)); + extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT)); Set parameterContraints = generateParameterConstraints(forMethod, assumption, info, resolver); From aa9782edd7a3f2b431a44f815d1a3e7f9174fc1f Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 4 Apr 2023 13:09:05 +0200 Subject: [PATCH 10/16] Fix test case --- src/test/java/targetast/TestGenerics.java | 39 +++++++++++------------ 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/test/java/targetast/TestGenerics.java b/src/test/java/targetast/TestGenerics.java index 23c8b152..f6308861 100644 --- a/src/test/java/targetast/TestGenerics.java +++ b/src/test/java/targetast/TestGenerics.java @@ -158,33 +158,30 @@ public class TestGenerics { var generics = result.genericsResults.get(0); assertEquals(1, generics.get(result.clazz).size()); - assertEquals(3, generics.get(id).size()); + assertEquals(2, generics.get(id).size()); assertEquals(2, generics.get(m).size()); assertEquals(3, generics.get(main).size()); - var M = generics.getBounds(a.getType(), result.clazz); - assertEquals(M, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT))); + var N = generics.getBounds(a.getType(), result.clazz); + assertEquals(N, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT))); - var O = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); - var OChain = new BoundsList(new Bound(true, TypePlaceholder.of("P")), new Bound(true, ASTToTargetAST.OBJECT)); - assertEquals(O, OChain); - var AK = generics.getBounds(id.getReturnType(), result.clazz, id); - var AKChain = new BoundsList(new Bound(true, ASTToTargetAST.OBJECT)); - assertEquals(AK, AKChain); + var P = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); + var O = generics.getBounds(id.getReturnType(), result.clazz, id); + assertEquals(P, O); + assertEquals(O, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); - // TODO Shouldn't AK and AK2 be the same and on the class? - var AK2 = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); - var Y = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); - assertEquals(AK2, AKChain); - assertEquals(Y, AKChain); + assertEquals(generics.resolve(m.getParameterList().getParameterAt(0).getType()), generics.resolve(m.getReturnType())); + var Y = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); + var AA = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); + assertEquals(Y, AA); + assertEquals(AA, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); - var AF = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main); - var AG= generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main); - var AK3 = generics.getBounds(main.getReturnType(), result.clazz, main); - var AFChain = new BoundsList(new Bound(true, TypePlaceholder.of("AG")), new Bound(true, ASTToTargetAST.OBJECT)); - assertEquals(AF, AFChain); - assertEquals(AG, AKChain); - assertEquals(AK3, AKChain); + var AI = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); + var AJ = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); + var AH = generics.getBounds(m.getReturnType(), result.clazz, m); + assertEquals(AI, AJ); + assertEquals(AJ, AH); + assertEquals(AH, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); } @Test From e231cf722bce8add5740e8335433f6b22ada753f Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 4 Apr 2023 13:09:28 +0200 Subject: [PATCH 11/16] Fix type variable being declared twice --- .../de/dhbwstuttgart/target/generate/ASTToTargetAST.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index b28bfb41..3ebd5430 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -116,6 +116,8 @@ public class ASTToTargetAST { } return false; }); + } else if (input.stream().anyMatch(p -> p.getLeft().equals(pair.getLeft()))) { + return; } input.add(pair); @@ -605,14 +607,14 @@ public class ASTToTargetAST { if (left.equals(tph)) { var pair = new PairTPHsmallerTPH(tph, right); if (!generics.contains(pair)) { - generics.add(pair); + addToPairs(generics, pair); findAllBounds(right, generics, equality); found = true; } } } if (!found) - generics.add(new PairTPHequalRefTypeOrWildcardType(tph, OBJECT)); + addToPairs(generics, new PairTPHequalRefTypeOrWildcardType(tph, OBJECT)); } else if (type instanceof RefType refType) { refType.getParaList().forEach(t -> findAllBounds(t, generics, equality)); } @@ -663,7 +665,6 @@ public class ASTToTargetAST { } } } - System.out.println(input); } void findTphs(RefTypeOrTPHOrWildcardOrGeneric type, Set tphs, Map equality) { From 0908d512d9f2e2451878d3736903b6a939948fa6 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 4 Apr 2023 13:16:25 +0200 Subject: [PATCH 12/16] modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java --- .../de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 7108975b..2857d015 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -495,7 +495,7 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(Return returnExpr) { returnExpr.retexpr.accept(this); - constraintsSet.addUndConstraint(new Pair(returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), PairOperator.EQUALSDOT)); + constraintsSet.addUndConstraint(new Pair(returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), PairOperator.SMALLERDOT)); } @Override @@ -610,8 +610,8 @@ public class TYPEStmt implements StatementVisitor{ //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ENDE - methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT)); - extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT)); + methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT)); + extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT)); Set parameterContraints = generateParameterConstraints(forMethod, assumption, info, resolver); From 86d229cac94ec5af493cbb377a20c208d3f5e632 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 4 Apr 2023 15:54:03 +0200 Subject: [PATCH 13/16] modified: resources/bytecode/javFiles/Matrix.jav modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java modified: src/test/java/targetast/TestComplete.java --- resources/bytecode/javFiles/Matrix.jav | 2 +- .../typeinference/typeAlgo/TYPEStmt.java | 4 +-- src/test/java/targetast/TestComplete.java | 26 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/resources/bytecode/javFiles/Matrix.jav b/resources/bytecode/javFiles/Matrix.jav index f8c426b4..b94c0783 100644 --- a/resources/bytecode/javFiles/Matrix.jav +++ b/resources/bytecode/javFiles/Matrix.jav @@ -1,6 +1,6 @@ import java.util.Vector; import java.lang.Integer; -import java.lang.Float; +//import java.lang.Float; //import java.lang.Byte; //import java.lang.Boolean; diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 2857d015..ecd7600f 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -610,8 +610,8 @@ public class TYPEStmt implements StatementVisitor{ //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ENDE - methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT)); - extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT)); + methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT)); + extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT)); Set parameterContraints = generateParameterConstraints(forMethod, assumption, info, resolver); diff --git a/src/test/java/targetast/TestComplete.java b/src/test/java/targetast/TestComplete.java index 1b39618d..34d1370f 100644 --- a/src/test/java/targetast/TestComplete.java +++ b/src/test/java/targetast/TestComplete.java @@ -279,6 +279,32 @@ public class TestComplete { assertEquals(result, instanceOfClass_m3); } + @Test + public void scalarTest() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Scalar.jav"); + var scalar = classFiles.get("Scalar"); + + Vector> vv = new Vector<>(); + Vector v1 = new Vector<> (); + v1.addElement(2); + v1.addElement(2); + + var instanceOfClass_s1 = scalar.getDeclaredConstructor(Vector.class).newInstance(v1); + + Vector v2 = new Vector<> (); + v2.addElement(2); + v2.addElement(2); + + var instanceOfClass_s2 = scalar.getDeclaredConstructor(Vector.class).newInstance(v2); + + var mul = scalar.getDeclaredMethod("mul", Vector.class); + var result = mul.invoke(instanceOfClass_s1, instanceOfClass_s2); + System.out.println(instanceOfClass_s1.toString() + " * " + instanceOfClass_s2.toString() + " = " + result.toString()); + + assertEquals(result, 8); + } + + @Test public void mergeTest() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Merge.jav"); From f6fe3a68aaf95ab8bdfee269a36fcd2f9cca741e Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 4 Apr 2023 15:54:41 +0200 Subject: [PATCH 14/16] new file: resources/bytecode/javFiles/Scalar.jav --- resources/bytecode/javFiles/Scalar.jav | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 resources/bytecode/javFiles/Scalar.jav diff --git a/resources/bytecode/javFiles/Scalar.jav b/resources/bytecode/javFiles/Scalar.jav new file mode 100644 index 00000000..0ef1b580 --- /dev/null +++ b/resources/bytecode/javFiles/Scalar.jav @@ -0,0 +1,26 @@ +import java.util.Vector; +import java.lang.Integer; +import java.lang.Float; +//import java.lang.Byte; +//import java.lang.Boolean; + +public class Scalar extends Vector { + + Scalar(v) { + Integer i; + i = 0; + while(i < v.size()) { + this.add(v.elementAt(i)); + i=i+1; + } + } + + mul(v) { + var ret = 0; + var i = 0; + while(i < size()) { + ret = ret + this.elementAt(i) * v.elementAt(i); + } + return ret; + } +} From f6a4fe81423eb4cbf2ddcae990b840b5bbe39d12 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 4 Apr 2023 16:00:54 +0200 Subject: [PATCH 15/16] modified: resources/bytecode/javFiles/Scalar.jav --- resources/bytecode/javFiles/Scalar.jav | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/bytecode/javFiles/Scalar.jav b/resources/bytecode/javFiles/Scalar.jav index 0ef1b580..381869af 100644 --- a/resources/bytecode/javFiles/Scalar.jav +++ b/resources/bytecode/javFiles/Scalar.jav @@ -20,6 +20,7 @@ public class Scalar extends Vector { var i = 0; while(i < size()) { ret = ret + this.elementAt(i) * v.elementAt(i); + i = i+1; } return ret; } From 82061474b2d84e94d8dfb970d4a553d1f3090cfa Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 4 Apr 2023 17:26:13 +0200 Subject: [PATCH 16/16] modified: resources/bytecode/javFiles/Scalar.jav modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java --- resources/bytecode/javFiles/Scalar.jav | 3 +++ .../de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/resources/bytecode/javFiles/Scalar.jav b/resources/bytecode/javFiles/Scalar.jav index 381869af..b731df11 100644 --- a/resources/bytecode/javFiles/Scalar.jav +++ b/resources/bytecode/javFiles/Scalar.jav @@ -6,6 +6,8 @@ import java.lang.Float; public class Scalar extends Vector { + +/* /statistics auskommentiert Scalar(v) { Integer i; i = 0; @@ -14,6 +16,7 @@ public class Scalar extends Vector { i=i+1; } } + */ mul(v) { var ret = 0; diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index ecd7600f..198b0f4c 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -495,7 +495,7 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(Return returnExpr) { returnExpr.retexpr.accept(this); - constraintsSet.addUndConstraint(new Pair(returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), PairOperator.SMALLERDOT)); + constraintsSet.addUndConstraint(new Pair(returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), PairOperator.EQUALSDOT)); } @Override @@ -610,8 +610,8 @@ public class TYPEStmt implements StatementVisitor{ //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ENDE - methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT)); - extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT)); + methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT)); + extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT)); Set parameterContraints = generateParameterConstraints(forMethod, assumption, info, resolver);