Add support for property-specific converters.

Closes #1484
Original pull request: #2566.
This commit is contained in:
Christoph Strobl
2021-12-10 12:18:54 +01:00
committed by Mark Paluch
parent 9a3a38dc22
commit caf49ad739
15 changed files with 1220 additions and 8 deletions

View File

@@ -23,6 +23,9 @@ import java.util.Map;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.convert.PropertyConverter;
import org.springframework.data.convert.PropertyValueConverter;
import org.springframework.data.convert.PropertyValueConverter.ValueConversionContext;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -422,4 +425,14 @@ public interface PersistentProperty<P extends PersistentProperty<P>> {
return getOwner().getPropertyAccessor(owner);
}
@Nullable
default Class<? extends PropertyValueConverter<?,?, ? extends ValueConversionContext>> getValueConverterType() {
PropertyConverter annotation = findAnnotation(PropertyConverter.class);
return annotation == null ? null : annotation.value();
}
default boolean hasValueConverter() {
return isAnnotationPresent(PropertyConverter.class);
}
}

View File

@@ -33,6 +33,9 @@ import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.annotation.Reference;
import org.springframework.data.annotation.Transient;
import org.springframework.data.annotation.Version;
import org.springframework.data.convert.PropertyConverter;
import org.springframework.data.convert.PropertyValueConverter;
import org.springframework.data.convert.PropertyValueConverter.ValueConversionContext;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentEntity;
@@ -132,8 +135,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
+ "multiple times on accessor methods of property %s in class %s!",
annotationType.getSimpleName(), getName(), getOwner().getType().getSimpleName());
annotationCache.put(annotationType,
Optional.of(mergedAnnotation));
annotationCache.put(annotationType, Optional.of(mergedAnnotation));
}
});
@@ -148,8 +150,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
"Ambiguous mapping! Annotation %s configured " + "on field %s and one of its accessor methods in class %s!",
annotationType.getSimpleName(), it.getName(), getOwner().getType().getSimpleName());
annotationCache.put(annotationType,
Optional.of(mergedAnnotation));
annotationCache.put(annotationType, Optional.of(mergedAnnotation));
}
});
}
@@ -284,6 +285,15 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
return associationTargetType.getNullable();
}
@Nullable
@Override
public Class<? extends PropertyValueConverter<?, ?, ? extends ValueConversionContext>> getValueConverterType() {
return doFindAnnotation(PropertyConverter.class) //
.map(PropertyConverter::value) //
.orElse(null);
}
@Override
public String toString() {
@@ -315,8 +325,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
@SuppressWarnings("unchecked")
private static Class<? extends Annotation> loadIdentityType() {
return (Class<? extends Annotation>) ReflectionUtils.loadIfPresent(
"org.jmolecules.ddd.annotation.Identity",
return (Class<? extends Annotation>) ReflectionUtils.loadIfPresent("org.jmolecules.ddd.annotation.Identity",
AbstractPersistentProperty.class.getClassLoader());
}
}