java.lang.Object
nl.basjes.parse.useragent.utils.springframework.core.ResolvableType
All Implemented Interfaces:
Serializable

public final class ResolvableType extends Object implements Serializable
Encapsulates a Java {link java.lang.reflect.Type}, providing access to {link #getSuperType() supertypes}, {link #getInterfaces() interfaces}, and {link #getGeneric(int...) generic parameters} along with the ability to ultimately {link #resolve() resolve} to a {link java.lang.Class}.

A ResolvableType may be obtained from a {linkplain #forField(Field) field}, a {linkplain #forMethodParameter(Method, int) method parameter}, a {linkplain #forMethodReturnType(Method) method return type}, or a {linkplain #forClass(Class) 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 #forField(Field) see #forMethodParameter(Method, int) see #forMethodReturnType(Method) see #forConstructorParameter(Constructor, int) see #forClass(Class) see #forType(Type) see #forInstance(Object) see ResolvableTypeProvider
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final ResolvableType
    ResolvableType returned when no value is available.
  • Method Summary

    Modifier and Type
    Method
    Description
    as(Class<?> typE)
    Return this type as a {link ResolvableType} of the specified class.
    boolean
    equals(Object other)
     
    forClass(Class<?> clazz)
    Return a {link ResolvableType} for the specified {link Class}, using the full generic type information for assignability checks.
    forType(Type type, ResolvableType owner)
    Return a {link ResolvableType} for the specified {link Type} backed by the given owner type.
    Return the ResolvableType representing the component type of the array or {link #NONE} if this type does not represent an array.
    Return an array of {link ResolvableType ResolvableTypes} representing the generic parameters of this type.
    Return a {link ResolvableType} array representing the direct interfaces implemented by this type.
    Return a {link ResolvableType} representing the direct supertype of this type.
    Return the underling Java {link Type} being managed.
    boolean
    Return true if this type contains generic parameters.
    int
     
    boolean
    Return true if this type resolves to a Class that represents an array.
    Resolve this type to a {link java.lang.Class}, returning null if the type cannot be resolved.
    resolve(Class<?> fallback)
    Resolve this type to a {link java.lang.Class}, returning the specified fallback if the type cannot be resolved.
    Class<?>[]
    resolveGenerics(Class<?> fallback)
    Convenience method that will {link #getGenerics() get} and {link #resolve() resolve} generic parameters, using the specified fallback if any type cannot be resolved.
    Return a String representation of this type in its fully resolved form (including any generic parameters).

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • 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 {link Type} being managed.
    • isArray

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

      public ResolvableType getComponentType()
      Return the ResolvableType representing the component type of the array or {link #NONE} if this type does not represent an array. see #isArray()
    • as

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

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

      If no supertype is available this method returns {link #NONE}.

      Note: The resulting {link ResolvableType} instance may not be {link Serializable}. see #getInterfaces()

    • getInterfaces

      public ResolvableType[] getInterfaces()
      Return a {link 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 {link ResolvableType} instances may not be {link Serializable}. see #getSuperType()

    • hasGenerics

      public boolean hasGenerics()
      Return true if this type contains generic parameters. see #getGeneric(int...) see #getGenerics()
    • getGenerics

      public ResolvableType[] getGenerics()
      Return an array of {link 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 {link #getGeneric(int...)} method as it allows access to nested generics and protects against IndexOutOfBoundsExceptions.
      Returns:
      an array of {link ResolvableType ResolvableTypes} representing the generic parameters (never null) see #hasGenerics() see #getGeneric(int...) see #resolveGeneric(int...) see #resolveGenerics()
    • resolveGenerics

      public Class<?>[] resolveGenerics(Class<?> fallback)
      Convenience method that will {link #getGenerics() get} and {link #resolve() 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 #getGenerics() see #resolve()
    • resolve

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

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

      Returns:
      the resolved {link Class}, or null if not resolvable see #resolve(Class) see #resolveGeneric(int...) see #resolveGenerics()
    • resolve

      public Class<?> resolve(Class<?> fallback)
      Resolve this type to a {link java.lang.Class}, returning the specified fallback if the type cannot be resolved. This method will consider bounds of {link TypeVariable TypeVariables} and {link WildcardType 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 {link Class} or the fallback see #resolve() see #resolveGeneric(int...) see #resolveGenerics()
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class Object
    • 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 {link ResolvableType} for the specified {link 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 {link ResolvableType} for the specified class see #forClass(Class, Class) see #forClassWithGenerics(Class, Class...)
    • forType

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

      Note: The resulting {link ResolvableType} instance may not be {link Serializable}.

      Parameters:
      type - the source type or null
      owner - the owner type used to resolve variables
      Returns:
      a {link ResolvableType} for the specified {link Type} and owner see #forType(Type)