Search in sources :

Example 21 with UserFunctionSignature

use of org.neo4j.internal.kernel.api.procs.UserFunctionSignature in project neo4j by neo4j.

the class ProcedureCompiler method compileAggregationFunction.

private CallableUserAggregationFunction compileAggregationFunction(Class<?> definition, Method create, QualifiedName funcName) throws ProcedureException {
    restrictions.verify(funcName);
    // find update and result method
    Method update = null;
    Method result = null;
    Class<?> aggregator = create.getReturnType();
    for (Method m : aggregator.getDeclaredMethods()) {
        if (m.isAnnotationPresent(UserAggregationUpdate.class)) {
            if (update != null) {
                throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Class '%s' contains multiple methods annotated with '@%s'.", aggregator.getSimpleName(), UserAggregationUpdate.class.getSimpleName());
            }
            update = m;
        }
        if (m.isAnnotationPresent(UserAggregationResult.class)) {
            if (result != null) {
                throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Class '%s' contains multiple methods annotated with '@%s'.", aggregator.getSimpleName(), UserAggregationResult.class.getSimpleName());
            }
            result = m;
        }
    }
    if (result == null || update == null) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Class '%s' must contain methods annotated with both '@%s' as well as '@%s'.", aggregator.getSimpleName(), UserAggregationResult.class.getSimpleName(), UserAggregationUpdate.class.getSimpleName());
    }
    if (update.getReturnType() != void.class) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Update method '%s' in %s has type '%s' but must have return type 'void'.", update.getName(), aggregator.getSimpleName(), update.getReturnType().getSimpleName());
    }
    if (!isPublic(create.getModifiers())) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Aggregation method '%s' in %s must be public.", create.getName(), definition.getSimpleName());
    }
    if (!isPublic(aggregator.getModifiers())) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Aggregation class '%s' must be public.", aggregator.getSimpleName());
    }
    if (!isPublic(update.getModifiers())) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Aggregation update method '%s' in %s must be public.", update.getName(), aggregator.getSimpleName());
    }
    if (!isPublic(result.getModifiers())) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Aggregation result method '%s' in %s must be public.", result.getName(), aggregator.getSimpleName());
    }
    List<FieldSignature> inputSignature = inputSignatureDeterminer.signatureFor(update);
    Class<?> returnType = result.getReturnType();
    TypeCheckers.TypeChecker valueConverter = typeCheckers.checkerFor(returnType);
    String description = description(create);
    UserAggregationFunction function = create.getAnnotation(UserAggregationFunction.class);
    String deprecated = deprecated(create, function::deprecatedBy, "Use of @UserAggregationFunction(deprecatedBy) without @Deprecated in " + funcName);
    List<FieldSetter> setters = allFieldInjections.setters(definition);
    if (!config.fullAccessFor(funcName.toString())) {
        try {
            setters = safeFieldInjections.setters(definition);
        } catch (ComponentInjectionException e) {
            description = describeAndLogLoadFailure(funcName);
            UserFunctionSignature signature = new UserFunctionSignature(funcName, inputSignature, valueConverter.type(), deprecated, config.rolesFor(funcName.toString()), description, null, false);
            return new FailedLoadAggregatedFunction(signature);
        }
    }
    UserFunctionSignature signature = new UserFunctionSignature(funcName, inputSignature, valueConverter.type(), deprecated, config.rolesFor(funcName.toString()), description, null, false);
    return ProcedureCompilation.compileAggregation(signature, setters, create, update, result);
}
Also used : UserAggregationResult(org.neo4j.procedure.UserAggregationResult) UserAggregationUpdate(org.neo4j.procedure.UserAggregationUpdate) FieldSignature(org.neo4j.internal.kernel.api.procs.FieldSignature) Method(java.lang.reflect.Method) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) CallableUserAggregationFunction(org.neo4j.kernel.api.procedure.CallableUserAggregationFunction) UserAggregationFunction(org.neo4j.procedure.UserAggregationFunction) FailedLoadAggregatedFunction(org.neo4j.kernel.api.procedure.FailedLoadAggregatedFunction) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException)

Example 22 with UserFunctionSignature

use of org.neo4j.internal.kernel.api.procs.UserFunctionSignature in project neo4j by neo4j.

the class ProcedureRegistry method register.

/**
 * Register a new function.
 *
 * @param function the function.
 */
public void register(CallableUserFunction function, boolean overrideCurrentImplementation, boolean builtIn) throws ProcedureException {
    UserFunctionSignature signature = function.signature();
    QualifiedName name = signature.name();
    if (aggregationFunctions.get(name) != null) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register function, because the name `%s` is already in use as an aggregation function.", name);
    }
    CallableUserFunction oldImplementation = functions.get(name);
    if (oldImplementation == null) {
        functions.put(name, function, signature.caseInsensitive());
    } else {
        if (overrideCurrentImplementation) {
            functions.put(name, function, signature.caseInsensitive());
        } else {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register function, because the name `%s` is already in use.", name);
        }
    }
    if (builtIn) {
        builtInFunctionIds.add(functions.idOf(name));
    }
}
Also used : CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature)

Aggregations

UserFunctionSignature (org.neo4j.internal.kernel.api.procs.UserFunctionSignature)22 Test (org.junit.jupiter.api.Test)16 CallableUserFunction (org.neo4j.kernel.api.procedure.CallableUserFunction)13 AnyValue (org.neo4j.values.AnyValue)7 CallableUserAggregationFunction (org.neo4j.kernel.api.procedure.CallableUserAggregationFunction)6 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)5 UserAggregator (org.neo4j.internal.kernel.api.procs.UserAggregator)4 Method (java.lang.reflect.Method)3 NTString (org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTString)3 Arrays.asList (java.util.Arrays.asList)2 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 ClassGenerator (org.neo4j.codegen.ClassGenerator)2 ClassHandle (org.neo4j.codegen.ClassHandle)2 CodeBlock (org.neo4j.codegen.CodeBlock)2 CodeGenerator (org.neo4j.codegen.CodeGenerator)2 FieldReference (org.neo4j.codegen.FieldReference)2 FieldSignature (org.neo4j.internal.kernel.api.procs.FieldSignature)2 NTList (org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTList)2