use of java.lang.invoke.MethodHandle in project neo4j by neo4j.
the class CodeGenerationTest method shouldHandleNot.
@Test
public void shouldHandleNot() throws Throwable {
// given
ClassHandle handle;
try (ClassGenerator simple = generateClass("SimpleClass")) {
try (CodeBlock conditional = simple.generateMethod(boolean.class, "conditional", param(boolean.class, "test"))) {
conditional.returns(not(conditional.load("test")));
}
handle = simple.handle();
}
// when
MethodHandle conditional = instanceMethod(handle.newInstance(), "conditional", boolean.class);
// then
assertThat(conditional.invoke(true), equalTo(false));
assertThat(conditional.invoke(false), equalTo(true));
}
use of java.lang.invoke.MethodHandle in project neo4j by neo4j.
the class CodeGenerationTest method shouldGenerateTryCatch.
@Test
public void shouldGenerateTryCatch() 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"))) {
run.tryCatch(body -> body.expression(invoke(run.load("body"), RUN)), handler -> handler.expression(invoke(run.load("catcher"), RUN)), param(RuntimeException.class, "E"));
}
handle = simple.handle();
}
// when
Runnable successBody = mock(Runnable.class), failBody = mock(Runnable.class), successCatch = mock(Runnable.class), failCatch = mock(Runnable.class);
RuntimeException theFailure = new RuntimeException();
doThrow(theFailure).when(failBody).run();
MethodHandle run = instanceMethod(handle.newInstance(), "run", Runnable.class, Runnable.class);
//success
run.invoke(successBody, successCatch);
verify(successBody).run();
verify(successCatch, never()).run();
//failure
run.invoke(failBody, failCatch);
InOrder orderFailure = inOrder(failBody, failCatch);
orderFailure.verify(failBody).run();
orderFailure.verify(failCatch).run();
}
use of java.lang.invoke.MethodHandle in project neo4j by neo4j.
the class CodeGenerationTest method addForType.
@SuppressWarnings("unchecked")
private <T> T addForType(Class<T> clazz, T lhs, T rhs) throws Throwable {
// given
createGenerator();
ClassHandle handle;
try (ClassGenerator simple = generateClass("SimpleClass")) {
try (CodeBlock block = simple.generateMethod(clazz, "add", param(clazz, "a"), param(clazz, "b"))) {
block.returns(add(block.load("a"), block.load("b")));
}
handle = simple.handle();
}
// when
MethodHandle add = instanceMethod(handle.newInstance(), "add", clazz, clazz);
// then
return (T) add.invoke(lhs, rhs);
}
use of java.lang.invoke.MethodHandle in project neo4j by neo4j.
the class CodeGenerationTest method shouldGenerateIfStatement.
@Test
public void shouldGenerateIfStatement() throws Throwable {
// given
ClassHandle handle;
try (ClassGenerator simple = generateClass("SimpleClass")) {
try (CodeBlock conditional = simple.generateMethod(void.class, "conditional", param(boolean.class, "test"), param(Runnable.class, "runner"))) {
try (CodeBlock doStuff = conditional.ifStatement(conditional.load("test"))) {
doStuff.expression(invoke(doStuff.load("runner"), RUN));
}
}
handle = simple.handle();
}
Runnable runner1 = mock(Runnable.class);
Runnable runner2 = mock(Runnable.class);
// when
MethodHandle conditional = instanceMethod(handle.newInstance(), "conditional", boolean.class, Runnable.class);
conditional.invoke(true, runner1);
conditional.invoke(false, runner2);
// then
verify(runner1).run();
verifyZeroInteractions(runner2);
}
use of java.lang.invoke.MethodHandle in project neo4j by neo4j.
the class CodeGenerationTest method shouldGenerateTryAndMultipleCatch.
@Test
public void shouldGenerateTryAndMultipleCatch() 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, "catcher1"), param(Runnable.class, "catcher2"))) {
run.tryCatch((tryBlock) -> tryBlock.tryCatch((innerTry) -> innerTry.expression(invoke(run.load("body"), RUN)), (catchBlock1) -> catchBlock1.expression(invoke(run.load("catcher1"), RUN)), param(MyFirstException.class, "E")), (catchBlock2) -> catchBlock2.expression(invoke(run.load("catcher2"), RUN)), param(MySecondException.class, "E"));
}
handle = simple.handle();
}
// when
Runnable body1 = mock(Runnable.class), body2 = mock(Runnable.class), catcher11 = mock(Runnable.class), catcher12 = mock(Runnable.class), catcher21 = mock(Runnable.class), catcher22 = mock(Runnable.class);
doThrow(MyFirstException.class).when(body1).run();
doThrow(MySecondException.class).when(body2).run();
MethodHandle run = instanceMethod(handle.newInstance(), "run", Runnable.class, Runnable.class, Runnable.class);
run.invoke(body1, catcher11, catcher12);
verify(body1).run();
verify(catcher11).run();
verify(catcher12, never()).run();
run.invoke(body2, catcher21, catcher22);
verify(body2).run();
verify(catcher22).run();
verify(catcher21, never()).run();
}
Aggregations