Browse Source

DATACMNS-836 - Favor usage of Function over Converter.

pull/185/head
Christoph Strobl 9 years ago committed by Oliver Gierke
parent
commit
5bf19d1c2a
  1. 4
      src/main/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupport.java
  2. 23
      src/main/java/org/springframework/data/repository/core/support/ReactiveRepositoryInformation.java
  3. 2
      src/main/java/org/springframework/data/repository/query/ResultProcessor.java
  4. 76
      src/main/java/org/springframework/data/repository/util/ReactiveWrapperConverters.java
  5. 7
      src/main/java/org/springframework/data/repository/util/ReactiveWrappers.java

4
src/main/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupport.java

@ -301,9 +301,7 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit @@ -301,9 +301,7 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
try {
return org.springframework.util.ClassUtils.forName(repositoryInterface, classLoader);
} catch (ClassNotFoundException e) {
LOGGER.warn(String.format(CLASS_LOADING_ERROR, getModuleName(), repositoryInterface, classLoader), e);
} catch (LinkageError e) {
} catch (ClassNotFoundException | LinkageError e) {
LOGGER.warn(String.format(CLASS_LOADING_ERROR, getModuleName(), repositoryInterface, classLoader), e);
}

23
src/main/java/org/springframework/data/repository/core/support/ReactiveRepositoryInformation.java

@ -31,7 +31,7 @@ import org.springframework.util.Assert; @@ -31,7 +31,7 @@ import org.springframework.util.Assert;
/**
* This {@link RepositoryInformation} uses a {@link ConversionService} to check whether method arguments can be
* converted for invocation of implementation methods.
*
*
* @author Mark Paluch
* @since 2.0
*/
@ -42,7 +42,7 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation @@ -42,7 +42,7 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation
/**
* Creates a new {@link ReactiveRepositoryInformation} for the given repository interface and repository base class
* using a {@link ConversionService}.
*
*
* @param metadata must not be {@literal null}.
* @param repositoryBaseClass must not be {@literal null}.
* @param customImplementationClass
@ -67,15 +67,15 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation @@ -67,15 +67,15 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation
* @param baseClass
* @return
*/
@Override
Method getTargetClassMethod(Method method, Class<?> baseClass) {
if (baseClass == null) {
return method;
}
boolean wantsWrappers = wantsMethodUsingReactiveWrapperParameters(method);
if (usesParametersWithReactiveWrappers(method)) {
if (wantsWrappers) {
Method candidate = getMethodCandidate(method, baseClass, new AssignableWrapperMatch(method));
if (candidate != null) {
@ -99,12 +99,13 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation @@ -99,12 +99,13 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation
return method;
}
private boolean wantsMethodUsingReactiveWrapperParameters(Method method) {
private boolean usesParametersWithReactiveWrappers(Method method) {
boolean wantsWrappers = false;
for (Class<?> parameterType : method.getParameterTypes()) {
if (isNonunwrappingWrapper(parameterType)) {
if (isNonUnwrappingWrapper(parameterType)) {
wantsWrappers = true;
break;
}
@ -167,13 +168,13 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation @@ -167,13 +168,13 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation
* @param parameterType
* @return
*/
static boolean isNonunwrappingWrapper(Class<?> parameterType) {
static boolean isNonUnwrappingWrapper(Class<?> parameterType) {
return QueryExecutionConverters.supports(parameterType)
&& !QueryExecutionConverters.supportsUnwrapping(parameterType);
}
/**
* {@link BiPredicate} to check whether a method parameter is a {@link #isNonunwrappingWrapper(Class)} and can be
* {@link BiPredicate} to check whether a method parameter is a {@link #isNonUnwrappingWrapper(Class)} and can be
* converted into a different wrapper. Usually {@link rx.Observable} to {@link org.reactivestreams.Publisher}
* conversion.
*/
@ -193,7 +194,7 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation @@ -193,7 +194,7 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation
@Override
public boolean test(Class<?> candidateParameterType, Integer index) {
if (isNonunwrappingWrapper(candidateParameterType) && isNonunwrappingWrapper(declaredParameterTypes[index])) {
if (isNonUnwrappingWrapper(candidateParameterType) && isNonUnwrappingWrapper(declaredParameterTypes[index])) {
if (conversionService.canConvert(declaredParameterTypes[index], candidateParameterType)) {
return true;
@ -205,7 +206,7 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation @@ -205,7 +206,7 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation
}
/**
* {@link BiPredicate} to check parameter assignability between a {@link #isNonunwrappingWrapper(Class)} parameter and
* {@link BiPredicate} to check parameter assignability between a {@link #isNonUnwrappingWrapper(Class)} parameter and
* a declared parameter. Usually {@link reactor.core.publisher.Flux} vs. {@link org.reactivestreams.Publisher}
* conversion.
*/
@ -223,7 +224,7 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation @@ -223,7 +224,7 @@ public class ReactiveRepositoryInformation extends DefaultRepositoryInformation
@Override
public boolean test(Class<?> candidateParameterType, Integer index) {
if (isNonunwrappingWrapper(candidateParameterType) && isNonunwrappingWrapper(declaredParameterTypes[index])) {
if (isNonUnwrappingWrapper(candidateParameterType) && isNonUnwrappingWrapper(declaredParameterTypes[index])) {
if (declaredParameterTypes[index].isAssignableFrom(candidateParameterType)) {
return true;

2
src/main/java/org/springframework/data/repository/query/ResultProcessor.java

@ -159,7 +159,7 @@ public class ResultProcessor { @@ -159,7 +159,7 @@ public class ResultProcessor {
}
if(ReactiveWrapperConverters.supports(source.getClass())){
return (T) ReactiveWrapperConverters.map(source, o -> type.isInstance(o) ? o : converter.convert(o));
return (T) ReactiveWrapperConverters.map(source, converter::convert);
}
return (T) converter.convert(source);

76
src/main/java/org/springframework/data/repository/util/ReactiveWrapperConverters.java

@ -19,6 +19,7 @@ import static org.springframework.data.repository.util.ReactiveWrapperConverters @@ -19,6 +19,7 @@ import static org.springframework.data.repository.util.ReactiveWrapperConverters
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.reactivestreams.Publisher;
import org.springframework.core.ReactiveAdapterRegistry;
@ -54,7 +55,7 @@ import rx.Single; @@ -54,7 +55,7 @@ import rx.Single;
@UtilityClass
public class ReactiveWrapperConverters {
private static final List<AbstractReactiveWrapper<?>> REACTIVE_WRAPPERS = new ArrayList<>();
private static final List<ReactiveTypeWrapper<?>> REACTIVE_WRAPPERS = new ArrayList<>();
private static final GenericConversionService GENERIC_CONVERSION_SERVICE = new GenericConversionService();
static {
@ -194,12 +195,12 @@ public class ReactiveWrapperConverters { @@ -194,12 +195,12 @@ public class ReactiveWrapperConverters {
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T map(Object stream, Converter<Object, Object> converter) {
public static <T> T map(Object stream, Function<Object, Object> converter) {
Assert.notNull(stream, "Stream must not be null!");
Assert.notNull(converter, "Converter must not be null!");
for (AbstractReactiveWrapper<?> reactiveWrapper : REACTIVE_WRAPPERS) {
for (ReactiveTypeWrapper<?> reactiveWrapper : REACTIVE_WRAPPERS) {
if (ClassUtils.isAssignable(reactiveWrapper.getWrapperClass(), stream.getClass())) {
return (T) reactiveWrapper.map(stream, converter);
@ -214,11 +215,12 @@ public class ReactiveWrapperConverters { @@ -214,11 +215,12 @@ public class ReactiveWrapperConverters {
// -------------------------------------------------------------------------
/**
* Wrapper descriptor that can apply a {@link Converter} to map (convert) items inside its stream.
* Wrapper descriptor that can apply a {@link Function} to map items inside its stream.
*
* @author Mark Paluch
* @author Christoph Strobl
*/
private interface AbstractReactiveWrapper<T> {
private interface ReactiveTypeWrapper<T> {
/**
* @return the wrapper class.
@ -226,19 +228,19 @@ public class ReactiveWrapperConverters { @@ -226,19 +228,19 @@ public class ReactiveWrapperConverters {
Class<? super T> getWrapperClass();
/**
* Apply a {@link Converter} to a reactive type.
* Apply a {@link Function} to a reactive type.
*
* @param wrapper the reactive type, must not be {@literal null}.
* @param converter the converter, must not be {@literal null}.
* @param function the converter, must not be {@literal null}.
* @return the reactive type applying conversion.
*/
Object map(Object wrapper, Converter<Object, Object> converter);
Object map(Object wrapper, Function<Object, Object> function);
}
/**
* Wrapper for Project Reactor's {@link Mono}.
*/
private enum MonoWrapper implements AbstractReactiveWrapper<Mono<?>> {
private enum MonoWrapper implements ReactiveTypeWrapper<Mono<?>> {
INSTANCE;
@ -248,15 +250,15 @@ public class ReactiveWrapperConverters { @@ -248,15 +250,15 @@ public class ReactiveWrapperConverters {
}
@Override
public Mono<?> map(Object wrapper, Converter<Object, Object> converter) {
return ((Mono<?>) wrapper).map(converter::convert);
public Mono<?> map(Object wrapper, Function<Object, Object> function) {
return ((Mono<?>) wrapper).map(function::apply);
}
}
/**
* Wrapper for Project Reactor's {@link Flux}.
*/
private enum FluxWrapper implements AbstractReactiveWrapper<Flux<?>> {
private enum FluxWrapper implements ReactiveTypeWrapper<Flux<?>> {
INSTANCE;
@ -265,15 +267,15 @@ public class ReactiveWrapperConverters { @@ -265,15 +267,15 @@ public class ReactiveWrapperConverters {
return Flux.class;
}
public Flux<?> map(Object wrapper, Converter<Object, Object> converter) {
return ((Flux<?>) wrapper).map(converter::convert);
public Flux<?> map(Object wrapper, Function<Object, Object> function) {
return ((Flux<?>) wrapper).map(function::apply);
}
}
/**
* Wrapper for Reactive Stream's {@link Publisher}.
*/
private enum PublisherWrapper implements AbstractReactiveWrapper<Publisher<?>> {
private enum PublisherWrapper implements ReactiveTypeWrapper<Publisher<?>> {
INSTANCE;
@ -283,24 +285,24 @@ public class ReactiveWrapperConverters { @@ -283,24 +285,24 @@ public class ReactiveWrapperConverters {
}
@Override
public Publisher<?> map(Object wrapper, Converter<Object, Object> converter) {
public Publisher<?> map(Object wrapper, Function<Object, Object> function) {
if (wrapper instanceof Mono) {
return MonoWrapper.INSTANCE.map(wrapper, converter);
return MonoWrapper.INSTANCE.map(wrapper, function);
}
if (wrapper instanceof Flux) {
return FluxWrapper.INSTANCE.map(wrapper, converter);
return FluxWrapper.INSTANCE.map(wrapper, function);
}
return FluxWrapper.INSTANCE.map(Flux.from((Publisher<?>) wrapper), converter);
return FluxWrapper.INSTANCE.map(Flux.from((Publisher<?>) wrapper), function);
}
}
/**
* Wrapper for RxJava 1's {@link Single}.
*/
private enum RxJava1SingleWrapper implements AbstractReactiveWrapper<Single<?>> {
private enum RxJava1SingleWrapper implements ReactiveTypeWrapper<Single<?>> {
INSTANCE;
@ -310,15 +312,15 @@ public class ReactiveWrapperConverters { @@ -310,15 +312,15 @@ public class ReactiveWrapperConverters {
}
@Override
public Single<?> map(Object wrapper, Converter<Object, Object> converter) {
return ((Single<?>) wrapper).map(converter::convert);
public Single<?> map(Object wrapper, Function<Object, Object> function) {
return ((Single<?>) wrapper).map(function::apply);
}
}
/**
* Wrapper for RxJava 1's {@link Observable}.
*/
private enum RxJava1ObservableWrapper implements AbstractReactiveWrapper<Observable<?>> {
private enum RxJava1ObservableWrapper implements ReactiveTypeWrapper<Observable<?>> {
INSTANCE;
@ -328,15 +330,15 @@ public class ReactiveWrapperConverters { @@ -328,15 +330,15 @@ public class ReactiveWrapperConverters {
}
@Override
public Observable<?> map(Object wrapper, Converter<Object, Object> converter) {
return ((Observable<?>) wrapper).map(converter::convert);
public Observable<?> map(Object wrapper, Function<Object, Object> function) {
return ((Observable<?>) wrapper).map(function::apply);
}
}
/**
* Wrapper for RxJava 2's {@link io.reactivex.Single}.
*/
private enum RxJava2SingleWrapper implements AbstractReactiveWrapper<io.reactivex.Single<?>> {
private enum RxJava2SingleWrapper implements ReactiveTypeWrapper<io.reactivex.Single<?>> {
INSTANCE;
@ -346,15 +348,15 @@ public class ReactiveWrapperConverters { @@ -346,15 +348,15 @@ public class ReactiveWrapperConverters {
}
@Override
public io.reactivex.Single<?> map(Object wrapper, Converter<Object, Object> converter) {
return ((io.reactivex.Single<?>) wrapper).map(converter::convert);
public io.reactivex.Single<?> map(Object wrapper, Function<Object, Object> function) {
return ((io.reactivex.Single<?>) wrapper).map(function::apply);
}
}
/**
* Wrapper for RxJava 2's {@link io.reactivex.Maybe}.
*/
private enum RxJava2MaybeWrapper implements AbstractReactiveWrapper<io.reactivex.Maybe<?>> {
private enum RxJava2MaybeWrapper implements ReactiveTypeWrapper<Maybe<?>> {
INSTANCE;
@ -364,15 +366,15 @@ public class ReactiveWrapperConverters { @@ -364,15 +366,15 @@ public class ReactiveWrapperConverters {
}
@Override
public io.reactivex.Maybe<?> map(Object wrapper, Converter<Object, Object> converter) {
return ((io.reactivex.Maybe<?>) wrapper).map(converter::convert);
public io.reactivex.Maybe<?> map(Object wrapper, Function<Object, Object> function) {
return ((io.reactivex.Maybe<?>) wrapper).map(function::apply);
}
}
/**
* Wrapper for RxJava 2's {@link io.reactivex.Observable}.
*/
private enum RxJava2ObservableWrapper implements AbstractReactiveWrapper<io.reactivex.Observable<?>> {
private enum RxJava2ObservableWrapper implements ReactiveTypeWrapper<io.reactivex.Observable<?>> {
INSTANCE;
@ -382,15 +384,15 @@ public class ReactiveWrapperConverters { @@ -382,15 +384,15 @@ public class ReactiveWrapperConverters {
}
@Override
public io.reactivex.Observable<?> map(Object wrapper, Converter<Object, Object> converter) {
return ((io.reactivex.Observable<?>) wrapper).map(converter::convert);
public io.reactivex.Observable<?> map(Object wrapper, Function<Object, Object> function) {
return ((io.reactivex.Observable<?>) wrapper).map(function::apply);
}
}
/**
* Wrapper for RxJava 2's {@link io.reactivex.Flowable}.
*/
private enum RxJava2FlowableWrapper implements AbstractReactiveWrapper<io.reactivex.Flowable<?>> {
private enum RxJava2FlowableWrapper implements ReactiveTypeWrapper<Flowable<?>> {
INSTANCE;
@ -400,8 +402,8 @@ public class ReactiveWrapperConverters { @@ -400,8 +402,8 @@ public class ReactiveWrapperConverters {
}
@Override
public io.reactivex.Flowable<?> map(Object wrapper, Converter<Object, Object> converter) {
return ((io.reactivex.Flowable<?>) wrapper).map(converter::convert);
public io.reactivex.Flowable<?> map(Object wrapper, Function<Object, Object> function) {
return ((io.reactivex.Flowable<?>) wrapper).map(function::apply);
}
}

7
src/main/java/org/springframework/data/repository/util/ReactiveWrappers.java

@ -43,6 +43,7 @@ import rx.Single; @@ -43,6 +43,7 @@ import rx.Single;
* multiplicity and whether a reactive wrapper type is acceptable for a specific operation.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.0
* @see org.reactivestreams.Publisher
* @see rx.Single
@ -72,7 +73,7 @@ public class ReactiveWrappers { @@ -72,7 +73,7 @@ public class ReactiveWrappers {
static {
Map<Class<?>, Descriptor> reactiveWrappers = new LinkedHashMap<>(3);
Map<Class<?>, Descriptor> reactiveWrappers = new LinkedHashMap<>(5);
if (RXJAVA1_PRESENT) {
@ -140,6 +141,10 @@ public class ReactiveWrappers { @@ -140,6 +141,10 @@ public class ReactiveWrappers {
* @return {@literal true} if the {@code type} is a supported reactive wrapper type.
*/
public static boolean supports(Class<?> type) {
return isWrapper(ClassUtils.getUserClass(type));
}
private static boolean isWrapper(Class<?> type) {
return isNoValueType(type) || isSingleValueType(type) || isMultiValueType(type);
}

Loading…
Cancel
Save