Class ResolvableType
- All Implemented Interfaces:
Serializable
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
Modifier and TypeFieldDescriptionstatic final ResolvableType
ResolvableType
returned when no value is available. -
Method Summary
Modifier and TypeMethodDescriptionReturn this type as a {link ResolvableType} of the specified class.boolean
static ResolvableType
Return a {link ResolvableType} for the specified {link Class}, using the full generic type information for assignability checks.static ResolvableType
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.getType()
Return the underling Java {link Type} being managed.boolean
Returntrue
if this type contains generic parameters.int
hashCode()
boolean
isArray()
Returntrue
if this type resolves to a Class that represents an array.Class<?>
resolve()
Resolve this type to a {link java.lang.Class}, returningnull
if the type cannot be resolved.Class<?>
Resolve this type to a {link java.lang.Class}, returning the specifiedfallback
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 specifiedfallback
if any type cannot be resolved.toString()
Return a String representation of this type in its fully resolved form (including any generic parameters).
-
Field Details
-
NONE
ResolvableType
returned when no value is available.NONE
is used in preference tonull
so that multiple method calls can be safely chained.
-
-
Method Details
-
getType
Return the underling Java {link Type} being managed. -
isArray
public boolean isArray()Returntrue
if this type resolves to a Class that represents an array. see #getComponentType() -
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
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
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
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()Returntrue
if this type contains generic parameters. see #getGeneric(int...) see #getGenerics() -
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 againstIndexOutOfBoundsExceptions
.- Returns:
- an array of {link ResolvableType ResolvableTypes} representing the generic parameters
(never
null
) see #hasGenerics() see #getGeneric(int...) see #resolveGeneric(int...) see #resolveGenerics()
-
resolveGenerics
Convenience method that will {link #getGenerics() get} and {link #resolve() resolve} generic parameters, using the specifiedfallback
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
Resolve this type to a {link java.lang.Class}, returningnull
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 ofObject.class
will be ignored.If this method returns a non-null
Class
and {link #hasGenerics()} returnsfalse
, the given type effectively wraps a plainClass
, allowing for plainClass
processing if desirable.- Returns:
- the resolved {link Class}, or
null
if not resolvable see #resolve(Class) see #resolveGeneric(int...) see #resolveGenerics()
-
resolve
Resolve this type to a {link java.lang.Class}, returning the specifiedfallback
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 ofObject.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
-
hashCode
public int hashCode() -
toString
Return a String representation of this type in its fully resolved form (including any generic parameters). -
forClass
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 toObject.class
for typical use cases here)- Returns:
- a {link ResolvableType} for the specified class see #forClass(Class, Class) see #forClassWithGenerics(Class, Class...)
-
forType
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 ornull
owner
- the owner type used to resolve variables- Returns:
- a {link ResolvableType} for the specified {link Type} and owner see #forType(Type)
-