Infer return type of parametrized factory methods

Currently, if a factory method is parameterized and the corresponding
variable types are declared on the method itself instead of on the
enclosing class or interface, Spring always predicts the return type to
be Object, even if the return type can be explicitly inferred from the
method signature and supplied arguments (which are available in the bean
definition).

This commit introduces a new resolveParameterizedReturnType() method in
GenericTypeResolver that attempts to infer the concrete type for the
generic return type of a given parameterized method, falling back to the
standard return type if necessary. Furthermore,
AbstractAutowireCapableBeanFactory now delegates to
resolveParameterizedReturnType() when predicting the return type for
factory methods.

resolveParameterizedReturnType() is capable of inferring the concrete
type for return type T for method signatures similar to the following.
Such methods may potentially be static. Also, the formal argument list
for such methods is not limited to a single argument.

 - public <T> T foo(Class<T> clazz)
 - public <T> T foo(Object obj, Class<T> clazz)
 - public <V, T> T foo(V obj, Class<T> clazz)
 - public <T> T foo(T obj)

Issue: SPR-9493
This commit is contained in:
Sam Brannen
2012-06-13 00:39:56 +02:00
parent 9fc05a80d0
commit c461455c7c
7 changed files with 341 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -30,7 +30,11 @@ import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Helper class for resolving generic types against type variables.
@@ -40,11 +44,14 @@ import org.springframework.util.Assert;
*
* @author Juergen Hoeller
* @author Rob Harrop
* @author Sam Brannen
* @since 2.5.2
* @see GenericCollectionTypeResolver
*/
public abstract class GenericTypeResolver {
private static final Log logger = LogFactory.getLog(GenericTypeResolver.class);
/** Cache from Class to TypeVariable Map */
private static final Map<Class, Reference<Map<TypeVariable, Type>>> typeVariableCache =
Collections.synchronizedMap(new WeakHashMap<Class, Reference<Map<TypeVariable, Type>>>());
@@ -88,18 +95,144 @@ public abstract class GenericTypeResolver {
}
/**
* Determine the target type for the generic return type of the given method.
* Determine the target type for the generic return type of the given method,
* where the type variable is declared on the given class.
*
* @param method the method to introspect
* @param clazz the class to resolve type variables against
* @return the corresponding generic parameter or return type
* @see #resolveParameterizedReturnType
*/
public static Class<?> resolveReturnType(Method method, Class clazz) {
public static Class<?> resolveReturnType(Method method, Class<?> clazz) {
Assert.notNull(method, "Method must not be null");
Type genericType = method.getGenericReturnType();
Assert.notNull(clazz, "Class must not be null");
Map<TypeVariable, Type> typeVariableMap = getTypeVariableMap(clazz);
Type rawType = getRawType(genericType, typeVariableMap);
return (rawType instanceof Class ? (Class) rawType : method.getReturnType());
return (rawType instanceof Class ? (Class<?>) rawType : method.getReturnType());
}
/**
* Determine the target type for the generic return type of the given
* <em>parameterized</em> method, where the type variable is declared
* on the given method.
*
* <p>For example, given a factory method with the following signature,
* if {@code resolveParameterizedReturnType()} is invoked with the reflected
* method for {@code creatProxy()} and an {@code Object[]} array containing
* {@code MyService.class}, {@code resolveParameterizedReturnType()} will
* infer that the target return type is {@code MyService}.
*
* <pre>{@code public static <T> T createProxy(Class<T> clazz)}</pre>
*
* <h4>Possible Return Values</h4>
* <ul>
* <li>the target return type if it can be inferred</li>
* <li>the {@link Method#getReturnType() standard return type}, if
* the given {@code method} does not declare any {@link
* Method#getTypeParameters() generic types}</li>
* <li>the {@link Method#getReturnType() standard return type}, if the
* target return type cannot be inferred (e.g., due to type erasure)</li>
* <li>{@code null}, if the length of the given arguments array is shorter
* than the length of the {@link
* Method#getGenericParameterTypes() formal argument list} for the given
* method</li>
* </ul>
*
* @param method the method to introspect, never {@code null}
* @param args the arguments that will be supplied to the method when it is
* invoked, never {@code null}
* @return the resolved target return type, the standard return type, or
* {@code null}
* @since 3.2
* @see #resolveReturnType
*/
public static Class<?> resolveParameterizedReturnType(Method method, Object[] args) {
Assert.notNull(method, "method must not be null");
Assert.notNull(args, "args must not be null");
final TypeVariable<Method>[] declaredGenericTypes = method.getTypeParameters();
final Type genericReturnType = method.getGenericReturnType();
final Type[] genericArgumentTypes = method.getGenericParameterTypes();
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"Resolving parameterized return type for [%s] with concrete method arguments [%s].",
method.toGenericString(), ObjectUtils.nullSafeToString(args)));
}
// No declared generic types to inspect, so just return the standard return type.
if (declaredGenericTypes.length == 0) {
return method.getReturnType();
}
// The supplied argument list is too short for the method's signature, so
// return null, since such a method invocation would fail.
if (args.length < genericArgumentTypes.length) {
return null;
}
// Ensure that the generic type is declared directly on the method
// itself, not on the enclosing class or interface.
boolean locallyDeclaredGenericTypeMatchesReturnType = false;
for (TypeVariable<Method> currentType : declaredGenericTypes) {
if (currentType.equals(genericReturnType)) {
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"Found declared generic type [%s] that matches the target return type [%s].",
currentType, genericReturnType));
}
locallyDeclaredGenericTypeMatchesReturnType = true;
break;
}
}
if (locallyDeclaredGenericTypeMatchesReturnType) {
for (int i = 0; i < genericArgumentTypes.length; i++) {
final Type currentArgumentType = genericArgumentTypes[i];
if (currentArgumentType.equals(genericReturnType)) {
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"Found generic method argument at index [%s] that matches the target return type.", i));
}
return args[i].getClass();
}
if (currentArgumentType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) currentArgumentType;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
for (int j = 0; j < actualTypeArguments.length; j++) {
final Type typeArg = actualTypeArguments[j];
if (typeArg.equals(genericReturnType)) {
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"Found method argument at index [%s] that is parameterized with a type that matches the target return type.",
i));
}
if (args[i] instanceof Class) {
return (Class<?>) args[i];
} else {
// Consider adding logic to determine the class of the
// J'th typeArg, if possible.
logger.info(String.format(
"Could not determine the target type for parameterized type [%s] for method [%s].",
typeArg, method.toGenericString()));
// For now, just fall back...
return method.getReturnType();
}
}
}
}
}
}
// Fall back...
return method.getReturnType();
}
/**
@@ -128,7 +261,7 @@ public abstract class GenericTypeResolver {
return null;
}
}
return GenericTypeResolver.resolveTypeArgument((Class<?>) returnType, genericIfc);
return resolveTypeArgument((Class<?>) returnType, genericIfc);
}
/**
@@ -186,7 +319,7 @@ public abstract class GenericTypeResolver {
}
return null;
}
private static Class[] doResolveTypeArguments(Class ownerClass, Type ifc, Class genericIfc) {
if (ifc instanceof ParameterizedType) {
ParameterizedType paramIfc = (ParameterizedType) ifc;
@@ -236,7 +369,6 @@ public abstract class GenericTypeResolver {
return (arg instanceof Class ? (Class) arg : Object.class);
}
/**
* Resolve the specified generic type against the given TypeVariable map.
* @param genericType the generic type to resolve
@@ -272,9 +404,9 @@ public abstract class GenericTypeResolver {
}
/**
* Build a mapping of {@link TypeVariable#getName TypeVariable names} to concrete
* {@link Class} for the specified {@link Class}. Searches all super types,
* enclosing types and interfaces.
* Build a mapping of {@link TypeVariable#getName TypeVariable names} to
* {@link Class concrete classes} for the specified {@link Class}. Searches
* all super types, enclosing types and interfaces.
*/
public static Map<TypeVariable, Type> getTypeVariableMap(Class clazz) {
Reference<Map<TypeVariable, Type>> ref = typeVariableCache.get(clazz);