DATACMNS-1246 - Minor optimization in QueryExecutorMethodInterceptor.

We now prevent the superfluous creation of a MethodParameter and TypeDescriptor instance in repository method execution in case the value to be returned already is an instance of the expected method return type.
This commit is contained in:
Oliver Gierke
2018-01-18 17:22:43 +01:00
parent 06116b7726
commit c777363f3d
3 changed files with 51 additions and 41 deletions

View File

@@ -15,10 +15,12 @@
*/
package org.springframework.data.repository.core.support;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Optional;
import org.springframework.core.CollectionFactory;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
@@ -50,6 +52,26 @@ class QueryExecutionResultHandler {
this.conversionService = conversionService;
}
/**
* Post-processes the given result of a query invocation to match the return type of the given method.
*
* @param result can be {@literal null}.
* @param metho must not be {@literal null}.
* @return
*/
@Nullable
public Object postProcessInvocationResult(@Nullable Object result, Method method) {
if (method.getReturnType().isInstance(result)) {
return result;
}
MethodParameter parameter = new MethodParameter(method, -1);
TypeDescriptor methodReturnTypeDescriptor = TypeDescriptor.nested(parameter, 0);
return postProcessInvocationResult(result, methodReturnTypeDescriptor);
}
/**
* Post-processes the given result of a query invocation to the given type.
*
@@ -58,7 +80,7 @@ class QueryExecutionResultHandler {
* @return
*/
@Nullable
public Object postProcessInvocationResult(@Nullable Object result, @Nullable TypeDescriptor returnTypeDescriptor) {
Object postProcessInvocationResult(@Nullable Object result, @Nullable TypeDescriptor returnTypeDescriptor) {
if (returnTypeDescriptor == null) {
return result;

View File

@@ -41,9 +41,7 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
@@ -579,12 +577,7 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware,
Object result = doInvoke(invocation);
// Looking up the TypeDescriptor for the return type - yes, this way o.O
Method method = invocation.getMethod();
MethodParameter parameter = new MethodParameter(method, -1);
TypeDescriptor methodReturnTypeDescriptor = TypeDescriptor.nested(parameter, 0);
return resultHandler.postProcessInvocationResult(result, methodReturnTypeDescriptor);
return resultHandler.postProcessInvocationResult(result, invocation.getMethod());
}
@Nullable