public abstract class GenericTypeResolver
extends java.lang.Object
Mainly intended for usage within the framework, resolving method parameter types even when they are declared generically.
GenericCollectionTypeResolver
Modifier and Type | Field and Description |
---|---|
private static Log |
logger |
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 and Description |
---|
GenericTypeResolver() |
Modifier and Type | Method and Description |
---|---|
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 classes 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,
where formal type variables are declared on the given class.
|
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<?> |
resolveReturnTypeForGenericMethod(java.lang.reflect.Method method,
java.lang.Object[] args)
Determine the target type for the generic return type of the given
generic method, where formal type variables are declared on
the given method itself.
|
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.
|
private static final Log logger
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
public static java.lang.reflect.Type getTargetType(MethodParameter methodParam)
methodParam
- the method parameter specificationpublic static java.lang.Class<?> resolveParameterType(MethodParameter methodParam, java.lang.Class clazz)
methodParam
- the method parameter specificationclazz
- the class to resolve type variables againstpublic static java.lang.Class<?> resolveReturnType(java.lang.reflect.Method method, java.lang.Class<?> clazz)
method
- the method to introspectclazz
- the class to resolve type variables againstresolveReturnTypeForGenericMethod(java.lang.reflect.Method, java.lang.Object[])
public static java.lang.Class<?> resolveReturnTypeForGenericMethod(java.lang.reflect.Method method, java.lang.Object[] args)
For example, given a factory method with the following signature,
if resolveReturnTypeForGenericMethod()
is invoked with the reflected
method for creatProxy()
and an Object[]
array containing
MyService.class
, resolveReturnTypeForGenericMethod()
will
infer that the target return type is MyService
.
public static <T> T createProxy(Class<T> clazz)
method
does not declare any formal type variablesnull
, if the length of the given arguments array is shorter
than the length of the formal argument list for the given
methodmethod
- the method to introspect, never null
args
- the arguments that will be supplied to the method when it is
invoked, never null
null
resolveReturnType(java.lang.reflect.Method, java.lang.Class<?>)
public static java.lang.Class<?> resolveReturnTypeArgument(java.lang.reflect.Method method, java.lang.Class<?> genericIfc)
method
- the target method to check the return type ofgenericIfc
- the generic interface or superclass to resolve the type argument fromnull
if not resolvable or if the single argument is of type WildcardType
.public static java.lang.Class<?> resolveTypeArgument(java.lang.Class clazz, java.lang.Class genericIfc)
clazz
- the target class to check againstgenericIfc
- the generic interface or superclass to resolve the type argument fromnull
if not resolvablepublic static java.lang.Class[] resolveTypeArguments(java.lang.Class clazz, java.lang.Class genericIfc)
clazz
- the target class to check againstgenericIfc
- the generic interface or superclass to resolve the type argument fromnull
if not resolvableprivate 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)
private static java.lang.Class extractClass(java.lang.Class ownerClass, java.lang.reflect.Type arg)
public static java.lang.Class<?> resolveType(java.lang.reflect.Type genericType, java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)
genericType
- the generic type to resolvetypeVariableMap
- the TypeVariable Map to resolved againstObject.class
otherwisestatic java.lang.reflect.Type getRawType(java.lang.reflect.Type genericType, java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)
genericType
- the generic type to resolvetypeVariableMap
- the TypeVariable Map to resolved againstpublic static java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> getTypeVariableMap(java.lang.Class clazz)
TypeVariable names
to
concrete classes
for the specified Class
. Searches
all super types, enclosing types and interfaces.static java.lang.reflect.Type extractBoundForTypeVariable(java.lang.reflect.TypeVariable typeVariable)
Type
for a given TypeVariable
.private static void extractTypeVariablesFromGenericInterfaces(java.lang.reflect.Type[] genericInterfaces, java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)
private static void populateTypeMapFromParameterizedType(java.lang.reflect.ParameterizedType type, java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> typeVariableMap)