use of java.lang.invoke.MethodHandle in project neo4j by neo4j.
the class CodeGenerationTest method shouldGenerateMethodUsingOr.
@Test
public void shouldGenerateMethodUsingOr() throws Throwable {
// given
ClassHandle handle;
try (ClassGenerator simple = generateClass("SimpleClass")) {
try (CodeBlock conditional = simple.generateMethod(boolean.class, "conditional", param(boolean.class, "test1"), param(boolean.class, "test2"))) {
conditional.returns(or(conditional.load("test1"), conditional.load("test2")));
}
handle = simple.handle();
}
// when
MethodHandle conditional = instanceMethod(handle.newInstance(), "conditional", boolean.class, boolean.class);
// then
assertThat(conditional.invoke(true, true), equalTo(true));
assertThat(conditional.invoke(true, false), equalTo(true));
assertThat(conditional.invoke(false, true), equalTo(true));
assertThat(conditional.invoke(false, false), equalTo(false));
}
use of java.lang.invoke.MethodHandle in project neo4j by neo4j.
the class CodeGenerationTest method shouldHandleTernaryOnNonNullOperator.
@Test
public void shouldHandleTernaryOnNonNullOperator() throws Throwable {
// given
ClassHandle handle;
try (ClassGenerator simple = generateClass("SimpleClass")) {
try (CodeBlock ternaryBlock = simple.generateMethod(String.class, "ternary", param(Object.class, "test"), param(TernaryChecker.class, "check"))) {
ternaryBlock.returns(ternary(notNull(ternaryBlock.load("test")), invoke(ternaryBlock.load("check"), methodReference(TernaryChecker.class, String.class, "onTrue")), invoke(ternaryBlock.load("check"), methodReference(TernaryChecker.class, String.class, "onFalse"))));
}
handle = simple.handle();
}
// when
MethodHandle ternary = instanceMethod(handle.newInstance(), "ternary", Object.class, TernaryChecker.class);
// then
TernaryChecker checker1 = new TernaryChecker();
assertThat(ternary.invoke(new Object(), checker1), equalTo("on true"));
assertTrue(checker1.ranOnTrue);
assertFalse(checker1.ranOnFalse);
TernaryChecker checker2 = new TernaryChecker();
assertThat(ternary.invoke(null, checker2), equalTo("on false"));
assertFalse(checker2.ranOnTrue);
assertTrue(checker2.ranOnFalse);
}
use of java.lang.invoke.MethodHandle in project neo4j by neo4j.
the class CodeGenerationTest method shouldGenerateTryCatchWithNestedBlock.
@Test
public void shouldGenerateTryCatchWithNestedBlock() throws Throwable {
// given
ClassHandle handle;
try (ClassGenerator simple = generateClass("SimpleClass")) {
try (CodeBlock run = simple.generateMethod(void.class, "run", param(Runnable.class, "body"), param(Runnable.class, "catcher"), param(boolean.class, "test"))) {
run.tryCatch((tryBlock) -> {
try (CodeBlock ifBlock = tryBlock.ifStatement(run.load("test"))) {
ifBlock.expression(invoke(run.load("body"), RUN));
}
}, (catchBlock) -> catchBlock.expression(invoke(run.load("catcher"), RUN)), param(RuntimeException.class, "E"));
}
handle = simple.handle();
}
// when
Runnable runnable = mock(Runnable.class);
MethodHandle run = instanceMethod(handle.newInstance(), "run", Runnable.class, Runnable.class, boolean.class);
// then
run.invoke(runnable, mock(Runnable.class), false);
verify(runnable, never()).run();
run.invoke(runnable, mock(Runnable.class), true);
verify(runnable).run();
}
use of java.lang.invoke.MethodHandle in project neo4j by neo4j.
the class CodeGenerationTest method shouldGenerateTryWithNestedWhileIfLoop.
@Test
public void shouldGenerateTryWithNestedWhileIfLoop() throws Throwable {
// given
ClassHandle handle;
try (ClassGenerator simple = generateClass("SimpleClass")) {
try (CodeBlock callEach = simple.generateMethod(void.class, "callEach", param(TypeReference.parameterizedType(Iterator.class, Runnable.class), "targets"), param(boolean.class, "test"), param(Runnable.class, "runner"))) {
callEach.tryCatch((tryBlock) -> {
try (CodeBlock loop = tryBlock.whileLoop(invoke(callEach.load("targets"), methodReference(Iterator.class, boolean.class, "hasNext")))) {
try (CodeBlock doStuff = loop.ifStatement(not(callEach.load("test")))) {
doStuff.expression(invoke(doStuff.load("runner"), RUN));
}
loop.expression(invoke(Expression.cast(Runnable.class, invoke(callEach.load("targets"), methodReference(Iterator.class, Object.class, "next"))), methodReference(Runnable.class, void.class, "run")));
}
}, (catchBlock) -> catchBlock.expression(invoke(catchBlock.load("runner"), RUN)), param(RuntimeException.class, "e"));
}
handle = simple.handle();
}
Runnable a = mock(Runnable.class);
Runnable b = mock(Runnable.class);
Runnable c = mock(Runnable.class);
Runnable runner1 = mock(Runnable.class);
Runnable runner2 = mock(Runnable.class);
// when
MethodHandle callEach = instanceMethod(handle.newInstance(), "callEach", Iterator.class, boolean.class, Runnable.class);
callEach.invoke(Arrays.asList(a, b, c).iterator(), false, runner1);
callEach.invoke(Arrays.asList(a, b, c).iterator(), true, runner2);
// then
verify(runner1, times(3)).run();
verify(runner2, times(0)).run();
}
use of java.lang.invoke.MethodHandle in project neo4j by neo4j.
the class CodeGenerationTest method shouldHandleTernaryOperator.
@Test
public void shouldHandleTernaryOperator() throws Throwable {
// given
ClassHandle handle;
try (ClassGenerator simple = generateClass("SimpleClass")) {
try (CodeBlock ternaryBlock = simple.generateMethod(String.class, "ternary", param(boolean.class, "test"), param(TernaryChecker.class, "check"))) {
ternaryBlock.returns(ternary(ternaryBlock.load("test"), invoke(ternaryBlock.load("check"), methodReference(TernaryChecker.class, String.class, "onTrue")), invoke(ternaryBlock.load("check"), methodReference(TernaryChecker.class, String.class, "onFalse"))));
}
handle = simple.handle();
}
// when
MethodHandle ternary = instanceMethod(handle.newInstance(), "ternary", boolean.class, TernaryChecker.class);
// then
TernaryChecker checker1 = new TernaryChecker();
assertThat(ternary.invoke(true, checker1), equalTo("on true"));
assertTrue(checker1.ranOnTrue);
assertFalse(checker1.ranOnFalse);
TernaryChecker checker2 = new TernaryChecker();
assertThat(ternary.invoke(false, checker2), equalTo("on false"));
assertFalse(checker2.ranOnTrue);
assertTrue(checker2.ranOnFalse);
}
Aggregations