Search in sources :

Example 1 with FieldSignature

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

the class ProcedureCompiler method compileProcedure.

private CallableProcedure compileProcedure(Class<?> procDefinition, Method method, String warning, boolean fullAccess, QualifiedName procName) throws ProcedureException {
    List<FieldSignature> inputSignature = inputSignatureDeterminer.signatureFor(method);
    List<FieldSignature> outputSignature = outputSignatureCompiler.fieldSignatures(method);
    String description = description(method);
    Procedure procedure = method.getAnnotation(Procedure.class);
    Mode mode = procedure.mode();
    boolean admin = method.isAnnotationPresent(Admin.class);
    boolean systemProcedure = method.isAnnotationPresent(SystemProcedure.class);
    boolean allowExpiredCredentials = systemProcedure ? method.getAnnotation(SystemProcedure.class).allowExpiredCredentials() : false;
    boolean internal = method.isAnnotationPresent(Internal.class);
    String deprecated = deprecated(method, procedure::deprecatedBy, "Use of @Procedure(deprecatedBy) without @Deprecated in " + procName);
    List<FieldSetter> setters = allFieldInjections.setters(procDefinition);
    if (!fullAccess && !config.fullAccessFor(procName.toString())) {
        try {
            setters = safeFieldInjections.setters(procDefinition);
        } catch (ComponentInjectionException e) {
            description = describeAndLogLoadFailure(procName);
            ProcedureSignature signature = new ProcedureSignature(procName, inputSignature, outputSignature, Mode.DEFAULT, admin, null, new String[0], description, warning, procedure.eager(), false, systemProcedure, internal, allowExpiredCredentials);
            return new FailedLoadProcedure(signature);
        }
    }
    ProcedureSignature signature = new ProcedureSignature(procName, inputSignature, outputSignature, mode, admin, deprecated, config.rolesFor(procName.toString()), description, warning, procedure.eager(), false, systemProcedure, internal, allowExpiredCredentials);
    return ProcedureCompilation.compileProcedure(signature, setters, method);
}
Also used : ProcedureSignature(org.neo4j.internal.kernel.api.procs.ProcedureSignature) FailedLoadProcedure(org.neo4j.kernel.api.procedure.FailedLoadProcedure) Mode(org.neo4j.procedure.Mode) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure) FailedLoadProcedure(org.neo4j.kernel.api.procedure.FailedLoadProcedure) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) FieldSignature(org.neo4j.internal.kernel.api.procs.FieldSignature) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException)

Example 2 with FieldSignature

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

the class ProcedureCompiler method compileFunction.

private CallableUserFunction compileFunction(Class<?> procDefinition, Method method, QualifiedName procName) throws ProcedureException {
    restrictions.verify(procName);
    List<FieldSignature> inputSignature = inputSignatureDeterminer.signatureFor(method);
    Class<?> returnType = method.getReturnType();
    TypeCheckers.TypeChecker typeChecker = typeCheckers.checkerFor(returnType);
    String description = description(method);
    UserFunction function = method.getAnnotation(UserFunction.class);
    String deprecated = deprecated(method, function::deprecatedBy, "Use of @UserFunction(deprecatedBy) without @Deprecated in " + procName);
    List<FieldSetter> setters = allFieldInjections.setters(procDefinition);
    if (!config.fullAccessFor(procName.toString())) {
        try {
            setters = safeFieldInjections.setters(procDefinition);
        } catch (ComponentInjectionException e) {
            description = describeAndLogLoadFailure(procName);
            UserFunctionSignature signature = new UserFunctionSignature(procName, inputSignature, typeChecker.type(), deprecated, config.rolesFor(procName.toString()), description, null, false);
            return new FailedLoadFunction(signature);
        }
    }
    UserFunctionSignature signature = new UserFunctionSignature(procName, inputSignature, typeChecker.type(), deprecated, config.rolesFor(procName.toString()), description, null, false);
    return ProcedureCompilation.compileFunction(signature, setters, method);
}
Also used : FieldSignature(org.neo4j.internal.kernel.api.procs.FieldSignature) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) FailedLoadFunction(org.neo4j.kernel.api.procedure.FailedLoadFunction) CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) UserFunction(org.neo4j.procedure.UserFunction) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException)

Example 3 with FieldSignature

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

the class MethodSignatureCompilerTest method shouldMapSimpleRecordWithString.

@Test
void shouldMapSimpleRecordWithString() throws Throwable {
    // When
    Method echo = ClassWithProcedureWithSimpleArgs.class.getMethod("echo", String.class);
    List<FieldSignature> signature = new MethodSignatureCompiler(new TypeCheckers()).signatureFor(echo);
    // THen
    assertThat(signature).containsExactly(FieldSignature.inputField("name", Neo4jTypes.NTString));
}
Also used : FieldSignature(org.neo4j.internal.kernel.api.procs.FieldSignature) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 4 with FieldSignature

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

the class ProcedureOutputSignatureCompiler method fieldSignatures.

List<FieldSignature> fieldSignatures(Class<?> userClass) throws ProcedureException {
    assertIsValidRecordClass(userClass);
    List<Field> fields = instanceFields(userClass);
    FieldSignature[] signature = new FieldSignature[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 {
            TypeCheckers.TypeChecker checker = typeCheckers.checkerFor(field.getGenericType());
            signature[i] = FieldSignature.outputField(field.getName(), checker.type(), field.isAnnotationPresent(Deprecated.class));
        } 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());
        }
    }
    return Arrays.asList(signature);
}
Also used : Field(java.lang.reflect.Field) FieldSignature(org.neo4j.internal.kernel.api.procs.FieldSignature) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException)

Example 5 with FieldSignature

use of org.neo4j.internal.kernel.api.procs.FieldSignature 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)

Aggregations

FieldSignature (org.neo4j.internal.kernel.api.procs.FieldSignature)6 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)3 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)3 Method (java.lang.reflect.Method)2 UserFunctionSignature (org.neo4j.internal.kernel.api.procs.UserFunctionSignature)2 Field (java.lang.reflect.Field)1 Parameter (java.lang.reflect.Parameter)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1 DefaultParameterValue (org.neo4j.internal.kernel.api.procs.DefaultParameterValue)1 ProcedureSignature (org.neo4j.internal.kernel.api.procs.ProcedureSignature)1 CallableProcedure (org.neo4j.kernel.api.procedure.CallableProcedure)1 CallableUserAggregationFunction (org.neo4j.kernel.api.procedure.CallableUserAggregationFunction)1 CallableUserFunction (org.neo4j.kernel.api.procedure.CallableUserFunction)1 FailedLoadAggregatedFunction (org.neo4j.kernel.api.procedure.FailedLoadAggregatedFunction)1 FailedLoadFunction (org.neo4j.kernel.api.procedure.FailedLoadFunction)1 FailedLoadProcedure (org.neo4j.kernel.api.procedure.FailedLoadProcedure)1 SystemProcedure (org.neo4j.kernel.api.procedure.SystemProcedure)1 Mode (org.neo4j.procedure.Mode)1