From 3c96e4e079c0ba02024d43a15388d49cbf5f4614 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 24 Nov 2017 10:53:47 +0100 Subject: [PATCH] DATACMNS-1158 - Polishing. Some code reorganization, polishing of nullable annotations, JavaDoc. Original pull request: #243. Related issue: DATAJPA-1173. --- .../support/RepositoryFactorySupport.java | 35 +++++++++++-------- .../org/springframework/data/util/Pair.java | 6 ++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java index a27049135..20778cc8e 100644 --- a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java +++ b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java @@ -34,7 +34,6 @@ import java.util.stream.Collectors; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; -import org.jetbrains.annotations.NotNull; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.interceptor.ExposeInvocationInterceptor; import org.springframework.beans.BeanUtils; @@ -315,10 +314,9 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware, postProcessors.forEach(processor -> processor.postProcess(result, information)); result.addAdvice(new DefaultMethodInvokingMethodInterceptor()); - result.addAdvice(new QueryExecutorMethodInterceptor( // - information, // - getProjectionFactory(classLoader, beanFactory) // - )); + + ProjectionFactory projectionFactory = getProjectionFactory(classLoader, beanFactory); + result.addAdvice(new QueryExecutorMethodInterceptor(information, projectionFactory)); composition = composition.append(RepositoryFragment.implemented(target)); result.addAdvice(new ImplementationMethodExecutionInterceptor(composition)); @@ -326,7 +324,13 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware, return (T) result.getProxy(classLoader); } - @NotNull + /** + * Returns the {@link ProjectionFactory} to be used with the repository instances created. + * + * @param classLoader will never be {@literal null}. + * @param beanFactory will never be {@literal null}. + * @return will never be {@literal null}. + */ protected ProjectionFactory getProjectionFactory(ClassLoader classLoader, BeanFactory beanFactory) { SpelAwareProxyProjectionFactory factory = new SpelAwareProxyProjectionFactory(); @@ -532,22 +536,25 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware, + "infrastructure apparently does not support query methods!"); } - this.queries = lookupStrategy.map( // - it -> mapMethodsToQuery(repositoryInformation, projectionFactory, it) // - ).orElse(Collections.emptyMap()); + this.queries = lookupStrategy // + .map(it -> mapMethodsToQuery(repositoryInformation, it, projectionFactory)) // + .orElse(Collections.emptyMap()); } private Map mapMethodsToQuery(RepositoryInformation repositoryInformation, - ProjectionFactory projectionFactory, QueryLookupStrategy lookupStrategy) { + QueryLookupStrategy lookupStrategy, ProjectionFactory projectionFactory) { return repositoryInformation.getQueryMethods().stream() // - .map(method -> Pair.of( // - method, // - lookupStrategy.resolveQuery(method, repositoryInformation, projectionFactory, namedQueries))) // + .map(method -> lookupQuery(method, repositoryInformation, lookupStrategy, projectionFactory)) // .peek(pair -> invokeListeners(pair.getSecond())) // .collect(Pair.toMap()); } + private Pair lookupQuery(Method method, RepositoryInformation information, + QueryLookupStrategy strategy, ProjectionFactory projectionFactory) { + return Pair.of(method, strategy.resolveQuery(method, information, projectionFactory, namedQueries)); + } + @SuppressWarnings({ "rawtypes", "unchecked" }) private void invokeListeners(RepositoryQuery query) { @@ -580,6 +587,7 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware, return resultHandler.postProcessInvocationResult(result, methodReturnTypeDescriptor); } + @Nullable private Object doInvoke(MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); @@ -603,7 +611,6 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware, } } - /** * Method interceptor that calls methods on the {@link RepositoryComposition}. * diff --git a/src/main/java/org/springframework/data/util/Pair.java b/src/main/java/org/springframework/data/util/Pair.java index 602b43d29..6346b710a 100644 --- a/src/main/java/org/springframework/data/util/Pair.java +++ b/src/main/java/org/springframework/data/util/Pair.java @@ -24,6 +24,7 @@ import lombok.ToString; import java.util.Map; import java.util.stream.Collector; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * A tuple of things. @@ -72,6 +73,11 @@ public final class Pair { return second; } + /** + * A collector to create a {@link Map} from a {@link Stream} of {@link Pair}s. + * + * @return + */ public static Collector, ?, Map> toMap() { return Collectors.toMap(Pair::getFirst, Pair::getSecond); }