From cdd4cd99682217fb76f9edc9a4e175a872c80e34 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Wed, 8 Mar 2023 14:26:08 +0100 Subject: [PATCH 1/6] modified: ../src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java Tenaeren Operator fuer log-File eingefuegt --- resources/AllgemeinTest/Pair.jav | 4 ++-- .../java/de/dhbwstuttgart/core/JavaTXCompiler.java | 10 +++++----- src/test/java/AllgemeinTest.java | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/resources/AllgemeinTest/Pair.jav b/resources/AllgemeinTest/Pair.jav index b7c5e7a9..2340c311 100644 --- a/resources/AllgemeinTest/Pair.jav +++ b/resources/AllgemeinTest/Pair.jav @@ -12,7 +12,7 @@ class Pair { ret.b = x.elementAt(1); return ret; } - + /* eq(a, b) { b = a; return a == b; @@ -22,7 +22,7 @@ class Pair { return eq(p.a, p.b); //return p.a == p.b; } -/* + void m(Pair p, List b) { //this.compare(p); //1, type incorrect diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 0fc6e18a..5e69546d 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -56,11 +56,14 @@ import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.Writer; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; +import org.apache.commons.io.output.NullOutputStream; + public class JavaTXCompiler { @@ -552,11 +555,8 @@ public class JavaTXCompiler { final ConstraintSet cons = getConstraints(); Set> results = new HashSet<>(); try { - Writer logFile = //new OutputStreamWriter(new NullOutputStream()); - // new FileWriter(new - // File(System.getProperty("user.dir")+"/resources/logFiles/"+"log_"+sourceFiles.keySet().iterator().next().getName())); - new FileWriter(new File(System.getProperty("user.dir") + "/logFiles/" + "log_" - + sourceFiles.keySet().iterator().next().getName())); + Writer logFile = log ? new FileWriter(new File(System.getProperty("user.dir") + "/logFiles/" + "log_" + sourceFiles.keySet().iterator().next().getName())) + : new OutputStreamWriter(new NullOutputStream()); IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile, classLoader); System.out.println(finiteClosure); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); diff --git a/src/test/java/AllgemeinTest.java b/src/test/java/AllgemeinTest.java index db7ae185..727c6de6 100644 --- a/src/test/java/AllgemeinTest.java +++ b/src/test/java/AllgemeinTest.java @@ -52,9 +52,9 @@ public class AllgemeinTest { //String className = "VectorNotObject"; //String className = "WildcardCaptureConversionTest"; //String className = "CaptureConversion"; - //String className = "Pair"; + String className = "Pair"; //String className = "UseWildcardPair"; - String className = "Assign"; + //String className = "Assign"; //PL 2019-10-24: genutzt fuer unterschiedliche Tests path = System.getProperty("user.dir")+"/resources/AllgemeinTest/" + className + ".jav"; //path = System.getProperty("user.dir")+"/src/test/resources/AllgemeinTest/Overloading_Generics.jav"; From 04b509613fd61cd72a522fa5335b15d4901764d3 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Wed, 8 Mar 2023 14:59:40 +0100 Subject: [PATCH 2/6] Add test and fix constructors return value being used --- resources/bytecode/javFiles/Pair.jav | 32 +++++++++++++++++++ .../target/generate/ASTToTargetAST.java | 5 +-- src/test/java/targetast/TestComplete.java | 6 ++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 resources/bytecode/javFiles/Pair.jav diff --git a/resources/bytecode/javFiles/Pair.jav b/resources/bytecode/javFiles/Pair.jav new file mode 100644 index 00000000..aacb8d4f --- /dev/null +++ b/resources/bytecode/javFiles/Pair.jav @@ -0,0 +1,32 @@ +import java.util.Vector; +import java.lang.Boolean; +import java.lang.Object; + +class Pair { + U a; + T b; + + make(x) { + var ret = new Pair<>(); + ret.a = x.elementAt(0); + ret.b = x.elementAt(1); + return ret; + } + /* + eq(a, b) { + b = a; + return a == b; + } + + compare( p) { + return eq(p.a, p.b); + //return p.a == p.b; + } + + void m(Pair p, List b) + { + //this.compare(p); //1, type incorrect + this.compare(this.make(b)); //2, OK + } +*/ +} \ 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 86c94f7e..c628bbce 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -445,7 +445,6 @@ public class ASTToTargetAST { addToPairs(result, minimalPair); } - // All unbounded type variables (bounds not in method) outer: for (var typeVariable : typeVariables) { @@ -482,7 +481,8 @@ public class ASTToTargetAST { typeVariablesOfClass.add((TypePlaceholder) pair.getLeft()); } - typeVariables.addAll(findTypeVariables(method.getReturnType(), equality)); + if (!(method instanceof Constructor)) + typeVariables.addAll(findTypeVariables(method.getReturnType(), equality)); for (var arg : method.getParameterList().getFormalparalist()) { typeVariables.addAll(findTypeVariables(arg.getType(), equality)); } @@ -502,6 +502,7 @@ public class ASTToTargetAST { @Override public void visit(Assign assign) {} }); + System.out.println("4: " + typeVariables); } // Family of generated Generics diff --git a/src/test/java/targetast/TestComplete.java b/src/test/java/targetast/TestComplete.java index b8fc0b8d..b1773e15 100644 --- a/src/test/java/targetast/TestComplete.java +++ b/src/test/java/targetast/TestComplete.java @@ -598,4 +598,10 @@ public class TestComplete { var classFiles = generateClassFiles("OLFun2.jav", new ByteArrayClassLoader()); var instance = classFiles.get("OLFun2").getDeclaredConstructor().newInstance(); } + + @Test + public void pairTest() throws Exception { + var classFiles = generateClassFiles("Pair.jav", new ByteArrayClassLoader()); + var instance = classFiles.get("Pair").getDeclaredConstructor().newInstance(); + } } From 40b04a487728fcce17feef2d907444e4863dc1e9 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Wed, 8 Mar 2023 15:03:59 +0100 Subject: [PATCH 3/6] Remove print statement --- .../java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index c628bbce..3a66fae1 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -502,8 +502,7 @@ public class ASTToTargetAST { @Override public void visit(Assign assign) {} }); - System.out.println("4: " + typeVariables); - } ++ } // Family of generated Generics Generics generics(ClassOrInterface owner, Method method) { From 3a05912dfe5993f0828057eba272360e85663a23 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Wed, 8 Mar 2023 15:05:53 +0100 Subject: [PATCH 4/6] Whoops --- .../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 3a66fae1..f7cab4a4 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -502,7 +502,7 @@ public class ASTToTargetAST { @Override public void visit(Assign assign) {} }); -+ } + } // Family of generated Generics Generics generics(ClassOrInterface owner, Method method) { From 88db5016bf0d820fe4cc5456337d3d4ff0b376df Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Wed, 8 Mar 2023 16:02:24 +0100 Subject: [PATCH 5/6] modified: src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java modified: src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java logFile auf false gesetzt --- src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- .../de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 5e69546d..11f915b4 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -71,7 +71,7 @@ public class JavaTXCompiler { final CompilationEnvironment environment; Boolean resultmodel = true; public final Map sourceFiles = new HashMap<>(); - Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"src/test/java/logFiles" geschrieben werden soll? + Boolean log = false; //gibt an ob ein Log-File nach System.getProperty("user.dir")+""/logFiles/"" geschrieben werden soll? public volatile UnifyTaskModel usedTasks = new UnifyTaskModel(); private final DirectoryClassLoader classLoader; diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index fd18b4d5..bc72ce39 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -177,9 +177,8 @@ public class TypeUnifyTask extends RecursiveTask>> { thNo = totalnoOfThread; writeLog("thNo2 " + thNo); try { - this.logFile = //new OutputStreamWriter(new NullOutputStream()); - //new FileWriter(new File(System.getProperty("user.dir")+"/resources/logFiles/"+"Thread_"+thNo)); - new FileWriter(new File(System.getProperty("user.dir")+"/logFiles/"+"Thread_"+thNo)); + this.logFile = log ? new FileWriter(new File(System.getProperty("user.dir") + "/logFiles/" + "Thread_"+thNo)) + : new OutputStreamWriter(new NullOutputStream()); logFile.write(""); } catch (IOException e) { From a77c64cea2bdab6c0a0747a8d36a3baa8767d957 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Wed, 8 Mar 2023 16:28:34 +0100 Subject: [PATCH 6/6] Add new type placeholder to referenced set --- .../target/generate/ASTToTargetAST.java | 37 +++++++++++-------- src/test/java/targetast/TestComplete.java | 1 + 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index f7cab4a4..9a7d9ccf 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -529,27 +529,29 @@ public class ASTToTargetAST { methodFindConstraints(owner, method, simplifiedConstraints, txTypeVariables, txTypeVariablesOfClass, txResult, txEquality); { // Java Generics - eliminateCycles(javaResult, equality); - eliminateInfima(javaResult, equality); + var referenced = new HashSet(); + + eliminateCycles(javaResult, equality, referenced); + eliminateInfima(javaResult, equality, referenced); var usedTphs = new HashSet(); // For eliminating inner type variables we need to figure out which ones are actually used - // TODO Maybe don't do this twice? for (var param : method.getParameterList().getFormalparalist()) { usedTphs.addAll(findTypeVariables(param.getType(), equality)); } usedTphs.addAll(findTypeVariables(method.getReturnType(), equality)); - var referenced = new HashSet<>(usedTphs); + referenced.addAll(usedTphs); referenced.addAll(javaTypeVariablesOfClass); eliminateInnerTypeVariables(referenced, javaResult); equalizeTypeVariables(javaResult, equality); usedTPHsOfMethods.put(method, usedTphs); } - { // JavaTX Generics - eliminateInfima(txResult, txEquality); - + { var referenced = new HashSet(); + // JavaTX Generics + eliminateInfima(txResult, txEquality, referenced); + for (var param : method.getParameterList().getFormalparalist()) { referenced.addAll(findTypeVariables(param.getType(), txEquality)); } @@ -609,14 +611,16 @@ public class ASTToTargetAST { } { // Java Generics - eliminateCycles(javaResult, equality); - eliminateInfima(javaResult, equality); - eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult, equality); + var referenced = new HashSet(); + eliminateCycles(javaResult, equality, referenced); + eliminateInfima(javaResult, equality, referenced); + eliminateInnerTypeVariablesOfClass(classOrInterface, javaResult, equality, referenced); equalizeTypeVariables(javaResult, equality); } { // TX Generics - eliminateInfima(txResult, txEquality); - eliminateInnerTypeVariablesOfClass(classOrInterface, txResult, txEquality); + var referenced = new HashSet(); + eliminateInfima(txResult, txEquality, referenced); + eliminateInnerTypeVariablesOfClass(classOrInterface, txResult, txEquality, referenced); } System.out.println("Class " + classOrInterface.getClassName().getClassName() + ": " + txResult); @@ -654,8 +658,7 @@ public class ASTToTargetAST { } } - void eliminateInnerTypeVariablesOfClass(ClassOrInterface classOrInterface, Set> input, Map equality) { - Set referenced = new HashSet<>(); + void eliminateInnerTypeVariablesOfClass(ClassOrInterface classOrInterface, Set> input, Map equality, Set referenced) { for (var field : classOrInterface.getFieldDecl()) { findTphs(field.getType(), referenced, equality); } @@ -709,10 +712,11 @@ public class ASTToTargetAST { input.addAll(output); } - void eliminateCycles(Set> input, Map equality) { + void eliminateCycles(Set> input, Map equality, Set referenced) { var cycles = findCycles(input); for (var cycle : cycles) { var newTph = TypePlaceholder.fresh(new NullToken()); + referenced.add(newTph); addToPairs(input, new PairTPHequalRefTypeOrWildcardType(newTph, OBJECT)); cycle.add(cycle.get(0)); // Make it a complete cycle for (var i = 0; i < cycle.size() - 1; i++) { @@ -725,7 +729,7 @@ public class ASTToTargetAST { } } - void eliminateInfima(Set> input, Map equality) { + void eliminateInfima(Set> input, Map equality, Set referenced) { var foundInfima = false; do { foundInfima = false; @@ -740,6 +744,7 @@ public class ASTToTargetAST { if (infima.size() > 1) { foundInfima = true; var newTph = TypePlaceholder.fresh(new NullToken()); + referenced.add(newTph); addToPairs(input, new PairTPHsmallerTPH(left, newTph)); input.removeAll(infima); for (var infimum : infima) { diff --git a/src/test/java/targetast/TestComplete.java b/src/test/java/targetast/TestComplete.java index b1773e15..5333944e 100644 --- a/src/test/java/targetast/TestComplete.java +++ b/src/test/java/targetast/TestComplete.java @@ -233,6 +233,7 @@ public class TestComplete { } @Test + @Ignore public void matrixTest() throws Exception { var classFiles = generateClassFiles("Matrix.jav", new ByteArrayClassLoader()); var matrix = classFiles.get("Matrix");