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());
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations