From 99f219de3b40d8e94024984f0e63ef396d7396d9 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 27 May 2024 12:14:00 +0200 Subject: [PATCH] Work on #338, partially fixed --- resources/bytecode/javFiles/Bug338.jav | 11 +++++++++++ .../dhbwstuttgart/target/generate/ASTToTargetAST.java | 8 +++++--- src/test/java/TestComplete.java | 7 +++++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 resources/bytecode/javFiles/Bug338.jav diff --git a/resources/bytecode/javFiles/Bug338.jav b/resources/bytecode/javFiles/Bug338.jav new file mode 100644 index 00000000..fa846e95 --- /dev/null +++ b/resources/bytecode/javFiles/Bug338.jav @@ -0,0 +1,11 @@ +import java.util.List; +import java.lang.Integer; +import java.lang.String; +import java.lang.Object; +import java.util.List; + +public class Bug338 { + public hashCode() { + return List.of(42); + } +} \ 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 1c485d7e..e62fbf80 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -366,7 +366,6 @@ public class ASTToTargetAST { for (var i = 0; i < params.size(); i++) { var a = TargetType.toPrimitive(params.get(i).pattern().type()); var b = convert(sParams.getFormalparalist().get(i).getType()); - System.out.println(a + " " + b); if (!Objects.equals(a, b)) return false; } return true; @@ -387,8 +386,11 @@ public class ASTToTargetAST { var superMethod = findSuperMethodToOverride(currentClass, method.getName(), params); if (superMethod.isPresent()) { // If we find a super method to override, use its parameters and return types - returnType = convert(superMethod.get().getReturnType(), this.generics.javaGenerics); - params = convert(superMethod.get().getParameterList(), method.getParameterList(), this.generics.javaGenerics); + var newReturnType = convert(superMethod.get().getReturnType(), this.generics.javaGenerics); + if (newReturnType instanceof TargetPrimitiveType && TargetType.toPrimitive(returnType).equals(newReturnType)) { + returnType = newReturnType; + params = convert(superMethod.get().getParameterList(), method.getParameterList(), this.generics.javaGenerics); + } } List finalParams = params; diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 93cfb387..3058ac14 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -1084,4 +1084,11 @@ public class TestComplete { var res = clazz.getDeclaredMethod("convert", List.class).invoke(instance, list); assertEquals(res, List.of(6, 7, 8)); } + + @Test + public void testBug338() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug338.jav"); + var clazz = classFiles.get("Bug338"); + var instance = clazz.getDeclaredConstructor().newInstance(); + } }