From abe7553c379d1b51b55055ae1281f8bed22e7f41 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 17 Jan 2023 15:57:41 +0100 Subject: [PATCH] Add missing object bounds --- resources/bytecode/javFiles/Generics4.jav | 12 +++++++++ .../target/generate/ASTToTargetAST.java | 27 +++++++++++++++---- .../java/targetast/ASTToTypedTargetAST.java | 15 +++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 resources/bytecode/javFiles/Generics4.jav diff --git a/resources/bytecode/javFiles/Generics4.jav b/resources/bytecode/javFiles/Generics4.jav new file mode 100644 index 00000000..e66bd3a0 --- /dev/null +++ b/resources/bytecode/javFiles/Generics4.jav @@ -0,0 +1,12 @@ +import java.lang.String; +import java.lang.Integer; + +class Generics4 { + C m1(C b){ + return b; + } + + m2(x) { + return m1(); + } +} \ 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 a1359be4..3b34c459 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -17,7 +17,6 @@ import de.dhbwstuttgart.typeinference.result.*; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; -import java.util.stream.StreamSupport; /** * @author dholle @@ -453,11 +452,14 @@ public class ASTToTargetAST { eliminateInfima(input); } - RefTypeOrTPHOrWildcardOrGeneric getType(TypePlaceholder tph) { - if (equality.containsKey(tph)) { - return getType(equality.get(tph)); + RefTypeOrTPHOrWildcardOrGeneric getType(RefTypeOrTPHOrWildcardOrGeneric type) { + if (type instanceof TypePlaceholder tph) { + if (equality.containsKey(tph)) { + return getType(equality.get(tph)); + } + return concreteTypes.getOrDefault(tph, tph); } - return concreteTypes.getOrDefault(tph, tph); + return type; } TargetType getTargetType(TypePlaceholder tph) { @@ -655,6 +657,10 @@ public class ASTToTargetAST { .map(param -> new MethodParameter(convert(param.getType()), param.getName())).toList(); } + private boolean hasGeneric(Set generics, GenericRefType type) { + return generics.stream().anyMatch(g -> g.name().equals(type.getParsedName())); + } + private Set collectMethodGenerics(Set> generics, Method input) { var convertedGenerics = new HashSet<>(convert(generics)); outer: @@ -666,6 +672,17 @@ public class ASTToTargetAST { } convertedGenerics.addAll(convert(typeVar)); } + var returnType = sigma.getType(input.getReturnType()); + if ((returnType instanceof GenericRefType refType) && !hasGeneric(convertedGenerics, refType)) { + convertedGenerics.add(new TargetGeneric(refType.getParsedName(), convert(OBJECT))); + } + for (var param : input.getParameterList()) { + var type = sigma.getType(param.getType()); + 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/ASTToTypedTargetAST.java b/src/test/java/targetast/ASTToTypedTargetAST.java index 0d26fde5..6d5a08c0 100644 --- a/src/test/java/targetast/ASTToTypedTargetAST.java +++ b/src/test/java/targetast/ASTToTypedTargetAST.java @@ -148,4 +148,19 @@ public class ASTToTypedTargetAST { var generics3 = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader()); } + + @Test + public void definedGenerics4() throws Exception { + var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Generics4.jav").toFile(); + var compiler = new JavaTXCompiler(file); + var resultSet = compiler.typeInference(); + var converter = new ASTToTargetAST(resultSet); + var classes = compiler.sourceFiles.get(file).getClasses(); + + var generics4 = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader()); + + //var instance = generics4.getDeclaredConstructor().newInstance(); + //var method = generics4.getDeclaredMethod("m2", Object.class); + //method.invoke(instance, new Object()); + } }