org.springframework.core

## Class GenericTypeResolver

- java.lang.Object
- - org.springframework.core.GenericTypeResolver

    public abstract class GenericTypeResolver
    extends 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, Sam Brannen
See Also:
  `GenericCollectionTypeResolver`

### Constructor Summary

Constructors
Constructor and Description
`GenericTypeResolver()`

### Method Summary

Methods
`static Type` `getTargetType(MethodParameter methodParam)` Determine the target type for the given parameter specification.
`static Map<TypeVariable,Type>` `getTypeVariableMap(Class clazz)` Build a mapping of `TypeVariable names` to `concrete classes` for the specified `Class`.
`static Class<?>` `resolveParameterType(MethodParameter methodParam, Class clazz)` Determine the target type for the given generic parameter type.
`static Class<?>` `resolveReturnType(Method method, 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 Class<?>` `resolveReturnTypeArgument(Method method, 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 Class<?>` `resolveReturnTypeForGenericMethod(Method method, 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 Class<?>` `resolveType(Type genericType, Map<TypeVariable,Type> typeVariableMap)` Resolve the specified generic type against the given TypeVariable map.
`static Class<?>` `resolveTypeArgument(Class clazz, 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 Class[]` `resolveTypeArguments(Class clazz, 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`

### Constructor Detail

#### GenericTypeResolver

    public GenericTypeResolver()

### Method Detail

#### getTargetType

    public static 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 Class<?> resolveParameterType(MethodParameter methodParam,
                                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 Class<?> resolveReturnType(Method method,
                             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.

Parameters:
  `method` - the method to introspect
  `clazz` - the class to resolve type variables against
Returns:
  the corresponding generic parameter or return type
See Also:
  `resolveReturnTypeForGenericMethod(java.lang.reflect.Method, java.lang.Object[])`

#### resolveReturnTypeForGenericMethod

    public static Class<?> resolveReturnTypeForGenericMethod(Method method,
                                             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.

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)

#### Possible Return Values

- the target return type, if it can be inferred
- the standard return type, if the given `method` does not declare any formal type variables
- the standard return type, if the target return type cannot be inferred (e.g., due to type erasure)
- `null`, if the length of the given arguments array is shorter than the length of the formal argument list for the given method

Parameters:
  `method` - the method to introspect, never `null`
  `args` - the arguments that will be supplied to the method when it is invoked, never `null`
Returns:
  the resolved target return type, the standard return type, or `null`
Since:
  3.2
See Also:
  `resolveReturnType(java.lang.reflect.Method, java.lang.Class<?>)`

#### resolveReturnTypeArgument

    public static Class<?> resolveReturnTypeArgument(Method method,
                                     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 Class<?> resolveTypeArgument(Class clazz,
                               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 Class[] resolveTypeArguments(Class clazz,
                               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

#### resolveType

    public static Class<?> resolveType(Type genericType,
                       Map<TypeVariable,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

#### getTypeVariableMap

    public static Map<TypeVariable,Type> getTypeVariableMap(Class clazz)

Build a mapping of `TypeVariable names` to `concrete classes` for the specified `Class`. Searches all super types, enclosing types and interfaces.