From d55b402269205edafefeaf590001025f7286b7e8 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Wed, 28 Jun 2023 14:09:24 +0200 Subject: [PATCH 1/5] Some fixes --- .../target/generate/GenerateGenerics.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java b/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java index 8c06b5d6..e966d29d 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java @@ -140,6 +140,7 @@ public abstract class GenerateGenerics { this.astToTargetAST = astToTargetAST; for (var constraint : constraints.results) { if (constraint instanceof PairTPHsmallerTPH p) { + System.out.println(p.left + " " + p.left.getVariance()); simplifiedConstraints.add(new PairLT(new TPH(p.left), new TPH(p.right))); } else if (constraint instanceof PairTPHEqualTPH p) { equality.put(p.getLeft(), p.getRight()); @@ -213,7 +214,8 @@ public abstract class GenerateGenerics { equality.put(entry.getKey(), to); } } - to.setVariance(from.getVariance()); + System.out.println(from + " -> " + to + " " + from.getVariance()); + //from.setVariance(to.getVariance()); equality.put(from, to); referenced.remove(new TPH(from)); referenced.add(new TPH(to)); @@ -838,19 +840,24 @@ public abstract class GenerateGenerics { } } - TPH findConnectionToReturnType(Set returnTypes, Set input, Set visited, TPH tph) { + Set findConnectionToReturnType(Set returnTypes, Set input, Set visited, TPH tph) { if (returnTypes.contains(tph)) { - return tph; + var res = new HashSet(); + res.add(tph); + return res; } else { for (var pair : input) if (pair instanceof PairLT ptph) { if (ptph.left.equals(tph) && !visited.contains(ptph.right)) { visited.add(ptph.right); var result = findConnectionToReturnType(returnTypes, input, visited, ptph.right); - if (result != null) return result; + if (result.size() > 0) { + result.add(ptph.right); + return result; + }; } } } - return null; + return new HashSet<>(); } void eliminateInfimaConnectedToReturnType(Method method, Set input, Set referenced) { @@ -867,19 +874,22 @@ public abstract class GenerateGenerics { } } if (infima.size() > 1) { + System.out.println(infima); for (var pair : infima) { var returnTypes = findTypeVariables(method.getReturnType()); - var returnType = findConnectionToReturnType(returnTypes, input, new HashSet<>(), pair.left); - if (returnType != null && !returnType.equals(pair.left)) { - System.out.println("Equals now: " + pair.left.resolve() + " " + returnType.resolve()); - addToEquality(pair.left.resolve(), returnType.resolve(), referenced); + var chain = findConnectionToReturnType(returnTypes, input, new HashSet<>(), pair.left); + System.out.println("Find: " + pair.left + " " + chain); + chain.remove(pair.left); + if (chain.size() > 0) { + for (var tph : chain) + addToEquality(pair.left.resolve(), tph.resolve(), referenced); foundInfima = true; } } } } } while (foundInfima); - input.removeIf((i) -> !(i instanceof PairLT lt) || lt.left.equals(lt.right)); + input.removeIf((i) -> (i instanceof PairLT lt) && lt.left.equals(lt.right)); } void eliminateInfima(Set input, Set referenced) { From d05054755c69a141388dc811949435e42f770653 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 3 Jul 2023 10:13:01 +0200 Subject: [PATCH 2/5] Fix bug --- .../target/generate/ASTToTargetAST.java | 4 +-- .../target/generate/GenerateGenerics.java | 30 ++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 5db79459..bfe353a1 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -240,8 +240,8 @@ public class ASTToTargetAST { ); } - private final Map usedFunN = new HashMap<>(); - private final Set usedFunNSuperTypes = new HashSet<>(); + private static final Map usedFunN = new HashMap<>(); + private static final Set usedFunNSuperTypes = new HashSet<>(); public Map auxiliaries = new HashMap<>(); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java b/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java index 8c06b5d6..e966d29d 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java @@ -140,6 +140,7 @@ public abstract class GenerateGenerics { this.astToTargetAST = astToTargetAST; for (var constraint : constraints.results) { if (constraint instanceof PairTPHsmallerTPH p) { + System.out.println(p.left + " " + p.left.getVariance()); simplifiedConstraints.add(new PairLT(new TPH(p.left), new TPH(p.right))); } else if (constraint instanceof PairTPHEqualTPH p) { equality.put(p.getLeft(), p.getRight()); @@ -213,7 +214,8 @@ public abstract class GenerateGenerics { equality.put(entry.getKey(), to); } } - to.setVariance(from.getVariance()); + System.out.println(from + " -> " + to + " " + from.getVariance()); + //from.setVariance(to.getVariance()); equality.put(from, to); referenced.remove(new TPH(from)); referenced.add(new TPH(to)); @@ -838,19 +840,24 @@ public abstract class GenerateGenerics { } } - TPH findConnectionToReturnType(Set returnTypes, Set input, Set visited, TPH tph) { + Set findConnectionToReturnType(Set returnTypes, Set input, Set visited, TPH tph) { if (returnTypes.contains(tph)) { - return tph; + var res = new HashSet(); + res.add(tph); + return res; } else { for (var pair : input) if (pair instanceof PairLT ptph) { if (ptph.left.equals(tph) && !visited.contains(ptph.right)) { visited.add(ptph.right); var result = findConnectionToReturnType(returnTypes, input, visited, ptph.right); - if (result != null) return result; + if (result.size() > 0) { + result.add(ptph.right); + return result; + }; } } } - return null; + return new HashSet<>(); } void eliminateInfimaConnectedToReturnType(Method method, Set input, Set referenced) { @@ -867,19 +874,22 @@ public abstract class GenerateGenerics { } } if (infima.size() > 1) { + System.out.println(infima); for (var pair : infima) { var returnTypes = findTypeVariables(method.getReturnType()); - var returnType = findConnectionToReturnType(returnTypes, input, new HashSet<>(), pair.left); - if (returnType != null && !returnType.equals(pair.left)) { - System.out.println("Equals now: " + pair.left.resolve() + " " + returnType.resolve()); - addToEquality(pair.left.resolve(), returnType.resolve(), referenced); + var chain = findConnectionToReturnType(returnTypes, input, new HashSet<>(), pair.left); + System.out.println("Find: " + pair.left + " " + chain); + chain.remove(pair.left); + if (chain.size() > 0) { + for (var tph : chain) + addToEquality(pair.left.resolve(), tph.resolve(), referenced); foundInfima = true; } } } } } while (foundInfima); - input.removeIf((i) -> !(i instanceof PairLT lt) || lt.left.equals(lt.right)); + input.removeIf((i) -> (i instanceof PairLT lt) && lt.left.equals(lt.right)); } void eliminateInfima(Set input, Set referenced) { From b04201de423c3a03a0123d7e96fcd30c250f83e5 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Tue, 4 Jul 2023 11:40:23 +0200 Subject: [PATCH 3/5] Don't load classes that get compiled by a source file --- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 04537c2c..958d7cea 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -548,16 +548,13 @@ public class JavaTXCompiler { SourceFile sf = sourceFiles.get(f); allClasses.addAll(getAvailableClasses(sf)); allClasses.addAll(sf.getClasses()); - var newClasses = CompilationEnvironment.loadDefaultPackageClasses(f,classLoader).stream().map(ASTFactory::createClass).collect(Collectors.toList()); + var newClasses = CompilationEnvironment.loadDefaultPackageClasses(f,classLoader).stream().map(ASTFactory::createClass).toList(); for (var clazz : newClasses) { - var found = false; - for (var old : allClasses) { - if (clazz.getClassName().equals(old.getClassName())) { - found = true; - break; - } - } - if (!found) allClasses.add(clazz); + // Don't load classes that get recompiled + if (sf.getClasses().stream().anyMatch(nf -> nf.getClassName().equals(clazz.getClassName()))) + continue; + if (allClasses.stream().noneMatch(old -> old.getClassName().equals(clazz.getClassName()))) + allClasses.add(clazz); } } From 5c62191f3bf811dcd10cf52aa9100373366a93b0 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Tue, 4 Jul 2023 12:17:14 +0200 Subject: [PATCH 4/5] Consider constructors when creating class generics --- resources/bytecode/javFiles/PairNoGenerics.jav | 0 .../target/generate/GenerateGenerics.java | 12 ++++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 resources/bytecode/javFiles/PairNoGenerics.jav diff --git a/resources/bytecode/javFiles/PairNoGenerics.jav b/resources/bytecode/javFiles/PairNoGenerics.jav new file mode 100644 index 00000000..e69de29b diff --git a/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java b/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java index e966d29d..2f437d6e 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.target.generate; +import com.google.common.collect.Streams; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Constructor; @@ -18,6 +19,8 @@ import de.dhbwstuttgart.util.Pair; import java.util.*; import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; public abstract class GenerateGenerics { @@ -145,6 +148,7 @@ public abstract class GenerateGenerics { } else if (constraint instanceof PairTPHEqualTPH p) { equality.put(p.getLeft(), p.getRight()); } else if (constraint instanceof PairTPHequalRefTypeOrWildcardType p) { + System.out.println(p.left + " = " + p.right); concreteTypes.put(new TPH(p.left), p.right); } } @@ -761,14 +765,14 @@ public abstract class GenerateGenerics { oldFamily.clear(); oldFamily.putAll(familyOfMethods); familyOfMethods.clear(); - for (var method : classOrInterface.getMethods()) { + Stream.concat(classOrInterface.getMethods().stream(), classOrInterface.getConstructors().stream()).forEach(method -> { family(classOrInterface, method); - } + }); } while(!oldFamily.equals(familyOfMethods)); - for (var method : classOrInterface.getMethods()) { + Stream.concat(classOrInterface.getMethods().stream(), classOrInterface.getConstructors().stream()).forEach(method -> { generics(classOrInterface, method); - } + }); } private void findChain(Set referenced, Set input, Set output, TPH start, TPH end, Set chain) { From a17e1f473a6a09911f4e4a9f09a760ace2613edf Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Wed, 5 Jul 2023 15:48:27 +0200 Subject: [PATCH 5/5] Catch linkage error --- .../target/generate/ASTToTargetAST.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index bfe353a1..639bdfd0 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -240,8 +240,8 @@ public class ASTToTargetAST { ); } - private static final Map usedFunN = new HashMap<>(); - private static final Set usedFunNSuperTypes = new HashSet<>(); + private final Map usedFunN = new HashMap<>(); + private final Set usedFunNSuperTypes = new HashSet<>(); public Map auxiliaries = new HashMap<>(); @@ -295,7 +295,9 @@ public class ASTToTargetAST { try { classLoader.findClass(superClassName); } catch (ClassNotFoundException e) { - classLoader.loadClass(code); + try { + classLoader.loadClass(code); + } catch (LinkageError ignored) {} } auxiliaries.put(superClassName, code); } @@ -306,7 +308,9 @@ public class ASTToTargetAST { try { classLoader.findClass(className); } catch (ClassNotFoundException e) { - classLoader.loadClass(code); + try { + classLoader.loadClass(code); + } catch (LinkageError ignored) {} } usedFunN.put(className, gep); auxiliaries.put(className, code);