From c07d4d36e92a630116ea609cd1e0b450dc396ba5 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 18 Mar 2024 12:14:03 +0100 Subject: [PATCH] Fix wrong type conversion, see #297 --- resources/bytecode/javFiles/Bug297.jav | 11 +++++++++++ src/main/java/de/dhbwstuttgart/bytecode/Codegen.java | 2 +- src/test/java/TestComplete.java | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 resources/bytecode/javFiles/Bug297.jav diff --git a/resources/bytecode/javFiles/Bug297.jav b/resources/bytecode/javFiles/Bug297.jav new file mode 100644 index 00000000..c4ff1e56 --- /dev/null +++ b/resources/bytecode/javFiles/Bug297.jav @@ -0,0 +1,11 @@ +import java.lang.Integer; + +public class Bug297 { + public static operation(func, a, b) { + return func.apply(a, b); + } + + public exec() { + return Foo.operation((x, y) -> x + y, 10, 10); + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index 2ff2d0a9..109d649c 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -966,8 +966,8 @@ public class Codegen { state.contextType = state.returnType; generate(state, ret.expression()); state.contextType = ctype; + boxPrimitive(state, ret.expression().type()); convertTo(state, ret.expression().type(), state.returnType); - boxPrimitive(state, state.returnType); mv.visitInsn(ARETURN); } else mv.visitInsn(RETURN); diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 1c031203..3d03e762 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -947,4 +947,12 @@ public class TestComplete { var clazz = classFiles.get("Bug296"); clazz.getDeclaredMethod("m1").invoke(null); } + + @Test + public void testBug297() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug297.jav"); + var clazz = classFiles.get("Bug297"); + var instance = clazz.getDeclaredConstructor().newInstance(); + clazz.getDeclaredMethod("exec").invoke(instance); + } }