Search in sources :

Example 21 with MethodHandle

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

Example 22 with MethodHandle

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

Example 23 with MethodHandle

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

Example 24 with MethodHandle

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

Example 25 with MethodHandle

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();
}
Also used : ByteCode(org.neo4j.codegen.bytecode.ByteCode) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) Expression.or(org.neo4j.codegen.Expression.or) Expression.multiply(org.neo4j.codegen.Expression.multiply) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) Mockito.doThrow(org.mockito.Mockito.doThrow) TypeReference.typeReference(org.neo4j.codegen.TypeReference.typeReference) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) ExpressionTemplate.self(org.neo4j.codegen.ExpressionTemplate.self) Assert.fail(org.junit.Assert.fail) Expression.ternary(org.neo4j.codegen.Expression.ternary) Parameterized(org.junit.runners.Parameterized) Expression.invoke(org.neo4j.codegen.Expression.invoke) Expression.and(org.neo4j.codegen.Expression.and) SourceCode(org.neo4j.codegen.source.SourceCode) Expression.not(org.neo4j.codegen.Expression.not) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Expression.newInstance(org.neo4j.codegen.Expression.newInstance) ExpressionTemplate.load(org.neo4j.codegen.ExpressionTemplate.load) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.inOrder(org.mockito.Mockito.inOrder) Expression.subtract(org.neo4j.codegen.Expression.subtract) MethodReference.constructorReference(org.neo4j.codegen.MethodReference.constructorReference) Expression.constant(org.neo4j.codegen.Expression.constant) TypeReference.extending(org.neo4j.codegen.TypeReference.extending) Expression.equal(org.neo4j.codegen.Expression.equal) Mockito.mock(org.mockito.Mockito.mock) MethodHandle(java.lang.invoke.MethodHandle) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) RunWith(org.junit.runner.RunWith) Function(java.util.function.Function) TypeReference.typeParameter(org.neo4j.codegen.TypeReference.typeParameter) Expression.isNull(org.neo4j.codegen.Expression.isNull) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Expression.add(org.neo4j.codegen.Expression.add) MethodReference.methodReference(org.neo4j.codegen.MethodReference.methodReference) Before(org.junit.Before) TypeReference.parameterizedType(org.neo4j.codegen.TypeReference.parameterizedType) InOrder(org.mockito.InOrder) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Parameter.param(org.neo4j.codegen.Parameter.param) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Field(java.lang.reflect.Field) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) Expression.notNull(org.neo4j.codegen.Expression.notNull) Expression.newArray(org.neo4j.codegen.Expression.newArray) ExpressionTemplate.cast(org.neo4j.codegen.ExpressionTemplate.cast) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) 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