Search in sources :

Example 26 with MethodHandle

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));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 27 with MethodHandle

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);
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 28 with MethodHandle

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();
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 29 with MethodHandle

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();
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 30 with MethodHandle

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);
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)300 Test (org.junit.Test)101 MethodType (java.lang.invoke.MethodType)43 Type (com.facebook.presto.spi.type.Type)37 Method (java.lang.reflect.Method)18 OperatorType (com.facebook.presto.spi.function.OperatorType)14 MethodHandles (java.lang.invoke.MethodHandles)12 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)11 Signature (com.facebook.presto.metadata.Signature)10 CallSite (java.lang.invoke.CallSite)10 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)9 ImmutableList (com.google.common.collect.ImmutableList)9 List (java.util.List)8 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)7 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)6 Parameter (com.facebook.presto.bytecode.Parameter)6 PrestoException (com.facebook.presto.spi.PrestoException)6 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)6 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)6