getValueConverter(
PersistentProperty> property) {
if (!initialized.get()) {
@@ -68,7 +69,7 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
factoryList.add(PropertyValueConverterFactory.simple());
}
- if (converterRegistrar != null && !converterRegistrar.isEmpty()) {
+ if ((converterRegistrar != null) && !converterRegistrar.isEmpty()) {
factoryList.add(PropertyValueConverterFactory.configuredInstance(converterRegistrar));
}
diff --git a/src/main/java/org/springframework/data/mapping/PersistentProperty.java b/src/main/java/org/springframework/data/mapping/PersistentProperty.java
index 30fc41e80..ddd863d28 100644
--- a/src/main/java/org/springframework/data/mapping/PersistentProperty.java
+++ b/src/main/java/org/springframework/data/mapping/PersistentProperty.java
@@ -427,9 +427,12 @@ public interface PersistentProperty> {
}
@Nullable
- default Class extends PropertyValueConverter,?, ? extends ValueConversionContext>> getValueConverterType() {
+ default Class extends PropertyValueConverter, ?, ? extends ValueConversionContext>>> getValueConverterType() {
+
PropertyConverter annotation = findAnnotation(PropertyConverter.class);
- return annotation == null ? null : annotation.value();
+
+ return annotation == null ? null
+ : (Class extends PropertyValueConverter, ?, ? extends ValueConversionContext>>>) annotation.value();
}
default boolean hasValueConverter() {
diff --git a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java
index 318241eb8..921bc8fe4 100644
--- a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java
+++ b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java
@@ -287,10 +287,12 @@ public abstract class AnnotationBasedPersistentProperty
> getValueConverterType() {
+ @SuppressWarnings("unchecked")
+ public Class extends PropertyValueConverter, ?, ? extends ValueConversionContext>>> getValueConverterType() {
return doFindAnnotation(PropertyConverter.class) //
.map(PropertyConverter::value) //
+ .map(Class.class::cast) //
.orElse(null);
}
diff --git a/src/test/java/org/springframework/data/convert/PropertyValueConverterFactoryUnitTests.java b/src/test/java/org/springframework/data/convert/PropertyValueConverterFactoryUnitTests.java
index 15426b866..b502145b1 100644
--- a/src/test/java/org/springframework/data/convert/PropertyValueConverterFactoryUnitTests.java
+++ b/src/test/java/org/springframework/data/convert/PropertyValueConverterFactoryUnitTests.java
@@ -26,6 +26,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.data.convert.PropertyValueConverter.ValueConversionContext;
import org.springframework.data.mapping.PersistentProperty;
+import org.springframework.data.mapping.context.SamplePersistentProperty;
import org.springframework.lang.Nullable;
/**
@@ -114,14 +115,14 @@ public class PropertyValueConverterFactoryUnitTests {
PropertyValueConverterFactory factory = PropertyValueConverterFactory.chained(new PropertyValueConverterFactory() {
@Nullable
@Override
- public PropertyValueConverter getConverter(
+ public > PropertyValueConverter getConverter(
Class extends PropertyValueConverter> converterType) {
return null;
}
}, new PropertyValueConverterFactory() {
@Nullable
@Override
- public PropertyValueConverter getConverter(
+ public > PropertyValueConverter getConverter(
Class extends PropertyValueConverter> converterType) {
return expected;
}
@@ -136,14 +137,14 @@ public class PropertyValueConverterFactoryUnitTests {
PropertyValueConverterFactory factory = PropertyValueConverterFactory.chained(new PropertyValueConverterFactory() {
@Nullable
@Override
- public PropertyValueConverter getConverter(
+ public > PropertyValueConverter getConverter(
Class extends PropertyValueConverter> converterType) {
return null;
}
}, new PropertyValueConverterFactory() {
@Nullable
@Override
- public PropertyValueConverter getConverter(
+ public > PropertyValueConverter getConverter(
Class extends PropertyValueConverter> converterType) {
throw new RuntimeException("can't touch this!");
}
@@ -176,22 +177,23 @@ public class PropertyValueConverterFactoryUnitTests {
.isSameAs(factory.getConverter(ConverterWithDefaultCtor.class)); // TODO: is this a valid assumption?
}
- static class ConverterWithDefaultCtor implements PropertyValueConverter {
+ static class ConverterWithDefaultCtor
+ implements PropertyValueConverter> {
@Nullable
@Override
- public String nativeToDomain(@Nullable UUID nativeValue, ValueConversionContext context) {
+ public String nativeToDomain(@Nullable UUID nativeValue, ValueConversionContext context) {
return nativeValue.toString();
}
@Nullable
@Override
- public UUID domainToNative(@Nullable String domainValue, ValueConversionContext context) {
+ public UUID domainToNative(@Nullable String domainValue, ValueConversionContext context) {
return UUID.fromString(domainValue);
}
}
- enum ConverterEnum implements PropertyValueConverter {
+ enum ConverterEnum implements PropertyValueConverter> {
INSTANCE;
@@ -208,7 +210,8 @@ public class PropertyValueConverterFactoryUnitTests {
}
}
- static class ConverterWithDependency implements PropertyValueConverter {
+ static class ConverterWithDependency
+ implements PropertyValueConverter> {
private final SomeDependency someDependency;
@@ -218,7 +221,7 @@ public class PropertyValueConverterFactoryUnitTests {
@Nullable
@Override
- public String nativeToDomain(@Nullable UUID nativeValue, ValueConversionContext context) {
+ public String nativeToDomain(@Nullable UUID nativeValue, ValueConversionContext context) {
assertThat(someDependency).isNotNull();
return nativeValue.toString();
@@ -226,7 +229,7 @@ public class PropertyValueConverterFactoryUnitTests {
@Nullable
@Override
- public UUID domainToNative(@Nullable String domainValue, ValueConversionContext context) {
+ public UUID domainToNative(@Nullable String domainValue, ValueConversionContext context) {
assertThat(someDependency).isNotNull();
return UUID.fromString(domainValue);
diff --git a/src/test/java/org/springframework/data/convert/WhatWeWant.java b/src/test/java/org/springframework/data/convert/WhatWeWant.java
index 9092b40e1..7ca616af4 100644
--- a/src/test/java/org/springframework/data/convert/WhatWeWant.java
+++ b/src/test/java/org/springframework/data/convert/WhatWeWant.java
@@ -31,11 +31,19 @@
*/
package org.springframework.data.convert;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
import java.util.function.Predicate;
import org.junit.jupiter.api.Test;
+import org.springframework.data.convert.PropertyValueConverter.ValueConversionContext;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PropertyPath;
+import org.springframework.data.mapping.context.SamplePersistentProperty;
+import org.springframework.data.util.MethodInvocationRecorder;
import org.springframework.lang.Nullable;
/**
@@ -47,7 +55,33 @@ public class WhatWeWant {
@Test
void converterConfig() {
- ConverterConfig converterConfig = null;
+ ConverterConfig converterConfig = new ConverterConfig();
+
+ // Arbitrary translations
+ converterConfig.registerConverter(Foo.class, Foo::getValue)
+ .writing((source, context) -> WhatWeWant.reverse(source)) // store reversed
+ .reading((source, context) -> WhatWeWant.reverse(source)); // restore order
+
+ converterConfig.registerConverter(it -> it.findAnnotation(Deprecated.class) != null)
+ .writing((source, context) -> WhatWeWant.reverse(source.toString()))
+ .reading((source, context) -> {
+ return context.read(WhatWeWant.reverse(source));
+ });
+
+ // Clean up on read
+ converterConfig.registerConverter(Foo.class, Foo::getValue)
+ .writingAsIs()
+ .reading((source, context) -> source.trim());
+
+ converterConfig.registerConverter(Foo.class, Foo::getAddress)
+ .writing((source, context) -> {
+
+ Map