use of org.neo4j.kernel.api.proc.FieldSignature 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;
}
use of org.neo4j.kernel.api.proc.FieldSignature 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);
}
use of org.neo4j.kernel.api.proc.FieldSignature in project neo4j by neo4j.
the class OutputMappersTest method shouldSkipStaticFields.
@Test
public void shouldSkipStaticFields() throws Throwable {
// When
OutputMapper mapper = mapper(RecordWithStaticFields.class);
// Then
assertThat(mapper.signature(), contains(new FieldSignature("includeMe", Neo4jTypes.NTString)));
assertThat(asList(mapper.apply(new RecordWithStaticFields("hello, world!"))), contains("hello, world!"));
}
use of org.neo4j.kernel.api.proc.FieldSignature in project neo4j by neo4j.
the class OutputMappersTest method shouldMapSimpleRecordWithString.
@Test
public void shouldMapSimpleRecordWithString() throws Throwable {
// When
OutputMapper mapper = mapper(SingleStringFieldRecord.class);
// Then
assertThat(mapper.signature(), contains(new FieldSignature("name", Neo4jTypes.NTString)));
assertThat(asList(mapper.apply(new SingleStringFieldRecord("hello, world!"))), contains("hello, world!"));
}
use of org.neo4j.kernel.api.proc.FieldSignature in project neo4j by neo4j.
the class ProcedureSignatureTest method inputSignatureShouldNotBeModifiable.
@Test
public void inputSignatureShouldNotBeModifiable() throws Throwable {
// Expect
exception.expect(UnsupportedOperationException.class);
// When
signature.inputSignature().add(new FieldSignature("b", Neo4jTypes.NTAny));
}
Aggregations