This commit is contained in:
Daniel Holle 2023-05-08 13:32:39 +02:00
parent 25c0c9c101
commit f8f76e9f7d
3 changed files with 25 additions and 18 deletions

View File

@ -1,12 +1,12 @@
public class TestContraVariant { public class TestContraVariant {
main(x) {
return x;
}
m(x) { m(x) {
var z = main(x);
var y; var y;
x = y; x = y;
return y; return new Pair(z, y);
}
main(x) {
return m(x);
} }
} }

View File

@ -51,6 +51,7 @@ public class ASTFactory {
var classReader = new ClassReader(bytes); var classReader = new ClassReader(bytes);
var classVisitor = new ClassVisitor(Opcodes.ASM7) { var classVisitor = new ClassVisitor(Opcodes.ASM7) {
String classSignature; String classSignature;
@Override @Override
public void visitAttribute(Attribute attribute) { public void visitAttribute(Attribute attribute) {
if (attribute.type.equals("JavaTXSignature")) { if (attribute.type.equals("JavaTXSignature")) {
@ -181,7 +182,7 @@ public class ASTFactory {
}else{ }else{
jreRetType = jreMethod.getReturnType(); jreRetType = jreMethod.getReturnType();
} }
returnType = createType(jreRetType); returnType = createType(jreRetType);
Parameter[] jreParams = jreMethod.getParameters(); Parameter[] jreParams = jreMethod.getParameters();
Type[] jreGenericParams = jreMethod.getGenericParameterTypes(); Type[] jreGenericParams = jreMethod.getGenericParameterTypes();
List<FormalParameter> params = new ArrayList<>(); List<FormalParameter> params = new ArrayList<>();
@ -284,7 +285,7 @@ public class ASTFactory {
public SignatureVisitor visitTypeArgument(char wildcard) { public SignatureVisitor visitTypeArgument(char wildcard) {
this.wildcard = wildcard; this.wildcard = wildcard;
return this; return this;
} }
boolean equals(RefTypeOrTPHOrWildcardOrGeneric a, RefTypeOrTPHOrWildcardOrGeneric b) { boolean equals(RefTypeOrTPHOrWildcardOrGeneric a, RefTypeOrTPHOrWildcardOrGeneric b) {
if (b instanceof SuperWildcardType wc) if (b instanceof SuperWildcardType wc)
@ -395,11 +396,12 @@ public class ASTFactory {
public static ClassOrInterface createObjectClass() { public static ClassOrInterface createObjectClass() {
return createClass(Object.class); return createClass(Object.class);
} }
public static RefType createObjectType() { public static RefType createObjectType() {
return new RefType(createClass(Object.class).getClassName(), new NullToken()); return new RefType(createClass(Object.class).getClassName(), new NullToken());
} }
/* /*
public Constructor createEmptyConstructor(Class parent){ public Constructor createEmptyConstructor(Class parent){
Block block = new Block(); Block block = new Block();
block.setType(new de.dhbwstuttgart.syntaxtree.type.Void(block, 0)); block.setType(new de.dhbwstuttgart.syntaxtree.type.Void(block, 0));

View File

@ -145,7 +145,7 @@ public class ASTToTargetAST {
return containsLeft && containsRight; return containsLeft && containsRight;
} }
static void addToPairs(Set<ResultPair<?, ?>> input, ResultPair<?, ?> pair) { void addToPairs(Set<ResultPair<?, ?>> input, ResultPair<?, ?> pair) {
if (pair instanceof PairTPHsmallerTPH) { if (pair instanceof PairTPHsmallerTPH) {
input.removeIf(pair2 -> { input.removeIf(pair2 -> {
if (pair2 instanceof PairTPHequalRefTypeOrWildcardType) { if (pair2 instanceof PairTPHequalRefTypeOrWildcardType) {
@ -248,7 +248,8 @@ public class ASTToTargetAST {
var optMethod = findMethod(owner, methodCall.name, methodCall.getArgumentList()); var optMethod = findMethod(owner, methodCall.name, methodCall.getArgumentList());
if (optMethod.isEmpty()) return; if (optMethod.isEmpty()) return;
var method = optMethod.get(); var method = optMethod.get();
var generics = family(owner, method); var generics = generics(owner, method);
System.out.println("Generics: " + generics);
// transitive and // transitive and
var all = transitiveClosure(generics); var all = transitiveClosure(generics);
@ -265,17 +266,21 @@ public class ASTToTargetAST {
outer: outer:
for (var R1 : typeVariables) { for (var R1 : typeVariables) {
if (typeVariablesOfClass.contains(R1)) continue; if (typeVariablesOfClass.contains(R1)) continue;
for (var generic : all) { for (var generic : all) if (generic instanceof PairTPHsmallerTPH ptph) {
if (!(generic.getRight() instanceof TypePlaceholder type)) var l = equality.getOrDefault(ptph.left, ptph.left);
continue; var r = equality.getOrDefault(ptph.right, ptph.right);
for (var pair : simplifiedConstraints) { 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; continue;
for (var R2 : typeVariables) { for (var R2 : typeVariables) {
for (var pair2 : simplifiedConstraints) { 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; continue;
if (R1.equals(R2)) continue; if (R1.equals(R2)) continue;
if (!T1s.contains(R1) || !R2.equals(T2)) continue; if (!T1s.contains(R1) || !R2.equals(T2)) continue;
@ -480,8 +485,8 @@ public class ASTToTargetAST {
} }
Set<ResultPair<?, ?>> generics(ClassOrInterface owner, Method method) { Set<ResultPair<?, ?>> generics(ClassOrInterface owner, Method method) {
if (computedGenericsOfMethods.containsKey(method)) //if (computedGenericsOfMethods.containsKey(method))
return computedGenericsOfMethods.get(method); // return computedGenericsOfMethods.get(method);
var classGenerics = generics(owner); var classGenerics = generics(owner);
@ -597,7 +602,7 @@ public class ASTToTargetAST {
continue outer; continue outer;
} }
if (classGenerics == null || classGenerics.stream().noneMatch((pair) -> pair.getLeft().equals(ptph.right))) 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));
} }
} }
} }