org.springframework.core
Class GenericTypeResolver

java.lang.Object
  extended by org.springframework.core.GenericTypeResolver

public abstract class GenericTypeResolver
extends java.lang.Object

Helper class for resolving generic types against type variables.

Mainly intended for usage within the framework, resolving method parameter types even when they are declared generically.

Since:
2.5.2
Author:
Juergen Hoeller, Rob Harrop
See Also:
GenericCollectionTypeResolver

Field Summary
private static java.util.Map<java.lang.Class,java.lang.ref.Reference<java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type>>> typeVariableCache
          Cache from Class to TypeVariable Map
 
Constructor Summary
GenericTypeResolver()
           
 
Method Summary
private static java.lang.Class[] doResolveTypeArguments(java.lang.Class ownerClass, java.lang.Class classToIntrospect, java.lang.Class genericIfc)
           
private static java.lang.Class[] doResolveTypeArguments(java.lang.Class ownerClass, java.lang.reflect.Type ifc, java.lang.Class genericIfc)
           
(package private) static java.lang.reflect.Type extractBoundForTypeVariable(java.lang.reflect.TypeVariable typeVariable)
          Extracts the bound Type for a given TypeVariable.
private static java.lang.Class extractClass(java.lang.Class ownerClass, java.lang.reflect.Type arg)
          Extract a class instance from given Type.
private static void extractTypeVariablesFromGenericInterfaces(java.lang.reflect.Type[] genericInterfaces, java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)
           
(package private) static java.lang.reflect.Type getRawType(java.lang.reflect.Type genericType, java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)
          Determine the raw type for the given generic parameter type.
static java.lang.reflect.Type getTargetType(MethodParameter methodParam)
          Determine the target type for the given parameter specification.
static java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> getTypeVariableMap(java.lang.Class clazz)
          Build a mapping of TypeVariable names to concrete Class for the specified Class.
private static void populateTypeMapFromParameterizedType(java.lang.reflect.ParameterizedType type, java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)
          Read the TypeVariables from the supplied ParameterizedType and add mappings corresponding to the TypeVariable name -> concrete type to the supplied Map.
static java.lang.Class<?> resolveParameterType(MethodParameter methodParam, java.lang.Class clazz)
          Determine the target type for the given generic parameter type.
static java.lang.Class<?> resolveReturnType(java.lang.reflect.Method method, java.lang.Class clazz)
          Determine the target type for the generic return type of the given method.
static java.lang.Class<?> resolveReturnTypeArgument(java.lang.reflect.Method method, java.lang.Class<?> genericIfc)
          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.
static java.lang.Class resolveType(java.lang.reflect.Type genericType, java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)
          Resolve the specified generic type against the given TypeVariable map.
static java.lang.Class<?> resolveTypeArgument(java.lang.Class clazz, java.lang.Class genericIfc)
          Resolve the single type argument of the given generic interface against the given target class which is assumed to implement the generic interface and possibly declare a concrete type for its type variable.
static java.lang.Class[] resolveTypeArguments(java.lang.Class clazz, java.lang.Class genericIfc)
          Resolve the type arguments of the given generic interface against the given target class which is assumed to implement the generic interface and possibly declare concrete types for its type variables.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

typeVariableCache

private static final java.util.Map<java.lang.Class,java.lang.ref.Reference<java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type>>> typeVariableCache
Cache from Class to TypeVariable Map

Constructor Detail

GenericTypeResolver

public GenericTypeResolver()
Method Detail

getTargetType

public static java.lang.reflect.Type getTargetType(MethodParameter methodParam)
Determine the target type for the given parameter specification.

Parameters:
methodParam - the method parameter specification
Returns:
the corresponding generic parameter type

resolveParameterType

public static java.lang.Class<?> resolveParameterType(MethodParameter methodParam,
                                                      java.lang.Class clazz)
Determine the target type for the given generic parameter type.

Parameters:
methodParam - the method parameter specification
clazz - the class to resolve type variables against
Returns:
the corresponding generic parameter or return type

resolveReturnType

public static java.lang.Class<?> resolveReturnType(java.lang.reflect.Method method,
                                                   java.lang.Class clazz)
Determine the target type for the generic return type of the given method.

Parameters:
method - the method to introspect
clazz - the class to resolve type variables against
Returns:
the corresponding generic parameter or return type

resolveReturnTypeArgument

public static java.lang.Class<?> resolveReturnTypeArgument(java.lang.reflect.Method method,
                                                           java.lang.Class<?> genericIfc)
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.

Parameters:
method - the target method to check the return type of
genericIfc - the generic interface or superclass to resolve the type argument from
Returns:
the resolved parameter type of the method return type, or null if not resolvable or if the single argument is of type WildcardType.

resolveTypeArgument

public static java.lang.Class<?> resolveTypeArgument(java.lang.Class clazz,
                                                     java.lang.Class genericIfc)
Resolve the single type argument of the given generic interface against the given target class which is assumed to implement the generic interface and possibly declare a concrete type for its type variable.

Parameters:
clazz - the target class to check against
genericIfc - the generic interface or superclass to resolve the type argument from
Returns:
the resolved type of the argument, or null if not resolvable

resolveTypeArguments

public static java.lang.Class[] resolveTypeArguments(java.lang.Class clazz,
                                                     java.lang.Class genericIfc)
Resolve the type arguments of the given generic interface against the given target class which is assumed to implement the generic interface and possibly declare concrete types for its type variables.

Parameters:
clazz - the target class to check against
genericIfc - the generic interface or superclass to resolve the type argument from
Returns:
the resolved type of each argument, with the array size matching the number of actual type arguments, or null if not resolvable

doResolveTypeArguments

private static java.lang.Class[] doResolveTypeArguments(java.lang.Class ownerClass,
                                                        java.lang.Class classToIntrospect,
                                                        java.lang.Class genericIfc)

doResolveTypeArguments

private static java.lang.Class[] doResolveTypeArguments(java.lang.Class ownerClass,
                                                        java.lang.reflect.Type ifc,
                                                        java.lang.Class genericIfc)

extractClass

private static java.lang.Class extractClass(java.lang.Class ownerClass,
                                            java.lang.reflect.Type arg)
Extract a class instance from given Type.


resolveType

public static java.lang.Class resolveType(java.lang.reflect.Type genericType,
                                          java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)
Resolve the specified generic type against the given TypeVariable map.

Parameters:
genericType - the generic type to resolve
typeVariableMap - the TypeVariable Map to resolved against
Returns:
the type if it resolves to a Class, or Object.class otherwise

getRawType

static java.lang.reflect.Type getRawType(java.lang.reflect.Type genericType,
                                         java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)
Determine the raw type for the given generic parameter type.

Parameters:
genericType - the generic type to resolve
typeVariableMap - the TypeVariable Map to resolved against
Returns:
the resolved raw type

getTypeVariableMap

public static java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> getTypeVariableMap(java.lang.Class clazz)
Build a mapping of TypeVariable names to concrete Class for the specified Class. Searches all super types, enclosing types and interfaces.


extractBoundForTypeVariable

static java.lang.reflect.Type extractBoundForTypeVariable(java.lang.reflect.TypeVariable typeVariable)
Extracts the bound Type for a given TypeVariable.


extractTypeVariablesFromGenericInterfaces

private static void extractTypeVariablesFromGenericInterfaces(java.lang.reflect.Type[] genericInterfaces,
                                                              java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)

populateTypeMapFromParameterizedType

private static void populateTypeMapFromParameterizedType(java.lang.reflect.ParameterizedType type,
                                                         java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)
Read the TypeVariables from the supplied ParameterizedType and add mappings corresponding to the TypeVariable name -> concrete type to the supplied Map.

Consider this case: