Search in sources :

Example 16 with MethodHandle

use of java.lang.invoke.MethodHandle in project neo4j by neo4j.

the class FieldInjections method createInjector.

private FieldSetter createInjector(Class<?> cls, Field field) throws ProcedureException {
    try {
        ComponentRegistry.Provider<?> provider = components.providerFor(field.getType());
        if (provider == null) {
            throw new ComponentInjectionException(Status.Procedure.ProcedureRegistrationFailed, "Unable to set up injection for procedure `%s`, the field `%s` " + "has type `%s` which is not a known injectable component.", cls.getSimpleName(), field.getName(), field.getType());
        }
        MethodHandle setter = MethodHandles.lookup().unreflectSetter(field);
        return new FieldSetter(field, setter, provider);
    } catch (IllegalAccessException e) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to set up injection for `%s`, failed to access field `%s`: %s", e, cls.getSimpleName(), field.getName(), e.getMessage());
    }
}
Also used : ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException) MethodHandle(java.lang.invoke.MethodHandle)

Example 17 with MethodHandle

use of java.lang.invoke.MethodHandle in project neo4j by neo4j.

the class OutputMappers method mapper.

public OutputMapper mapper(Class<?> userClass) throws ProcedureException {
    assertIsValidRecordClass(userClass);
    List<Field> fields = instanceFields(userClass);
    FieldSignature[] signature = new FieldSignature[fields.size()];
    FieldMapper[] fieldMappers = new FieldMapper[fields.size()];
    for (int i = 0; i < fields.size(); i++) {
        Field field = fields.get(i);
        if (!isPublic(field.getModifiers())) {
            throw new ProcedureException(Status.Procedure.TypeError, "Field `%s` in record `%s` cannot be accessed. Please ensure the field is marked as `public`.", field.getName(), userClass.getSimpleName());
        }
        try {
            TypeMappers.NeoValueConverter mapper = typeMappers.converterFor(field.getGenericType());
            MethodHandle getter = lookup.unreflectGetter(field);
            FieldMapper fieldMapper = new FieldMapper(getter, mapper);
            fieldMappers[i] = fieldMapper;
            signature[i] = new FieldSignature(field.getName(), mapper.type());
        } catch (ProcedureException e) {
            throw new ProcedureException(e.status(), e, "Field `%s` in record `%s` cannot be converted to a Neo4j type: %s", field.getName(), userClass.getSimpleName(), e.getMessage());
        } catch (IllegalAccessException e) {
            throw new ProcedureException(Status.Procedure.TypeError, e, "Field `%s` in record `%s` cannot be accessed: %s", field.getName(), userClass.getSimpleName(), e.getMessage());
        }
    }
    return new OutputMapper(signature, fieldMappers);
}
Also used : FieldSignature(org.neo4j.kernel.api.proc.FieldSignature) Field(java.lang.reflect.Field) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) MethodHandle(java.lang.invoke.MethodHandle)

Example 18 with MethodHandle

use of java.lang.invoke.MethodHandle in project neo4j by neo4j.

the class CodeGenerationTest method shouldGenerateIfNonNullStatement.

@Test
public void shouldGenerateIfNonNullStatement() throws Throwable {
    // given
    ClassHandle handle;
    try (ClassGenerator simple = generateClass("SimpleClass")) {
        try (CodeBlock conditional = simple.generateMethod(void.class, "conditional", param(Object.class, "test"), param(Runnable.class, "runner"))) {
            try (CodeBlock doStuff = conditional.ifStatement(notNull(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", Object.class, Runnable.class);
    conditional.invoke(new Object(), runner1);
    conditional.invoke(null, runner2);
    // then
    verify(runner1).run();
    verifyZeroInteractions(runner2);
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 19 with MethodHandle

use of java.lang.invoke.MethodHandle in project neo4j by neo4j.

the class CodeGenerationTest method shouldGenerateIfEqualsStatement.

@Test
public void shouldGenerateIfEqualsStatement() throws Throwable {
    // given
    ClassHandle handle;
    try (ClassGenerator simple = generateClass("SimpleClass")) {
        try (CodeBlock conditional = simple.generateMethod(void.class, "conditional", param(Object.class, "lhs"), param(Object.class, "rhs"), param(Runnable.class, "runner"))) {
            try (CodeBlock doStuff = conditional.ifStatement(equal(conditional.load("lhs"), conditional.load("rhs")))) {
                doStuff.expression(invoke(doStuff.load("runner"), RUN));
            }
        }
        handle = simple.handle();
    }
    Runnable runner1 = mock(Runnable.class);
    Runnable runner2 = mock(Runnable.class);
    Object a = "a", b = "b";
    // when
    MethodHandle conditional = instanceMethod(handle.newInstance(), "conditional", Object.class, Object.class, Runnable.class);
    conditional.invoke(a, b, runner1);
    conditional.invoke(a, a, runner2);
    // then
    verify(runner2).run();
    verifyZeroInteractions(runner1);
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Test(org.junit.Test)

Example 20 with MethodHandle

use of java.lang.invoke.MethodHandle in project neo4j by neo4j.

the class CodeGenerationTest method shouldGenerateNestedWhileLoop.

@Test
public void shouldGenerateNestedWhileLoop() 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"))) {
            try (CodeBlock loop = callEach.whileLoop(invoke(callEach.load("targets"), methodReference(Iterator.class, boolean.class, "hasNext")))) {
                try (CodeBlock inner = loop.whileLoop(invoke(callEach.load("targets"), methodReference(Iterator.class, boolean.class, "hasNext")))) {
                    inner.expression(invoke(Expression.cast(Runnable.class, invoke(callEach.load("targets"), methodReference(Iterator.class, Object.class, "next"))), methodReference(Runnable.class, void.class, "run")));
                }
            }
        }
        handle = simple.handle();
    }
    Runnable a = mock(Runnable.class);
    Runnable b = mock(Runnable.class);
    Runnable c = mock(Runnable.class);
    // when
    MethodHandle callEach = instanceMethod(handle.newInstance(), "callEach", Iterator.class);
    callEach.invoke(Arrays.asList(a, b, c).iterator());
    // then
    InOrder order = inOrder(a, b, c);
    order.verify(a).run();
    order.verify(b).run();
    order.verify(c).run();
    verifyNoMoreInteractions(a, b, c);
}
Also used : InOrder(org.mockito.InOrder) 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