Class ResolvableType

java.lang.Object
org.springframework.core.ResolvableType
All Implemented Interfaces:
Serializable

public class ResolvableType extends Object implements Serializable
Encapsulates a Java Type, providing access to supertypes, interfaces, and generic parameters along with the ability to ultimately resolve to a Class.

A ResolvableType may be obtained from a field, a method parameter, a method return type, or a class. Most methods on this class will themselves return a ResolvableType, allowing for easy navigation. For example:

 private HashMap<Integer, List<String>> myMap;

 public void example() {
     ResolvableType t = ResolvableType.forField(getClass().getDeclaredField("myMap"));
     t.getSuperType(); // AbstractMap<Integer, List<String>>
     t.asMap(); // Map<Integer, List<String>>
     t.getGeneric(0).resolve(); // Integer
     t.getGeneric(1).resolve(); // List
     t.getGeneric(1); // List<String>
     t.resolveGeneric(1, 0); // String
 }
 
Since:
4.0
Author:
Phillip Webb, Juergen Hoeller, Stephane Nicoll
See Also:
  • Field Details

    • NONE

      public static final ResolvableType NONE
      ResolvableType returned when no value is available. NONE is used in preference to null so that multiple method calls can be safely chained.
  • Method Details

    • getType

      public Type getType()
      Return the underling Java Type being managed.
    • getRawClass

      @Nullable public Class<?> getRawClass()
      Return the underlying Java Class being managed, if available; otherwise null.
    • getSource

      public Object getSource()
      Return the underlying source of the resolvable type. Will return a Field, MethodParameter or Type depending on how the ResolvableType was constructed. This method is primarily to provide access to additional type information or meta-data that alternative JVM languages may provide.
    • toClass

      public Class<?> toClass()
      Return this type as a resolved Class, falling back to Object if no specific class can be resolved.
      Returns:
      the resolved Class or the Object fallback
      Since:
      5.1
      See Also:
    • isInstance

      public boolean isInstance(@Nullable Object obj)
      Determine whether the given object is an instance of this ResolvableType.
      Parameters:
      obj - the object to check
      Since:
      4.2
      See Also:
    • isAssignableFrom

      public boolean isAssignableFrom(Class<?> other)
      Determine whether this ResolvableType is assignable from the specified other type.
      Parameters:
      other - the type to be checked against (as a Class)
      Since:
      4.2
      See Also:
    • isAssignableFrom

      public boolean isAssignableFrom(ResolvableType other)
      Determine whether this ResolvableType is assignable from the specified other type.

      Attempts to follow the same rules as the Java compiler, considering whether both the resolved Class is assignable from the given type as well as whether all generics are assignable.

      Parameters:
      other - the type to be checked against (as a ResolvableType)
      Returns:
      true if the specified other type can be assigned to this ResolvableType; false otherwise
    • isArray

      public boolean isArray()
      Return true if this type resolves to a Class that represents an array.
      See Also:
    • getComponentType

      public ResolvableType getComponentType()
      Return the ResolvableType representing the component type of the array or NONE if this type does not represent an array.
      See Also:
    • asCollection

      public ResolvableType asCollection()
      Convenience method to return this type as a resolvable Collection type.

      Returns NONE if this type does not implement or extend Collection.

      See Also:
    • asMap

      public ResolvableType asMap()
      Convenience method to return this type as a resolvable Map type.

      Returns NONE if this type does not implement or extend Map.

      See Also:
    • as

      public ResolvableType as(Class<?> type)
      Return this type as a ResolvableType of the specified class. Searches supertype and interface hierarchies to find a match, returning NONE if this type does not implement or extend the specified class.
      Parameters:
      type - the required type (typically narrowed)
      Returns:
      a ResolvableType representing this object as the specified type, or NONE if not resolvable as that type
      See Also:
    • getSuperType

      public ResolvableType getSuperType()
      Return a ResolvableType representing the direct supertype of this type.

      If no supertype is available this method returns NONE.

      Note: The resulting ResolvableType instance may not be Serializable.

      See Also:
    • getInterfaces

      public ResolvableType[] getInterfaces()
      Return a ResolvableType array representing the direct interfaces implemented by this type. If this type does not implement any interfaces an empty array is returned.

      Note: The resulting ResolvableType instances may not be Serializable.

      See Also:
    • hasGenerics

      public boolean hasGenerics()
      Return true if this type contains generic parameters.
      See Also:
    • hasUnresolvableGenerics

      public boolean hasUnresolvableGenerics()
      Determine whether the underlying type has any unresolvable generics: either through an unresolvable type variable on the type itself or through implementing a generic interface in a raw fashion, i.e. without substituting that interface's type variables. The result will be true only in those two scenarios.
    • getNested

      public ResolvableType getNested(int nestingLevel)
      Return a ResolvableType for the specified nesting level.

      See getNested(int, Map) for details.

      Parameters:
      nestingLevel - the nesting level
      Returns:
      the ResolvableType type, or #NONE
    • getNested

      public ResolvableType getNested(int nestingLevel, @Nullable Map<Integer,Integer> typeIndexesPerLevel)
      Return a ResolvableType for the specified nesting level.

      The nesting level refers to the specific generic parameter that should be returned. A nesting level of 1 indicates this type; 2 indicates the first nested generic; 3 the second; and so on. For example, given List<Set<Integer>> level 1 refers to the List, level 2 the Set, and level 3 the Integer.

      The typeIndexesPerLevel map can be used to reference a specific generic for the given level. For example, an index of 0 would refer to a Map key; whereas, 1 would refer to the value. If the map does not contain a value for a specific level the last generic will be used (e.g. a Map value).

      Nesting levels may also apply to array types; for example given String[], a nesting level of 2 refers to String.

      If a type does not contain generics the supertype hierarchy will be considered.

      Parameters:
      nestingLevel - the required nesting level, indexed from 1 for the current type, 2 for the first nested generic, 3 for the second and so on
      typeIndexesPerLevel - a map containing the generic index for a given nesting level (can be null)
      Returns:
      a ResolvableType for the nested level, or NONE
    • getGeneric

      public ResolvableType getGeneric(@Nullable int... indexes)
      Return a ResolvableType representing the generic parameter for the given indexes. Indexes are zero based; for example given the type Map<Integer, List<String>>, getGeneric(0) will access the Integer. Nested generics can be accessed by specifying multiple indexes; for example getGeneric(1, 0) will access the String from the nested List. For convenience, if no indexes are specified the first generic is returned.

      If no generic is available at the specified indexes NONE is returned.

      Parameters:
      indexes - the indexes that refer to the generic parameter (can be omitted to return the first generic)
      Returns:
      a ResolvableType for the specified generic, or NONE
      See Also:
    • getGenerics

      public ResolvableType[] getGenerics()
      Return an array of ResolvableType ResolvableTypes representing the generic parameters of this type. If no generics are available an empty array is returned. If you need to access a specific generic consider using the getGeneric(int...) method as it allows access to nested generics and protects against IndexOutOfBoundsExceptions.
      Returns:
      an array of ResolvableType ResolvableTypes representing the generic parameters (never null)
      See Also:
    • resolveGenerics

      public Class<?>[] resolveGenerics()
      Convenience method that will get and resolve generic parameters.
      Returns:
      an array of resolved generic parameters (the resulting array will never be null, but it may contain null elements)
      See Also:
    • resolveGenerics

      public Class<?>[] resolveGenerics(Class<?> fallback)
      Convenience method that will get and resolve generic parameters, using the specified fallback if any type cannot be resolved.
      Parameters:
      fallback - the fallback class to use if resolution fails
      Returns:
      an array of resolved generic parameters
      See Also:
    • resolveGeneric

      @Nullable public Class<?> resolveGeneric(int... indexes)
      Convenience method that will get and resolve a specific generic parameter.
      Parameters:
      indexes - the indexes that refer to the generic parameter (can be omitted to return the first generic)
      Returns:
      a resolved Class or null
      See Also:
    • resolve

      @Nullable public Class<?> resolve()
      Resolve this type to a Class, returning null if the type cannot be resolved. This method will consider bounds of TypeVariables and WildcardTypes if direct resolution fails; however, bounds of Object.class will be ignored.

      If this method returns a non-null Class and hasGenerics() returns false, the given type effectively wraps a plain Class, allowing for plain Class processing if desirable.

      Returns:
      the resolved Class, or null if not resolvable
      See Also:
    • resolve

      public Class<?> resolve(Class<?> fallback)
      Resolve this type to a Class, returning the specified fallback if the type cannot be resolved. This method will consider bounds of TypeVariables and WildcardTypes if direct resolution fails; however, bounds of Object.class will be ignored.
      Parameters:
      fallback - the fallback class to use if resolution fails
      Returns:
      the resolved Class or the fallback
      See Also:
    • equals

      public boolean equals(@Nullable Object other)
      Check for full equality of all type resolution artifacts: type as well as TypeProvider and VariableResolver.
      Overrides:
      equals in class Object
      See Also:
    • equalsType

      public boolean equalsType(ResolvableType otherType)
      Check for type-level equality with another ResolvableType.

      In contrast to equals(Object) or isAssignableFrom(ResolvableType), this works between different sources as well, e.g. method parameters and return types.

      Parameters:
      otherType - the ResolvableType to match against
      Returns:
      whether the declared type and type variables match
      Since:
      6.1
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Return a String representation of this type in its fully resolved form (including any generic parameters).
      Overrides:
      toString in class Object
    • forClass

      public static ResolvableType forClass(@Nullable Class<?> clazz)
      Return a ResolvableType for the specified Class, using the full generic type information for assignability checks.

      For example: ResolvableType.forClass(MyArrayList.class).

      Parameters:
      clazz - the class to introspect (null is semantically equivalent to Object.class for typical use cases here)
      Returns:
      a ResolvableType for the specified class
      See Also:
    • forRawClass

      public static ResolvableType forRawClass(@Nullable Class<?> clazz)
      Return a ResolvableType for the specified Class, doing assignability checks against the raw class only (analogous to Class.isAssignableFrom(java.lang.Class<?>), which this serves as a wrapper for).

      For example: ResolvableType.forRawClass(List.class).

      Parameters:
      clazz - the class to introspect (null is semantically equivalent to Object.class for typical use cases here)
      Returns:
      a ResolvableType for the specified class
      Since:
      4.2
      See Also:
    • forClass

      public static ResolvableType forClass(Class<?> baseType, Class<?> implementationClass)
      Return a ResolvableType for the specified base type (interface or base class) with a given implementation class.

      For example: ResolvableType.forClass(List.class, MyArrayList.class).

      Parameters:
      baseType - the base type (must not be null)
      implementationClass - the implementation class
      Returns:
      a ResolvableType for the specified base type backed by the given implementation class
      See Also:
    • forClassWithGenerics

      public static ResolvableType forClassWithGenerics(Class<?> clazz, Class<?>... generics)
      Return a ResolvableType for the specified Class with pre-declared generics.
      Parameters:
      clazz - the class (or interface) to introspect
      generics - the generics of the class
      Returns:
      a ResolvableType for the specific class and generics
      See Also:
    • forClassWithGenerics

      public static ResolvableType forClassWithGenerics(Class<?> clazz, @Nullable ResolvableType... generics)
      Return a ResolvableType for the specified Class with pre-declared generics.
      Parameters:
      clazz - the class (or interface) to introspect
      generics - the generics of the class
      Returns:
      a ResolvableType for the specific class and generics
      See Also:
    • forInstance

      public static ResolvableType forInstance(@Nullable Object instance)
      Return a ResolvableType for the specified instance. The instance does not convey generic information but if it implements ResolvableTypeProvider a more precise ResolvableType can be used than the simple one based on the Class instance.
      Parameters:
      instance - the instance (possibly null)
      Returns:
      a ResolvableType for the specified instance, or NONE for null
      Since:
      4.2
      See Also:
    • forField

      public static ResolvableType forField(Field field)
      Return a ResolvableType for the specified Field.
      Parameters:
      field - the source field
      Returns:
      a ResolvableType for the specified field
      See Also:
    • forField

      public static ResolvableType forField(Field field, Class<?> implementationClass)
      Return a ResolvableType for the specified Field with a given implementation.

      Use this variant when the class that declares the field includes generic parameter variables that are satisfied by the implementation class.

      Parameters:
      field - the source field
      implementationClass - the implementation class
      Returns:
      a ResolvableType for the specified field
      See Also:
    • forField

      public static ResolvableType forField(Field field, @Nullable ResolvableType implementationType)
      Return a ResolvableType for the specified Field with a given implementation.

      Use this variant when the class that declares the field includes generic parameter variables that are satisfied by the implementation type.

      Parameters:
      field - the source field
      implementationType - the implementation type
      Returns:
      a ResolvableType for the specified field
      See Also:
    • forField

      public static ResolvableType forField(Field field, int nestingLevel)
      Return a ResolvableType for the specified Field with the given nesting level.
      Parameters:
      field - the source field
      nestingLevel - the nesting level (1 for the outer level; 2 for a nested generic type; etc)
      See Also:
    • forField

      public static ResolvableType forField(Field field, int nestingLevel, @Nullable Class<?> implementationClass)
      Return a ResolvableType for the specified Field with a given implementation and the given nesting level.

      Use this variant when the class that declares the field includes generic parameter variables that are satisfied by the implementation class.

      Parameters:
      field - the source field
      nestingLevel - the nesting level (1 for the outer level; 2 for a nested generic type; etc)
      implementationClass - the implementation class
      Returns:
      a ResolvableType for the specified field
      See Also:
    • forConstructorParameter

      public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex)
      Return a ResolvableType for the specified Constructor parameter.
      Parameters:
      constructor - the source constructor (must not be null)
      parameterIndex - the parameter index
      Returns:
      a ResolvableType for the specified constructor parameter
      See Also:
    • forConstructorParameter

      public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex, Class<?> implementationClass)
      Return a ResolvableType for the specified Constructor parameter with a given implementation. Use this variant when the class that declares the constructor includes generic parameter variables that are satisfied by the implementation class.
      Parameters:
      constructor - the source constructor (must not be null)
      parameterIndex - the parameter index
      implementationClass - the implementation class
      Returns:
      a ResolvableType for the specified constructor parameter
      See Also:
    • forMethodReturnType

      public static ResolvableType forMethodReturnType(Method method)
      Return a ResolvableType for the specified Method return type.
      Parameters:
      method - the source for the method return type
      Returns:
      a ResolvableType for the specified method return
      See Also:
    • forMethodReturnType

      public static ResolvableType forMethodReturnType(Method method, Class<?> implementationClass)
      Return a ResolvableType for the specified Method return type.

      Use this variant when the class that declares the method includes generic parameter variables that are satisfied by the implementation class.

      Parameters:
      method - the source for the method return type
      implementationClass - the implementation class
      Returns:
      a ResolvableType for the specified method return
      See Also:
    • forMethodParameter

      public static ResolvableType forMethodParameter(Method method, int parameterIndex)
      Return a ResolvableType for the specified Method parameter.
      Parameters:
      method - the source method (must not be null)
      parameterIndex - the parameter index
      Returns:
      a ResolvableType for the specified method parameter
      See Also:
    • forMethodParameter

      public static ResolvableType forMethodParameter(Method method, int parameterIndex, Class<?> implementationClass)
      Return a ResolvableType for the specified Method parameter with a given implementation. Use this variant when the class that declares the method includes generic parameter variables that are satisfied by the implementation class.
      Parameters:
      method - the source method (must not be null)
      parameterIndex - the parameter index
      implementationClass - the implementation class
      Returns:
      a ResolvableType for the specified method parameter
      See Also:
    • forMethodParameter

      public static ResolvableType forMethodParameter(MethodParameter methodParameter)
      Return a ResolvableType for the specified MethodParameter.
      Parameters:
      methodParameter - the source method parameter (must not be null)
      Returns:
      a ResolvableType for the specified method parameter
      See Also:
    • forMethodParameter

      public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable ResolvableType implementationType)
      Return a ResolvableType for the specified MethodParameter with a given implementation type. Use this variant when the class that declares the method includes generic parameter variables that are satisfied by the implementation type.
      Parameters:
      methodParameter - the source method parameter (must not be null)
      implementationType - the implementation type
      Returns:
      a ResolvableType for the specified method parameter
      See Also:
    • forMethodParameter

      public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable Type targetType)
      Return a ResolvableType for the specified MethodParameter, overriding the target type to resolve with a specific given type.
      Parameters:
      methodParameter - the source method parameter (must not be null)
      targetType - the type to resolve (a part of the method parameter's type)
      Returns:
      a ResolvableType for the specified method parameter
      See Also:
    • forArrayComponent

      public static ResolvableType forArrayComponent(ResolvableType componentType)
      Return a ResolvableType as an array of the specified componentType.
      Parameters:
      componentType - the component type
      Returns:
      a ResolvableType as an array of the specified component type
    • forType

      public static ResolvableType forType(@Nullable Type type)
      Return a ResolvableType for the specified Type.

      Note: The resulting ResolvableType instance may not be Serializable.

      Parameters:
      type - the source type (potentially null)
      Returns:
      a ResolvableType for the specified Type
      See Also:
    • forType

      public static ResolvableType forType(@Nullable Type type, @Nullable ResolvableType owner)
      Return a ResolvableType for the specified Type backed by the given owner type.

      Note: The resulting ResolvableType instance may not be Serializable.

      Parameters:
      type - the source type or null
      owner - the owner type used to resolve variables
      Returns:
      a ResolvableType for the specified Type and owner
      See Also:
    • forType

      public static ResolvableType forType(ParameterizedTypeReference<?> typeReference)
      Return a ResolvableType for the specified ParameterizedTypeReference.

      Note: The resulting ResolvableType instance may not be Serializable.

      Parameters:
      typeReference - the reference to obtain the source type from
      Returns:
      a ResolvableType for the specified ParameterizedTypeReference
      Since:
      4.3.12
      See Also:
    • clearCache

      public static void clearCache()
      Clear the internal ResolvableType/SerializableTypeWrapper cache.
      Since:
      4.2