use of org.neo4j.codegen.ClassHandle in project neo4j by neo4j.
the class ProcedureCompilation method generateAggregator.
private static Class<?> generateAggregator(CodeGenerator codeGenerator, Method update, Method result, UserFunctionSignature signature) {
assert update.getDeclaringClass().equals(result.getDeclaringClass());
Class<?> userAggregatorClass = update.getDeclaringClass();
ClassHandle handle;
try (ClassGenerator generator = codeGenerator.generateClass(PACKAGE, "Aggregator" + userAggregatorClass.getSimpleName() + System.nanoTime(), UserAggregator.class)) {
FieldReference aggregator = generator.field(userAggregatorClass, "aggregator");
FieldReference context = generator.field(Context.class, "ctx");
// constructor
try (CodeBlock constructor = generator.generateConstructor(param(userAggregatorClass, "aggregator"), param(Context.class, "ctx"))) {
constructor.expression(invokeSuper(OBJECT));
constructor.put(constructor.self(), aggregator, constructor.load("aggregator"));
constructor.put(constructor.self(), context, constructor.load("ctx"));
}
// update
try (CodeBlock block = generator.generate(AGGREGATION_UPDATE)) {
block.tryCatch(onSuccess -> onSuccess.expression(invoke(get(onSuccess.self(), aggregator), methodReference(update), parameters(onSuccess, update, get(onSuccess.self(), context)))), onError -> onError(onError, format("function `%s`", signature.name())), param(Throwable.class, "T"));
}
// result
try (CodeBlock block = generator.generate(AGGREGATION_RESULT)) {
block.tryCatch(onSuccess -> onSuccess.returns(toAnyValue(invoke(get(onSuccess.self(), aggregator), methodReference(result)), result.getReturnType(), get(onSuccess.self(), context))), onError -> onError(onError, format("function `%s`", signature.name())), param(Throwable.class, "T"));
}
handle = generator.handle();
}
try {
return handle.loadClass();
} catch (CompilationFailureException e) {
// We are being called from a lambda so it'll have to do with runtime exceptions here
throw new RuntimeException("Failed to generate iterator", e);
}
}
Aggregations