Introduce GenericTypeResolver#resolveReturnTypeArgument

Issue: SPR-8514
This commit is contained in:
Chris Beams
2011-07-06 09:15:32 +00:00
parent 7c25c84ee2
commit 605f0e7a22
2 changed files with 48 additions and 0 deletions

View File

@@ -24,6 +24,8 @@ import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -102,6 +104,35 @@ public abstract class GenericTypeResolver {
return (rawType instanceof Class ? (Class) rawType : method.getReturnType());
}
/**
* Resolve the single type argument of the given generic interface against the given
* target method which is assumed to return the given interface or an implementation
* of it.
* @param method the target method to check the return type of
* @param genericIfc the generic interface or superclass to resolve the type argument from
* @return the resolved parameter type of the method return type, or <code>null</code>
* if not resolvable or if the single argument is of type {@link WildcardType}.
*/
public static Class<?> resolveReturnTypeArgument(Method method, Class<?> genericIfc) {
Type returnType = method.getReturnType();
Type genericReturnType = method.getGenericReturnType();
ParameterizedType targetType;
if (returnType.equals(genericIfc)) {
if (genericReturnType instanceof ParameterizedType) {
targetType = (ParameterizedType)genericReturnType;
Type[] actualTypeArguments = targetType.getActualTypeArguments();
Type typeArg = actualTypeArguments[0];
if (!(typeArg instanceof WildcardType)) {
return (Class<?>)typeArg;
}
}
else {
return null;
}
}
return GenericTypeResolver.resolveTypeArgument((Class<?>)returnType, genericIfc);
}
/**
* Resolve the single type argument of the given generic interface against
* the given target class which is assumed to implement the generic interface