org.springframework.core

Class GenericTypeResolver

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


  • public final 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, Sam Brannen, Phillip Webb
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      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.
      static java.lang.Class<?> resolveParameterType(MethodParameter methodParameter, java.lang.Class<?> implementationClass)
      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.reflect.Type resolveType(java.lang.reflect.Type genericType, java.lang.Class<?> contextClass)
      Resolve the given generic type against the given context class, substituting type variables as far as possible.
      static java.lang.Class<?> resolveType(java.lang.reflect.Type genericType, java.util.Map<java.lang.reflect.TypeVariable,java.lang.reflect.Type> map)
      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
    • Method Detail

      • resolveParameterType

        public static java.lang.Class<?> resolveParameterType(MethodParameter methodParameter,
                                                              java.lang.Class<?> implementationClass)
        Determine the target type for the given generic parameter type.
        Parameters:
        methodParameter - the method parameter specification
        implementationClass - 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, 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
      • resolveReturnTypeArgument

        @Nullable
        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

        @Nullable
        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

        @Nullable
        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
      • resolveType

        public static java.lang.reflect.Type resolveType(java.lang.reflect.Type genericType,
                                                         @Nullable
                                                         java.lang.Class<?> contextClass)
        Resolve the given generic type against the given context class, substituting type variables as far as possible.
        Parameters:
        genericType - the (potentially) generic type
        contextClass - a context class for the target type, for example a class in which the target type appears in a method signature (can be null)
        Returns:
        the resolved type (possibly the given generic type as-is)
        Since:
        5.0
      • resolveType

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

        Used by Spring Data.

        Parameters:
        genericType - the generic type to resolve
        map - the TypeVariable Map to resolved against
        Returns:
        the type if it resolves to a Class, or Object.class otherwise
      • 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 classes for the specified Class. Searches all super types, enclosing types and interfaces.
        See Also:
        resolveType(Type, Map)