diff --git a/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java b/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java index 0f601dcc4..337b35621 100644 --- a/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java +++ b/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java @@ -24,6 +24,7 @@ import static org.teavm.metaprogramming.Metaprogramming.emit; import static org.teavm.metaprogramming.Metaprogramming.exit; import static org.teavm.metaprogramming.Metaprogramming.findClass; import static org.teavm.metaprogramming.Metaprogramming.lazy; +import java.util.function.Consumer; import org.junit.Test; import org.junit.runner.RunWith; import org.teavm.junit.SkipJVM; @@ -350,6 +351,50 @@ public class MetaprogrammingTest { exit(() -> type.get()); } + @Test + public void conditionalActionWorks() { + class TypeConsumer implements Consumer { + String type; + @Override public void accept(String t) { + type = t; + } + } + TypeConsumer consumer = new TypeConsumer(); + + fieldType(Context.class, "a", consumer); + assertEquals("int", consumer.type); + + fieldType(Context.class, "b", consumer); + assertEquals("int", consumer.type); + + fieldType(Context.class, "c", consumer); + assertNull(consumer.type); + } + + @Meta + private static native void fieldType(Class cls, String name, Consumer typeConsumer); + private static void fieldType(ReflectClass cls, Value name, Value> typeConsumer) { + Value result = lazy(() -> { + typeConsumer.get().accept(null); + return null; + }); + for (ReflectField field : cls.getDeclaredFields()) { + String type = field.getType().getName(); + String fieldName = field.getName(); + Value existing = result; + result = lazy(() -> { + if (fieldName.equals(name.get())) { + typeConsumer.get().accept(type); + return null; + } else { + return existing.get(); + } + }); + } + Value type = result; + emit(() -> type); + } + static class Context { public int a; public int b;