Search in sources :

Example 1 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException 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 2 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class MethodSignatureCompiler method signatureFor.

public List<FieldSignature> signatureFor(Method method) throws ProcedureException {
    Parameter[] params = method.getParameters();
    Type[] types = method.getGenericParameterTypes();
    List<FieldSignature> signature = new ArrayList<>(params.length);
    boolean seenDefault = false;
    for (int i = 0; i < params.length; i++) {
        Parameter param = params[i];
        Type type = types[i];
        if (!param.isAnnotationPresent(Name.class)) {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Argument at position %d in method `%s` is missing an `@%s` annotation.%n" + "Please add the annotation, recompile the class and try again.", i, method.getName(), Name.class.getSimpleName());
        }
        Name parameter = param.getAnnotation(Name.class);
        String name = parameter.value();
        if (name.trim().length() == 0) {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Argument at position %d in method `%s` is annotated with a name,%n" + "but the name is empty, please provide a non-empty name for the argument.", i, method.getName());
        }
        try {
            NeoValueConverter valueConverter = typeMappers.converterFor(type);
            Optional<Neo4jValue> defaultValue = valueConverter.defaultValue(parameter);
            //it is not allowed to have holes in default values
            if (seenDefault && !defaultValue.isPresent()) {
                throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Non-default argument at position %d with name %s in method %s follows default argument. " + "Add a default value or rearrange arguments so that the non-default values comes first.", i, parameter.value(), method.getName());
            }
            seenDefault = defaultValue.isPresent();
            signature.add(new FieldSignature(name, valueConverter.type(), defaultValue));
        } catch (ProcedureException e) {
            throw new ProcedureException(e.status(), "Argument `%s` at position %d in `%s` with%n" + "type `%s` cannot be converted to a Neo4j type: %s", name, i, method.getName(), param.getType().getSimpleName(), e.getMessage());
        }
    }
    return signature;
}
Also used : NeoValueConverter(org.neo4j.kernel.impl.proc.TypeMappers.NeoValueConverter) ArrayList(java.util.ArrayList) FieldSignature(org.neo4j.kernel.api.proc.FieldSignature) Name(org.neo4j.procedure.Name) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException)

Example 3 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException 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 4 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException 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) throws ProcedureException {
    UserFunctionSignature signature = function.signature();
    QualifiedName name = signature.name();
    CallableUserFunction oldImplementation = functions.get(name);
    if (oldImplementation == null) {
        functions.put(name, function);
    } else {
        if (overrideCurrentImplementation) {
            functions.put(name, function);
        } else {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register function, because the name `%s` is already in use.", name);
        }
    }
}
Also used : CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) QualifiedName(org.neo4j.kernel.api.proc.QualifiedName) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) UserFunctionSignature(org.neo4j.kernel.api.proc.UserFunctionSignature)

Example 5 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ProcedureRegistry method register.

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

Aggregations

ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)49 Test (org.junit.Test)28 CallableProcedure (org.neo4j.kernel.api.proc.CallableProcedure)14 BasicContext (org.neo4j.kernel.api.proc.BasicContext)10 MethodHandle (java.lang.invoke.MethodHandle)8 ArrayList (java.util.ArrayList)8 CallableUserFunction (org.neo4j.kernel.api.proc.CallableUserFunction)8 FieldSignature (org.neo4j.kernel.api.proc.FieldSignature)8 QualifiedName (org.neo4j.kernel.api.proc.QualifiedName)8 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)7 UserFunctionSignature (org.neo4j.kernel.api.proc.UserFunctionSignature)7 RawIterator (org.neo4j.collection.RawIterator)6 Context (org.neo4j.kernel.api.proc.Context)6 Log (org.neo4j.logging.Log)6 List (java.util.List)5 Statement (org.neo4j.kernel.api.Statement)5 ProcedureSignature (org.neo4j.kernel.api.proc.ProcedureSignature)5 Procedure (org.neo4j.procedure.Procedure)5 Method (java.lang.reflect.Method)4 Comparator (java.util.Comparator)4