From f66b9099f37000c0cee0c9735bd5f7772984e230 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 8 Apr 2024 13:51:49 +0200 Subject: [PATCH] Fix #311 --- resources/bytecode/javFiles/Bug311.jav | 10 ++++++++++ .../dhbwstuttgart/target/generate/ASTToTargetAST.java | 3 ++- src/test/java/TestComplete.java | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 resources/bytecode/javFiles/Bug311.jav diff --git a/resources/bytecode/javFiles/Bug311.jav b/resources/bytecode/javFiles/Bug311.jav new file mode 100644 index 00000000..e8ccb7f7 --- /dev/null +++ b/resources/bytecode/javFiles/Bug311.jav @@ -0,0 +1,10 @@ +import java.lang.String; + +public class Bug311 { + Bug311A i = new Bug311A(); + public toString() { + return i.toString(); + } +} + +class Bug311A {} diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index b9774f08..a6f42d80 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -79,8 +79,9 @@ public class ASTToTargetAST { Optional findMethod(ClassOrInterface owner, String name, List argumentList) { Optional method = Optional.empty(); - while (method.isEmpty() && !owner.getClassName().toString().equals("java.lang.Object")) { + while (method.isEmpty()) { method = owner.getMethods().stream().filter(m -> m.name.equals(name) && parameterEquals(m.getParameterList(), argumentList)).findFirst(); + if (owner.getClassName().toString().equals("java.lang.Object")) break; owner = compiler.getClass(owner.getSuperClass().getName()); } return method; diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index a5c38489..d6c90703 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -1040,6 +1040,14 @@ public class TestComplete { assertEquals(clazz.getDeclaredMethod("toString").invoke(instance), "3"); } + @Test + public void testBug311() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug311.jav"); + var clazz = classFiles.get("Bug311"); + var instance = clazz.getDeclaredConstructor().newInstance(); + clazz.getDeclaredMethod("toString").invoke(instance); + } + @Test public void testBug312() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug312.jav");