use of org.neo4j.ogm.typeconversion.ProxyAttributeConverter in project neo4j-ogm by neo4j.
the class DomainInfo method registerDefaultFieldConverters.
private void registerDefaultFieldConverters(ClassInfo classInfo, FieldInfo fieldInfo) {
if (!fieldInfo.hasPropertyConverter() && !fieldInfo.hasCompositeConverter()) {
final String typeDescriptor = fieldInfo.getTypeDescriptor();
// Check if there's a registered set of attribute converters for the given field info and if so,
// select the correct one based on the features of the field
Function<AttributeConverters, Optional<AttributeConverter<?, ?>>> selectAttributeConverter = ac -> DomainInfo.selectAttributeConverterFor(fieldInfo, ac);
Optional<AttributeConverter<?, ?>> registeredAttributeConverter = ConvertibleTypes.REGISTRY.entrySet().stream().filter(e -> typeDescriptor.contains(e.getKey())).sorted(comparingInt((Map.Entry<String, ?> e) -> e.getKey().length()).reversed()).findFirst().map(Map.Entry::getValue).flatMap(selectAttributeConverter);
boolean isSupportedNativeType = typeSystem.supportsAsNativeType(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()));
// We can use a registered converter
if (registeredAttributeConverter.isPresent() && !isSupportedNativeType) {
fieldInfo.setPropertyConverter(registeredAttributeConverter.get());
} else {
// Check if the user configured one through the convert annotation
if (fieldInfo.getAnnotations().get(Convert.class) != null) {
// no converter's been set but this method is annotated with @Convert so we need to proxy it
Class<?> entityAttributeType = DescriptorMappings.getType(typeDescriptor);
String graphTypeDescriptor = fieldInfo.getAnnotations().get(Convert.class).get(Convert.GRAPH_TYPE, null);
if (graphTypeDescriptor == null) {
throw new MappingException("Found annotation to convert a " + (entityAttributeType != null ? entityAttributeType.getName() : " null object ") + " on " + classInfo.name() + '.' + fieldInfo.getName() + " but no target graph property type or specific AttributeConverter have been specified.");
}
fieldInfo.setPropertyConverter(new ProxyAttributeConverter(entityAttributeType, DescriptorMappings.getType(graphTypeDescriptor), this.conversionCallbackRegistry));
}
Class fieldType = DescriptorMappings.getType(typeDescriptor);
if (fieldType == null) {
throw new RuntimeException("Class " + classInfo.name() + " field " + fieldInfo.getName() + " has null field type.");
}
boolean enumConverterSet = false;
for (Class enumClass : enumTypes) {
if (fieldType.equals(enumClass)) {
setEnumFieldConverter(fieldInfo, enumClass);
enumConverterSet = true;
break;
}
}
if (!enumConverterSet && isEnum(fieldType)) {
LOGGER.debug("Setting default enum converter for unscanned class " + classInfo.name() + ", field: " + fieldInfo.getName());
setEnumFieldConverter(fieldInfo, fieldType);
}
}
}
}
Aggregations