From f8f76e9f7d1ce288f49c29a4f155b045c083c688 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 8 May 2023 13:32:39 +0200 Subject: [PATCH] WIP --- .../insertGenericsJav/TestContraVariant.jav | 10 ++++---- .../syntaxtree/factory/ASTFactory.java | 8 +++--- .../target/generate/ASTToTargetAST.java | 25 +++++++++++-------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/resources/insertGenericsJav/TestContraVariant.jav b/resources/insertGenericsJav/TestContraVariant.jav index 99c83de4..5876a446 100644 --- a/resources/insertGenericsJav/TestContraVariant.jav +++ b/resources/insertGenericsJav/TestContraVariant.jav @@ -1,12 +1,12 @@ public class TestContraVariant { + main(x) { + return x; + } m(x) { + var z = main(x); var y; x = y; - return y; - } - - main(x) { - return m(x); + return new Pair(z, y); } } \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 5b631250..bdef497f 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -51,6 +51,7 @@ public class ASTFactory { var classReader = new ClassReader(bytes); var classVisitor = new ClassVisitor(Opcodes.ASM7) { String classSignature; + @Override public void visitAttribute(Attribute attribute) { if (attribute.type.equals("JavaTXSignature")) { @@ -181,7 +182,7 @@ public class ASTFactory { }else{ jreRetType = jreMethod.getReturnType(); } - returnType = createType(jreRetType); + returnType = createType(jreRetType); Parameter[] jreParams = jreMethod.getParameters(); Type[] jreGenericParams = jreMethod.getGenericParameterTypes(); List params = new ArrayList<>(); @@ -284,7 +285,7 @@ public class ASTFactory { public SignatureVisitor visitTypeArgument(char wildcard) { this.wildcard = wildcard; return this; - } + } boolean equals(RefTypeOrTPHOrWildcardOrGeneric a, RefTypeOrTPHOrWildcardOrGeneric b) { if (b instanceof SuperWildcardType wc) @@ -395,11 +396,12 @@ public class ASTFactory { public static ClassOrInterface createObjectClass() { return createClass(Object.class); } + public static RefType createObjectType() { return new RefType(createClass(Object.class).getClassName(), new NullToken()); } - /* + /* public Constructor createEmptyConstructor(Class parent){ Block block = new Block(); block.setType(new de.dhbwstuttgart.syntaxtree.type.Void(block, 0)); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index f909a59c..2f14c859 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -145,7 +145,7 @@ public class ASTToTargetAST { return containsLeft && containsRight; } - static void addToPairs(Set> input, ResultPair pair) { + void addToPairs(Set> input, ResultPair pair) { if (pair instanceof PairTPHsmallerTPH) { input.removeIf(pair2 -> { if (pair2 instanceof PairTPHequalRefTypeOrWildcardType) { @@ -248,7 +248,8 @@ public class ASTToTargetAST { var optMethod = findMethod(owner, methodCall.name, methodCall.getArgumentList()); if (optMethod.isEmpty()) return; var method = optMethod.get(); - var generics = family(owner, method); + var generics = generics(owner, method); + System.out.println("Generics: " + generics); // transitive and var all = transitiveClosure(generics); @@ -265,17 +266,21 @@ public class ASTToTargetAST { outer: for (var R1 : typeVariables) { if (typeVariablesOfClass.contains(R1)) continue; - for (var generic : all) { - if (!(generic.getRight() instanceof TypePlaceholder type)) - continue; + for (var generic : all) if (generic instanceof PairTPHsmallerTPH ptph) { + var l = equality.getOrDefault(ptph.left, ptph.left); + var r = equality.getOrDefault(ptph.right, ptph.right); for (var pair : simplifiedConstraints) { - if (!(pair.left.equals(R1) && pair.right.equals(generic.getLeft()))) + var l2 = equality.getOrDefault(pair.left, pair.left); + var r2 = equality.getOrDefault(pair.right, pair.right); + if (!(l2.equals(R1) && r2.equals(l))) continue; for (var R2 : typeVariables) { for (var pair2 : simplifiedConstraints) { - if (!(pair2.right.equals(R2) && pair2.left.equals(type))) + var l3 = equality.getOrDefault(pair2.left, pair2.left); + var r3 = equality.getOrDefault(pair2.right, pair2.right); + if (!(r3.equals(R2) && l3.equals(r))) continue; if (R1.equals(R2)) continue; if (!T1s.contains(R1) || !R2.equals(T2)) continue; @@ -480,8 +485,8 @@ public class ASTToTargetAST { } Set> generics(ClassOrInterface owner, Method method) { - if (computedGenericsOfMethods.containsKey(method)) - return computedGenericsOfMethods.get(method); + //if (computedGenericsOfMethods.containsKey(method)) + // return computedGenericsOfMethods.get(method); var classGenerics = generics(owner); @@ -597,7 +602,7 @@ public class ASTToTargetAST { continue outer; } if (classGenerics == null || classGenerics.stream().noneMatch((pair) -> pair.getLeft().equals(ptph.right))) - result.add(new PairTPHequalRefTypeOrWildcardType(ptph.right, OBJECT)); + addToPairs(result, new PairTPHequalRefTypeOrWildcardType(ptph.right, OBJECT)); } } }