use of org.neo4j.ogm.typeconversion.InstantStringConverter in project neo4j-ogm by neo4j.
the class ObjectAnnotations method getConverter.
Object getConverter(Class<?> fieldType) {
// try to get a custom type converter
AnnotationInfo customType = get(Convert.class);
if (customType != null) {
String classDescriptor = customType.get(Convert.CONVERTER, null);
if (classDescriptor == null || Convert.Unset.class.getName().equals(classDescriptor)) {
// will have a default proxy converter applied later on
return null;
}
try {
Class<?> clazz = Class.forName(classDescriptor, false, Configuration.getDefaultClassLoader());
return clazz.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// try to find a pre-registered type annotation. this is very clumsy, but at least it is done only once
AnnotationInfo dateLongConverterInfo = get(DateLong.class);
if (dateLongConverterInfo != null) {
if (fieldType.equals(Instant.class)) {
return new InstantLongConverter();
}
return new DateLongConverter();
}
AnnotationInfo dateStringConverterInfo = get(DateString.class);
if (dateStringConverterInfo != null) {
String format = dateStringConverterInfo.get(DateString.FORMAT, DateString.ISO_8601);
if (fieldType == Date.class) {
return new DateStringConverter(format, isLenientConversion(dateStringConverterInfo));
} else if (fieldType == Instant.class) {
return new InstantStringConverter(format, dateStringConverterInfo.get("zoneId"), isLenientConversion(dateStringConverterInfo));
} else {
throw new MappingException("Cannot use @DateString with attribute of type " + fieldType);
}
}
AnnotationInfo enumStringConverterInfo = get(EnumString.class);
if (enumStringConverterInfo != null) {
String classDescriptor = enumStringConverterInfo.get(EnumString.TYPE, null);
try {
Class clazz = Class.forName(classDescriptor, false, Configuration.getDefaultClassLoader());
return new EnumStringConverter(clazz, isLenientConversion(enumStringConverterInfo));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
AnnotationInfo numberStringConverterInfo = get(NumberString.class);
if (numberStringConverterInfo != null) {
String classDescriptor = numberStringConverterInfo.get(NumberString.TYPE, null);
try {
Class clazz = Class.forName(classDescriptor, false, Configuration.getDefaultClassLoader());
return new NumberStringConverter(clazz, isLenientConversion(numberStringConverterInfo));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return null;
}
Aggregations