From e31f1c59e1b2136a1b012c36f375b29c4cbd6b6b Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Fri, 27 Oct 2023 14:27:47 +0200 Subject: [PATCH] Examples --- resources/bytecode/javFiles/Chain.jav | 13 +++++++++++++ .../bytecode/javFiles/FunctionalInterface.jav | 15 +++++++++++++++ .../target/tree/expression/TargetForEach.java | 2 -- src/test/java/TestComplete.java | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 resources/bytecode/javFiles/Chain.jav create mode 100644 resources/bytecode/javFiles/FunctionalInterface.jav diff --git a/resources/bytecode/javFiles/Chain.jav b/resources/bytecode/javFiles/Chain.jav new file mode 100644 index 00000000..c6b034a3 --- /dev/null +++ b/resources/bytecode/javFiles/Chain.jav @@ -0,0 +1,13 @@ +import java.lang.Integer; + +public class Chain { + x = 5; + + chain() { + return this; + } + + m() { + return this.chain().chain().chain().x; + } +} \ No newline at end of file diff --git a/resources/bytecode/javFiles/FunctionalInterface.jav b/resources/bytecode/javFiles/FunctionalInterface.jav new file mode 100644 index 00000000..62120fa2 --- /dev/null +++ b/resources/bytecode/javFiles/FunctionalInterface.jav @@ -0,0 +1,15 @@ +import java.lang.Integer; +import java.util.function.Function; + +public class FunctionalInterface { + Integer accept(Function f) { + return f.apply(20); + } + + Integer m() { + var v = accept(i -> { + return i * 10; + }); + return v; + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetForEach.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetForEach.java index ce50403c..ce4fa10b 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetForEach.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetForEach.java @@ -1,6 +1,4 @@ package de.dhbwstuttgart.target.tree.expression; -import de.dhbwstuttgart.target.tree.type.TargetType; - public record TargetForEach(TargetExpression vardecl, TargetExpression list) implements TargetExpression { } diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index dbdfff2d..e4e5849b 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -736,4 +736,22 @@ public class TestComplete { var m = clazz.getDeclaredMethod("m", Integer.class); assertEquals(m.invoke(instance, 10), 60); } + + @Test + public void testFunctionalInterface() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "FunctionalInterface.jav"); + var clazz = classFiles.get("FunctionalInterface"); + var instance = clazz.getDeclaredConstructor().newInstance(); + var m = clazz.getDeclaredMethod("m", Integer.class); + assertEquals(m.invoke(instance, 20), 400); + } + + @Test + public void testChain() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Chain.jav"); + var clazz = classFiles.get("Chain"); + var instance = clazz.getDeclaredConstructor().newInstance(); + var m = clazz.getDeclaredMethod("m"); + assertEquals(m.invoke(instance), 5); + } }