use of org.neo4j.procedure.UserFunction in project neo4j by neo4j.
the class UserFunctionVisitor method validateName.
private Stream<CompilationMessage> validateName(ExecutableElement method) {
UserFunction function = method.getAnnotation(UserFunction.class);
String name = function.name();
if (!name.isEmpty() && isInRootNamespace(name)) {
return Stream.of(rootNamespaceError(method, name));
}
String value = function.value();
if (!value.isEmpty() && isInRootNamespace(value)) {
return Stream.of(rootNamespaceError(method, value));
}
PackageElement namespace = elements.getPackageOf(method);
if (namespace == null) {
return Stream.of(rootNamespaceError(method));
}
return Stream.empty();
}
use of org.neo4j.procedure.UserFunction in project neo4j-apoc-procedures by neo4j-contrib.
the class Numbers method format.
@UserFunction
@Description("apoc.number.format(number) | format a long or double using the default system pattern and language to produce a string")
public String format(@Name("number") final Object value, @Name(value = "pattern", defaultValue = "") String pattern, @Name(value = "lang", defaultValue = "") String lang) {
Number number = validateNumberParam(value);
if (number == null)
return null;
DecimalFormat format = buildFormatter(pattern, lang);
if (format == null)
return null;
return format.format(number);
}
use of org.neo4j.procedure.UserFunction in project neo4j-apoc-procedures by neo4j-contrib.
the class Strings method random.
@UserFunction
@Description("apoc.text.random(length, valid) YIELD value - generate a random string")
public String random(@Name("length") final long length, @Name(value = "valid", defaultValue = "A-Za-z0-9") String valid) {
valid = valid.replaceAll("A-Z", upper).replaceAll("a-z", lower).replaceAll("0-9", numeric);
StringBuilder output = new StringBuilder(toIntExact(length));
ThreadLocalRandom rand = ThreadLocalRandom.current();
while (output.length() < length) {
output.append(valid.charAt(rand.nextInt(valid.length())));
}
return output.toString();
}
use of org.neo4j.procedure.UserFunction 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);
}
use of org.neo4j.procedure.UserFunction in project neo4j by neo4j.
the class ReflectiveProcedureCompiler method compileFunction.
private CallableUserFunction compileFunction(Class<?> procDefinition, MethodHandle constructor, Method method, QualifiedName procName) throws ProcedureException, IllegalAccessException {
if (procName.namespace() == null || procName.namespace().length == 0) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "It is not allowed to define functions in the root namespace please use a namespace, e.g. `@UserFunction(\"org.example.com.%s\")", procName.name());
}
List<FieldSignature> inputSignature = inputSignatureDeterminer.signatureFor(method);
Class<?> returnType = method.getReturnType();
TypeMappers.NeoValueConverter valueConverter = typeMappers.converterFor(returnType);
MethodHandle procedureMethod = lookup.unreflect(method);
Optional<String> description = description(method);
UserFunction function = method.getAnnotation(UserFunction.class);
Optional<String> deprecated = deprecated(method, function::deprecatedBy, "Use of @UserFunction(deprecatedBy) without @Deprecated in " + procName);
List<FieldInjections.FieldSetter> setters = allFieldInjections.setters(procDefinition);
if (!config.fullAccessFor(procName.toString())) {
try {
setters = safeFieldInjections.setters(procDefinition);
} catch (ComponentInjectionException e) {
description = Optional.of(procName.toString() + " is not available due to having restricted access rights, check configuration.");
log.warn(description.get());
UserFunctionSignature signature = new UserFunctionSignature(procName, inputSignature, valueConverter.type(), deprecated, config.rolesFor(procName.toString()), description);
return new FailedLoadFunction(signature);
}
}
UserFunctionSignature signature = new UserFunctionSignature(procName, inputSignature, valueConverter.type(), deprecated, config.rolesFor(procName.toString()), description);
return new ReflectiveUserFunction(signature, constructor, procedureMethod, valueConverter, setters);
}
Aggregations