Add support for property-specific converters.
Closes #1484 Original pull request: #2566.
This commit is contained in:
committed by
Mark Paluch
parent
9a3a38dc22
commit
caf49ad739
@@ -33,6 +33,7 @@ import org.springframework.core.convert.converter.GenericConverter;
|
||||
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.Predicates;
|
||||
import org.springframework.data.util.Streamable;
|
||||
@@ -96,6 +97,8 @@ public class CustomConversions {
|
||||
private final Function<ConvertiblePair, Class<?>> getRawWriteTarget = convertiblePair -> getCustomTarget(
|
||||
convertiblePair.getSourceType(), null, writingPairs);
|
||||
|
||||
private PropertyValueConversions propertyValueConversions;
|
||||
|
||||
/**
|
||||
* @param converterConfiguration the {@link ConverterConfiguration} to apply.
|
||||
* @since 2.3
|
||||
@@ -118,6 +121,7 @@ public class CustomConversions {
|
||||
this.converters = Collections.unmodifiableList(registeredConverters);
|
||||
this.simpleTypeHolder = new SimpleTypeHolder(customSimpleTypes,
|
||||
converterConfiguration.getStoreConversions().getStoreTypeHolder());
|
||||
this.propertyValueConversions = converterConfiguration.getPropertyValueConversions();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,6 +174,36 @@ public class CustomConversions {
|
||||
VavrCollectionConverters.getConvertersToRegister().forEach(it -> registerConverterIn(it, conversionService));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate check if a {@link PropertyValueConverter} for the given {@literal property} is present via
|
||||
* {@link PropertyValueConversions}.
|
||||
*
|
||||
* @param property must not be {@literal null}.
|
||||
* @return {@literal true} if a specific {@link PropertyValueConverter} is available.
|
||||
* @see PropertyValueConversions#hasValueConverter(PersistentProperty)
|
||||
* @since ?
|
||||
*/
|
||||
public boolean hasPropertyValueConverter(PersistentProperty<?> property) {
|
||||
return propertyValueConversions != null ? propertyValueConversions.hasValueConverter(property) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate to obtain the {@link PropertyValueConverter} for the given {@literal property} from
|
||||
* {@link PropertyValueConversions}.
|
||||
*
|
||||
* @param property must not be {@literal null}. param <A> domain specific type
|
||||
* @param <B> store native type
|
||||
* @param <C> conversion context type
|
||||
* @return the suitable {@link PropertyValueConverter} or {@literal null} if none available.
|
||||
* @see PropertyValueConversions#getValueConverter(PersistentProperty)
|
||||
* @since ?
|
||||
*/
|
||||
@Nullable
|
||||
public <A, B, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<A, B, C> getPropertyValueConverter(
|
||||
PersistentProperty<?> property) {
|
||||
return propertyValueConversions != null ? propertyValueConversions.getValueConverter(property) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all converters and add origin information
|
||||
*
|
||||
@@ -862,6 +896,7 @@ public class CustomConversions {
|
||||
private final StoreConversions storeConversions;
|
||||
private final List<?> userConverters;
|
||||
private final Predicate<ConvertiblePair> converterRegistrationFilter;
|
||||
private final PropertyValueConversions propertyValueConversions;
|
||||
|
||||
/**
|
||||
* Create a new ConverterConfiguration holding the given {@link StoreConversions} and user defined converters.
|
||||
@@ -887,9 +922,16 @@ public class CustomConversions {
|
||||
public ConverterConfiguration(StoreConversions storeConversions, List<?> userConverters,
|
||||
Predicate<ConvertiblePair> converterRegistrationFilter) {
|
||||
|
||||
this(storeConversions, userConverters, converterRegistrationFilter, new SimplePropertyValueConversions());
|
||||
}
|
||||
|
||||
public ConverterConfiguration(StoreConversions storeConversions, List<?> userConverters,
|
||||
Predicate<ConvertiblePair> converterRegistrationFilter, @Nullable PropertyValueConversions propertyValueConversions) {
|
||||
|
||||
this.storeConversions = storeConversions;
|
||||
this.userConverters = new ArrayList<>(userConverters);
|
||||
this.converterRegistrationFilter = converterRegistrationFilter;
|
||||
this.propertyValueConversions = propertyValueConversions;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -912,5 +954,14 @@ public class CustomConversions {
|
||||
boolean shouldRegister(ConvertiblePair candidate) {
|
||||
return this.converterRegistrationFilter.test(candidate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the configured {@link PropertyValueConversions} if set, {@literal null} otherwise.
|
||||
* @since ?
|
||||
*/
|
||||
@Nullable
|
||||
public PropertyValueConversions getPropertyValueConversions() {
|
||||
return this.propertyValueConversions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.data.convert.PropertyValueConverter.ObjectToObjectPropertyValueConverter;
|
||||
|
||||
/**
|
||||
* Annotation to define usage of a {@link PropertyValueConverter} to read/write the property.
|
||||
* <p>
|
||||
* Consult the store specific documentation for details and support notes.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
*/
|
||||
@Target(FIELD)
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface PropertyConverter {
|
||||
|
||||
/**
|
||||
* The {@link PropertyValueConverter} type handling the value conversion of the annotated property.
|
||||
*
|
||||
* @return the configured {@link PropertyValueConverter}. {@link ObjectToObjectPropertyValueConverter} by default.
|
||||
*/
|
||||
Class<? extends PropertyValueConverter<?, ?, ? extends PropertyValueConverter.ValueConversionContext>> value() default ObjectToObjectPropertyValueConverter.class;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* {@link PropertyValueConversions} provides access to {@link PropertyValueConverter converters} that may only be
|
||||
* applied to a specific property. Other than {@link org.springframework.core.convert.converter.Converter converters}
|
||||
* registered in {@link CustomConversions} the property based variants accept and allow returning {@literal null} values
|
||||
* and provide access to a store specific {@link PropertyValueConverter.ValueConversionContext conversion context}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
* @currentBook The Desert Prince - Peter V. Brett
|
||||
*/
|
||||
public interface PropertyValueConversions {
|
||||
|
||||
/**
|
||||
* Check if a {@link PropertyValueConverter} is present for the given {@literal property}.
|
||||
*
|
||||
* @param property must not be {@literal null}.
|
||||
* @return {@literal true} if a specific {@link PropertyValueConverter} is available.
|
||||
*/
|
||||
default boolean hasValueConverter(PersistentProperty<?> property) {
|
||||
return getValueConverter(property) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link PropertyValueConverter} for the given {@literal property} if present.
|
||||
*
|
||||
* @param property must not be {@literal null}. param <A> domain specific type
|
||||
* @param <B> store native type
|
||||
* @param <C> conversion context type
|
||||
* @return the suitable {@link PropertyValueConverter} or {@literal null} if none available.
|
||||
*/
|
||||
@Nullable
|
||||
<A, B, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<A, B, C> getValueConverter(
|
||||
PersistentProperty<?> property);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* {@link PropertyValueConverter} provides a symmetric way of converting certain properties from domain to store
|
||||
* specific values.
|
||||
* <p>
|
||||
* A {@link PropertyValueConverter} is, other than a {@link ReadingConverter} or {@link WritingConverter}, only applied
|
||||
* to special annotated fields which allows a fine grained conversion of certain values within a specific context.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @param <A> domain specific type
|
||||
* @param <B> store native type
|
||||
* @since 2.7
|
||||
*/
|
||||
public interface PropertyValueConverter<A, B, C extends PropertyValueConverter.ValueConversionContext> {
|
||||
|
||||
/**
|
||||
* Convert the given store specific value into it's domain value representation. Typically a {@literal read}
|
||||
* operation.
|
||||
*
|
||||
* @param nativeValue can be {@literal null}.
|
||||
* @param context never {@literal null}.
|
||||
* @return the converted value. Can be {@literal null}.
|
||||
*/
|
||||
@Nullable
|
||||
A /*read*/ nativeToDomain(@Nullable B nativeValue, C context);
|
||||
|
||||
/**
|
||||
* Convert the given domain specific value into it's native store representation. Typically a {@literal write}
|
||||
* operation.
|
||||
*
|
||||
* @param domainValue can be {@literal null}.
|
||||
* @param context never {@literal null}.
|
||||
* @return the converted value. Can be {@literal null}.
|
||||
*/
|
||||
@Nullable
|
||||
B /*write*/ domainToNative(@Nullable A domainValue, C context);
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
interface ValueConversionContext {
|
||||
|
||||
PersistentProperty<?> getProperty();
|
||||
}
|
||||
|
||||
/**
|
||||
* NoOp {@link PropertyValueConverter} implementation.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
enum ObjectToObjectPropertyValueConverter implements PropertyValueConverter<Object, Object, ValueConversionContext> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Object nativeToDomain(Object value, ValueConversionContext context) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object domainToNative(Object value, ValueConversionContext context) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link PropertyValueConverterFactories} provides a collection of predefined {@link PropertyValueConverterFactory}
|
||||
* implementations. Depending on the applications need {@link PropertyValueConverterFactory factories} can be
|
||||
* {@link CompositePropertyValueConverterFactory chained} and the created {@link PropertyValueConverter converter}
|
||||
* {@link CachingPropertyValueConverterFactory cached}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
*/
|
||||
final class PropertyValueConverterFactories {
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
*/
|
||||
static class CompositePropertyValueConverterFactory implements PropertyValueConverterFactory {
|
||||
|
||||
private List<PropertyValueConverterFactory> delegates;
|
||||
|
||||
CompositePropertyValueConverterFactory(PropertyValueConverterFactory... delegates) {
|
||||
this(Arrays.asList(delegates));
|
||||
}
|
||||
|
||||
CompositePropertyValueConverterFactory(List<PropertyValueConverterFactory> delegates) {
|
||||
this.delegates = delegates;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <A, B, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<A, B, C> getConverter(
|
||||
PersistentProperty<?> property) {
|
||||
return delegates.stream().map(it -> (PropertyValueConverter<A, B, C>) it.getConverter(property))
|
||||
.filter(Objects::nonNull).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S, T, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<S, T, C> getConverter(
|
||||
Class<? extends PropertyValueConverter<S, T, C>> converterType) {
|
||||
return delegates.stream().filter(it -> it.getConverter(converterType) != null).findFirst()
|
||||
.map(it -> it.getConverter(converterType)).orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trivial implementation of {@link PropertyValueConverter}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
*/
|
||||
static class SimplePropertyConverterFactory implements PropertyValueConverterFactory {
|
||||
|
||||
@Override
|
||||
public <S, T, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<S, T, C> getConverter(
|
||||
Class<? extends PropertyValueConverter<S, T, C>> converterType) {
|
||||
|
||||
Assert.notNull(converterType, "ConverterType must not be null!");
|
||||
|
||||
if (converterType.isEnum()) {
|
||||
return (PropertyValueConverter<S, T, C>) EnumSet.allOf((Class) converterType).iterator().next();
|
||||
}
|
||||
return BeanUtils.instantiateClass(converterType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link PropertyValueConverterFactory} implementation that leverages the {@link BeanFactory} to create the desired
|
||||
* {@link PropertyValueConverter}. This allows the {@link PropertyValueConverter} to make use of DI.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
*/
|
||||
static class BeanFactoryAwarePropertyValueConverterFactory implements PropertyValueConverterFactory {
|
||||
|
||||
private final BeanFactory beanFactory;
|
||||
|
||||
public BeanFactoryAwarePropertyValueConverterFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S, T, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<S, T, C> getConverter(
|
||||
Class<? extends PropertyValueConverter<S, T, C>> converterType) {
|
||||
|
||||
Assert.state(beanFactory != null, "BeanFactory must not be null. Did you forget to set it!");
|
||||
Assert.notNull(converterType, "ConverterType must not be null!");
|
||||
|
||||
try {
|
||||
return beanFactory.getBean(converterType);
|
||||
} catch (NoSuchBeanDefinitionException exception) {
|
||||
|
||||
if (beanFactory instanceof AutowireCapableBeanFactory) {
|
||||
return (PropertyValueConverter<S, T, C>) ((AutowireCapableBeanFactory) beanFactory).createBean(converterType,
|
||||
AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
*/
|
||||
static class ConfiguredInstanceServingValueConverterFactory implements PropertyValueConverterFactory {
|
||||
|
||||
private final PropertyValueConverterRegistrar conversionsRegistrar;
|
||||
|
||||
public ConfiguredInstanceServingValueConverterFactory(PropertyValueConverterRegistrar conversionsRegistrar) {
|
||||
|
||||
Assert.notNull(conversionsRegistrar, "ConversionsRegistrar must not be null!");
|
||||
this.conversionsRegistrar = conversionsRegistrar;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <A, B, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<A, B, C> getConverter(
|
||||
PersistentProperty<?> property) {
|
||||
return (PropertyValueConverter<A, B, C>) conversionsRegistrar.getConverter(property.getOwner().getType(),
|
||||
property.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S, T, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<S, T, C> getConverter(
|
||||
Class<? extends PropertyValueConverter<S, T, C>> converterType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: This would be easier if we'd get rid of {@link PropertyValueConverterFactory#getConverter(Class)}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
*/
|
||||
static class CachingPropertyValueConverterFactory implements PropertyValueConverterFactory {
|
||||
|
||||
private final PropertyValueConverterFactory delegate;
|
||||
private final Cache cache = new Cache();
|
||||
|
||||
public CachingPropertyValueConverterFactory(PropertyValueConverterFactory delegate) {
|
||||
|
||||
Assert.notNull(delegate, "Delegate must not be null!");
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <S, T, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<S, T, C> getConverter(
|
||||
PersistentProperty<?> property) {
|
||||
|
||||
PropertyValueConverter converter = cache.get(property);
|
||||
if (converter != null) {
|
||||
return converter;
|
||||
}
|
||||
return cache.cache(property, delegate.getConverter(property));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S, T, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<S, T, C> getConverter(
|
||||
Class<? extends PropertyValueConverter<S, T, C>> converterType) {
|
||||
|
||||
PropertyValueConverter converter = cache.get(converterType);
|
||||
if (converter != null) {
|
||||
return converter;
|
||||
}
|
||||
return cache.cache(converterType, delegate.getConverter(converterType));
|
||||
}
|
||||
|
||||
static class Cache {
|
||||
|
||||
Map<PersistentProperty<?>, PropertyValueConverter<?, ?, ? extends PropertyValueConverter.ValueConversionContext>> perPropertyCache = new HashMap<>();
|
||||
Map<Class<?>, PropertyValueConverter<?, ?, ? extends PropertyValueConverter.ValueConversionContext>> typeCache = new HashMap<>();
|
||||
|
||||
PropertyValueConverter<?, ?, ? extends PropertyValueConverter.ValueConversionContext> get(PersistentProperty<?> property) {
|
||||
return perPropertyCache.get(property);
|
||||
}
|
||||
|
||||
PropertyValueConverter<?, ?, ? extends PropertyValueConverter.ValueConversionContext> get(Class<?> type) {
|
||||
return typeCache.get(type);
|
||||
}
|
||||
|
||||
<S, T, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<S, T, C> cache(PersistentProperty<?> property,
|
||||
PropertyValueConverter<S, T, C> converter) {
|
||||
perPropertyCache.putIfAbsent(property, converter);
|
||||
cache(property.getValueConverterType(), converter);
|
||||
return converter;
|
||||
}
|
||||
|
||||
<S, T, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<S, T, C> cache(Class<?> type,
|
||||
PropertyValueConverter<S, T, C> converter) {
|
||||
typeCache.putIfAbsent(type, converter);
|
||||
return converter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.data.convert.PropertyValueConverterFactories.BeanFactoryAwarePropertyValueConverterFactory;
|
||||
import org.springframework.data.convert.PropertyValueConverterFactories.CachingPropertyValueConverterFactory;
|
||||
import org.springframework.data.convert.PropertyValueConverterFactories.CompositePropertyValueConverterFactory;
|
||||
import org.springframework.data.convert.PropertyValueConverterFactories.ConfiguredInstanceServingValueConverterFactory;
|
||||
import org.springframework.data.convert.PropertyValueConverterFactories.SimplePropertyConverterFactory;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A factory that provides {@link PropertyValueConverter value converters}.
|
||||
* <p>
|
||||
* Depending on the applications need {@link PropertyValueConverterFactory factories} can be {@link #chained(List)
|
||||
* chained} and the resulting {@link PropertyValueConverter converter} may be
|
||||
* {@link #caching(PropertyValueConverterFactory) cached}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
*/
|
||||
public interface PropertyValueConverterFactory {
|
||||
|
||||
/**
|
||||
* Get the {@link PropertyValueConverter} applicable for the given {@link PersistentProperty}.
|
||||
*
|
||||
* @param property must not be {@literal null}.
|
||||
* @param <A> domain specific type
|
||||
* @param <B> store native type
|
||||
* @return can be {@literal null}.
|
||||
*/
|
||||
@Nullable
|
||||
default <A, B, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<A, B, C> getConverter(
|
||||
PersistentProperty<?> property) {
|
||||
|
||||
if (!property.hasValueConverter()) {
|
||||
return null;
|
||||
}
|
||||
return getConverter((Class<PropertyValueConverter<A, B, C>>) property.getValueConverterType());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
<S, T, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<S, T, C> getConverter(
|
||||
Class<? extends PropertyValueConverter<S, T, C>> converterType);
|
||||
|
||||
/**
|
||||
* Obtain a simple {@link PropertyValueConverterFactory} capable of instantiating {@link PropertyValueConverter}
|
||||
* implementations via their default {@link java.lang.reflect.Constructor} or in case of an {@link Enum} accessing the
|
||||
* first enum value.
|
||||
*
|
||||
* @return new instance of {@link PropertyValueConverterFactory}.
|
||||
*/
|
||||
static PropertyValueConverterFactory simple() {
|
||||
return new SimplePropertyConverterFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a {@link PropertyValueConverterFactory} capable of looking up/creating the {@link PropertyValueConverter}
|
||||
* via the given {@link BeanFactory}.
|
||||
*
|
||||
* @param beanFactory must not be {@literal null}.
|
||||
* @return new instance of {@link PropertyValueConverterFactory}.
|
||||
*/
|
||||
static PropertyValueConverterFactory beanFactoryAware(BeanFactory beanFactory) {
|
||||
return new BeanFactoryAwarePropertyValueConverterFactory(beanFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a {@link PropertyValueConverterFactory} capable of looking up the {@link PropertyValueConverter} in the
|
||||
* given {@link PropertyValueConverterRegistrar}.
|
||||
*
|
||||
* @param registrar must not be {@literal null}.
|
||||
* @return new instance of {@link PropertyValueConverterFactory}.
|
||||
*/
|
||||
static PropertyValueConverterFactory configuredInstance(PropertyValueConverterRegistrar registrar) {
|
||||
return new ConfiguredInstanceServingValueConverterFactory(registrar);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a {@link PropertyValueConverterFactory} that will try to obtain a {@link PropertyValueConverter} from the
|
||||
* given array of {@link PropertyValueConverterFactory factories} by returning the first non {@literal null} one.
|
||||
*
|
||||
* @param factories must not be {@literal null} nor contain {@literal null} values.
|
||||
* @return new instance of {@link PropertyValueConverterFactory}.
|
||||
*/
|
||||
static PropertyValueConverterFactory chained(PropertyValueConverterFactory... factories) {
|
||||
return chained(Arrays.asList(factories));
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a {@link PropertyValueConverterFactory} that will try to obtain a {@link PropertyValueConverter} from the
|
||||
* given list of {@link PropertyValueConverterFactory factories} by returning the first non {@literal null} one.
|
||||
*
|
||||
* @param factoryList must not be {@literal null} nor contain {@literal null} values.
|
||||
* @return new instance of {@link PropertyValueConverterFactory}.
|
||||
*/
|
||||
static PropertyValueConverterFactory chained(List<PropertyValueConverterFactory> factoryList) {
|
||||
|
||||
Assert.noNullElements(factoryList, "FactoryList must not contain null elements.");
|
||||
|
||||
if (factoryList.size() == 1) {
|
||||
return factoryList.iterator().next();
|
||||
}
|
||||
|
||||
return new CompositePropertyValueConverterFactory(factoryList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a {@link PropertyValueConverterFactory} that will cache {@link PropertyValueConverter} instances per
|
||||
* {@link PersistentProperty}.
|
||||
*
|
||||
* @param factory
|
||||
* @return
|
||||
*/
|
||||
static PropertyValueConverterFactory caching(PropertyValueConverterFactory factory) {
|
||||
return new CachingPropertyValueConverterFactory(factory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
*/
|
||||
public class PropertyValueConverterRegistrar {
|
||||
|
||||
private final Map<Key, PropertyValueConverter<?, ?, ? extends PropertyValueConverter.ValueConversionContext>> converterRegistrationMap = new LinkedHashMap<>();
|
||||
|
||||
boolean hasConverterFor(Class<?> type, String path) {
|
||||
return converterRegistrationMap.containsKey(new Key(type, path));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PropertyValueConverter<?, ?, ? extends PropertyValueConverter.ValueConversionContext> getConverter(Class<?> type, String path) {
|
||||
return converterRegistrationMap.get(new Key(type, path));
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return converterRegistrationMap.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return converterRegistrationMap.isEmpty();
|
||||
}
|
||||
|
||||
public PropertyValueConverterRegistrar register(Class<?> type, String path,
|
||||
PropertyValueConverter<?, ?, ? extends PropertyValueConverter.ValueConversionContext> converter) {
|
||||
|
||||
converterRegistrationMap.put(new Key(type, path), converter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Collection<PropertyValueConverter<?, ?, ? extends PropertyValueConverter.ValueConversionContext>> converters() {
|
||||
return converterRegistrationMap.values();
|
||||
}
|
||||
|
||||
public PropertyValueConverterRegistrar registerIfAbsent(Class<?> type, String path,
|
||||
PropertyValueConverter<?, ?, ? extends PropertyValueConverter.ValueConversionContext> converter) {
|
||||
converterRegistrationMap.putIfAbsent(new Key(type, path), converter);
|
||||
return this;
|
||||
}
|
||||
|
||||
static class Key {
|
||||
|
||||
Class<?> type;
|
||||
String path;
|
||||
|
||||
public Key(Class<?> type, String path) {
|
||||
this.type = type;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
|
||||
Key key = (Key) o;
|
||||
|
||||
if (!ObjectUtils.nullSafeEquals(type, key.type)) {
|
||||
return false;
|
||||
}
|
||||
return ObjectUtils.nullSafeEquals(path, key.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = ObjectUtils.nullSafeHashCode(type);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(path);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @since ?
|
||||
*/
|
||||
public class SimplePropertyValueConversions implements PropertyValueConversions, InitializingBean {
|
||||
|
||||
private @Nullable PropertyValueConverterFactory converterFactory;
|
||||
private @Nullable PropertyValueConverterRegistrar converterRegistrar;
|
||||
private boolean converterCacheEnabled = true;
|
||||
private AtomicBoolean initialized = new AtomicBoolean(false);
|
||||
|
||||
public void setConverterFactory(PropertyValueConverterFactory converterFactory) {
|
||||
this.converterFactory = converterFactory;
|
||||
}
|
||||
|
||||
public void setConverterRegistrar(PropertyValueConverterRegistrar converterRegistrar) {
|
||||
this.converterRegistrar = converterRegistrar;
|
||||
}
|
||||
|
||||
public void setConverterCacheEnabled(boolean converterCacheEnabled) {
|
||||
this.converterCacheEnabled = converterCacheEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <A, B, C extends PropertyValueConverter.ValueConversionContext> PropertyValueConverter<A, B, C> getValueConverter(
|
||||
PersistentProperty<?> property) {
|
||||
|
||||
if (!initialized.get()) {
|
||||
init();
|
||||
}
|
||||
|
||||
return this.converterFactory.getConverter(property);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
|
||||
if (initialized.compareAndSet(false, true)) {
|
||||
List<PropertyValueConverterFactory> factoryList = new ArrayList<>(3);
|
||||
|
||||
if (converterFactory != null) {
|
||||
factoryList.add(converterFactory);
|
||||
} else {
|
||||
factoryList.add(PropertyValueConverterFactory.simple());
|
||||
}
|
||||
|
||||
if (converterRegistrar != null && !converterRegistrar.isEmpty()) {
|
||||
factoryList.add(PropertyValueConverterFactory.configuredInstance(converterRegistrar));
|
||||
}
|
||||
|
||||
PropertyValueConverterFactory targetFactory = factoryList.size() > 1
|
||||
? PropertyValueConverterFactory.chained(factoryList)
|
||||
: factoryList.iterator().next();
|
||||
|
||||
this.converterFactory = converterCacheEnabled ? PropertyValueConverterFactory.caching(targetFactory)
|
||||
: targetFactory;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
|
||||
init();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user