DATACMNS-867 - Update forward ported code.

Polished method lookup in DefaultRepositoryInformation using Optionals.firstNonEmpty(…). Optimized creation of JavaSlang's Option type as we don't have to rely on reflection to call static methods on interfaces on Java 8. AssertJ updates in corresponding tests.
This commit is contained in:
Oliver Gierke
2016-11-24 18:44:36 +01:00
parent 2d282b2d7f
commit 1ad82cb82d
5 changed files with 21 additions and 34 deletions

View File

@@ -32,6 +32,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
@@ -41,6 +42,7 @@ import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.CrudMethods;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.util.Optionals;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.data.util.Streamable;
import org.springframework.util.Assert;
@@ -238,15 +240,17 @@ class DefaultRepositoryInformation implements RepositoryInformation {
* @return
*/
Method getTargetClassMethod(Method method, Optional<Class<?>> baseClass) {
return baseClass.map(it -> {
return Optional.ofNullable(findMethod(it, method.getName(), method.getParameterTypes())).orElseGet(() -> {
return Arrays.stream(it.getMethods())//
.filter(baseClassMethod -> method.getName().equals(baseClassMethod.getName()))// Right name
.filter(baseClassMethod -> method.getParameterTypes().length == baseClassMethod.getParameterTypes().length)
.filter(baseClassMethod -> parametersMatch(method, baseClassMethod))// All parameters match
.findFirst().orElse(method);
});
}).orElse(method);
Supplier<Optional<Method>> directMatch = () -> baseClass
.map(it -> findMethod(it, method.getName(), method.getParameterTypes()));
Supplier<Optional<Method>> detailedComparison = () -> baseClass.flatMap(it -> Arrays.stream(it.getMethods())//
.filter(baseClassMethod -> method.getName().equals(baseClassMethod.getName()))// Right name
.filter(baseClassMethod -> method.getParameterTypes().length == baseClassMethod.getParameterTypes().length)
.filter(baseClassMethod -> parametersMatch(method, baseClassMethod))// All parameters match
.findFirst());
return Optionals.firstNonEmpty(directMatch, detailedComparison).orElse(method);
}
/*

View File

@@ -25,8 +25,6 @@ import scala.Function0;
import scala.Option;
import scala.runtime.AbstractFunction0;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
@@ -46,7 +44,6 @@ import org.springframework.data.domain.Slice;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.concurrent.ListenableFuture;
import com.google.common.base.Optional;
@@ -479,21 +476,13 @@ public abstract class QueryExecutionConverters {
*/
private static class NullableWrapperToJavaslangOptionConverter extends AbstractWrapperTypeConverter {
private static final Method OF_METHOD;
private static final Method NONE_METHOD;
static {
OF_METHOD = ReflectionUtils.findMethod(javaslang.control.Option.class, "of", Object.class);
NONE_METHOD = ReflectionUtils.findMethod(javaslang.control.Option.class, "none");
}
/**
* Creates a new {@link NullableWrapperToJavaslangOptionConverter} using the given {@link ConversionService}.
*
* @param conversionService must not be {@literal null}.
*/
public NullableWrapperToJavaslangOptionConverter(ConversionService conversionService) {
super(conversionService, createEmptyOption(), javaslang.control.Option.class);
super(conversionService, javaslang.control.Option.none(), getWrapperType());
}
public static WrapperType getWrapperType() {
@@ -505,14 +494,8 @@ public abstract class QueryExecutionConverters {
* @see org.springframework.data.repository.util.QueryExecutionConverters.AbstractWrapperTypeConverter#wrap(java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
protected Object wrap(Object source) {
return (javaslang.control.Option<Object>) ReflectionUtils.invokeMethod(OF_METHOD, null, source);
}
@SuppressWarnings("unchecked")
private static javaslang.control.Option<Object> createEmptyOption() {
return (javaslang.control.Option<Object>) ReflectionUtils.invokeMethod(NONE_METHOD, null);
return javaslang.control.Option.of(source);
}
}