Class ResolvableType
- All Implemented Interfaces:
Serializable
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 Summary
Modifier and TypeFieldDescriptionstatic final ResolvableType
ResolvableType
returned when no value is available. -
Method Summary
Modifier and TypeMethodDescriptionReturn this type as aResolvableType
of the specified class.Convenience method to return this type as a resolvableCollection
type.asMap()
Convenience method to return this type as a resolvableMap
type.static void
Clear the internalResolvableType
/SerializableTypeWrapper
cache.boolean
Check for full equality of all type resolution artifacts: type as well asTypeProvider
andVariableResolver
.boolean
equalsType
(ResolvableType otherType) Check for type-level equality with anotherResolvableType
.static ResolvableType
forArrayComponent
(ResolvableType componentType) Return aResolvableType
as an array of the specifiedcomponentType
.static ResolvableType
Return aResolvableType
for the specifiedClass
, using the full generic type information for assignability checks.static ResolvableType
Return aResolvableType
for the specified base type (interface or base class) with a given implementation class.static ResolvableType
forClassWithGenerics
(Class<?> clazz, Class<?>... generics) Return aResolvableType
for the specifiedClass
with pre-declared generics.static ResolvableType
forClassWithGenerics
(Class<?> clazz, ResolvableType... generics) Return aResolvableType
for the specifiedClass
with pre-declared generics.static ResolvableType
forConstructorParameter
(Constructor<?> constructor, int parameterIndex) Return aResolvableType
for the specifiedConstructor
parameter.static ResolvableType
forConstructorParameter
(Constructor<?> constructor, int parameterIndex, Class<?> implementationClass) Return aResolvableType
for the specifiedConstructor
parameter with a given implementation.static ResolvableType
Return aResolvableType
for the specifiedField
.static ResolvableType
Return aResolvableType
for the specifiedField
with the given nesting level.static ResolvableType
Return aResolvableType
for the specifiedField
with a given implementation and the given nesting level.static ResolvableType
Return aResolvableType
for the specifiedField
with a given implementation.static ResolvableType
forField
(Field field, ResolvableType implementationType) Return aResolvableType
for the specifiedField
with a given implementation.static ResolvableType
forInstance
(Object instance) Return aResolvableType
for the specified instance.static ResolvableType
forMethodParameter
(Method method, int parameterIndex) Return aResolvableType
for the specifiedMethod
parameter.static ResolvableType
forMethodParameter
(Method method, int parameterIndex, Class<?> implementationClass) Return aResolvableType
for the specifiedMethod
parameter with a given implementation.static ResolvableType
forMethodParameter
(MethodParameter methodParameter) Return aResolvableType
for the specifiedMethodParameter
.static ResolvableType
forMethodParameter
(MethodParameter methodParameter, Type targetType) Return aResolvableType
for the specifiedMethodParameter
, overriding the target type to resolve with a specific given type.static ResolvableType
forMethodParameter
(MethodParameter methodParameter, ResolvableType implementationType) Return aResolvableType
for the specifiedMethodParameter
with a given implementation type.static ResolvableType
forMethodReturnType
(Method method) Return aResolvableType
for the specifiedMethod
return type.static ResolvableType
forMethodReturnType
(Method method, Class<?> implementationClass) Return aResolvableType
for the specifiedMethod
return type.static ResolvableType
forRawClass
(Class<?> clazz) Return aResolvableType
for the specifiedClass
, doing assignability checks against the raw class only (analogous toClass.isAssignableFrom(java.lang.Class<?>)
, which this serves as a wrapper for).static ResolvableType
Return aResolvableType
for the specifiedType
.static ResolvableType
forType
(Type type, ResolvableType owner) Return aResolvableType
for the specifiedType
backed by the given owner type.static ResolvableType
forType
(ParameterizedTypeReference<?> typeReference) Return aResolvableType
for the specifiedParameterizedTypeReference
.Return the ResolvableType representing the component type of the array orNONE
if this type does not represent an array.getGeneric
(int... indexes) Return aResolvableType
representing the generic parameter for the given indexes.Return an array ofResolvableType ResolvableTypes
representing the generic parameters of this type.Return aResolvableType
array representing the direct interfaces implemented by this type.getNested
(int nestingLevel) Return aResolvableType
for the specified nesting level.Return aResolvableType
for the specified nesting level.Class<?>
Return the underlying JavaClass
being managed, if available; otherwisenull
.Return the underlying source of the resolvable type.Return aResolvableType
representing the direct supertype of this type.getType()
Return the underling JavaType
being managed.boolean
Returntrue
if this type contains generic parameters.int
hashCode()
boolean
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.boolean
isArray()
Returntrue
if this type resolves to a Class that represents an array.boolean
isAssignableFrom
(Class<?> other) Determine whether thisResolvableType
is assignable from the specified other type.boolean
isAssignableFrom
(ResolvableType other) Determine whether thisResolvableType
is assignable from the specified other type.boolean
isInstance
(Object obj) Determine whether the given object is an instance of thisResolvableType
.Class<?>
resolve()
Resolve this type to aClass
, returningnull
if the type cannot be resolved.Class<?>
Resolve this type to aClass
, returning the specifiedfallback
if the type cannot be resolved.Class<?>
resolveGeneric
(int... indexes) Class<?>[]
Class<?>[]
resolveGenerics
(Class<?> fallback) Class<?>
toClass()
Return this type as a resolvedClass
, falling back toObject
if no specific class can 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 JavaType
being managed. -
getRawClass
Return the underlying JavaClass
being managed, if available; otherwisenull
. -
getSource
Return the underlying source of the resolvable type. Will return aField
,MethodParameter
orType
depending on how theResolvableType
was constructed. This method is primarily to provide access to additional type information or meta-data that alternative JVM languages may provide. -
toClass
Return this type as a resolvedClass
, falling back toObject
if no specific class can be resolved.- Returns:
- the resolved
Class
or theObject
fallback - Since:
- 5.1
- See Also:
-
isInstance
Determine whether the given object is an instance of thisResolvableType
.- Parameters:
obj
- the object to check- Since:
- 4.2
- See Also:
-
isAssignableFrom
Determine whether thisResolvableType
is assignable from the specified other type.- Parameters:
other
- the type to be checked against (as aClass
)- Since:
- 4.2
- See Also:
-
isAssignableFrom
Determine whether thisResolvableType
is assignable from the specified other type.Attempts to follow the same rules as the Java compiler, considering whether both the
resolved
Class
isassignable from
the given type as well as whether allgenerics
are assignable.- Parameters:
other
- the type to be checked against (as aResolvableType
)- Returns:
true
if the specified other type can be assigned to thisResolvableType
;false
otherwise
-
isArray
public boolean isArray()Returntrue
if this type resolves to a Class that represents an array.- See Also:
-
getComponentType
Return the ResolvableType representing the component type of the array orNONE
if this type does not represent an array.- See Also:
-
asCollection
Convenience method to return this type as a resolvableCollection
type.Returns
NONE
if this type does not implement or extendCollection
. -
asMap
Convenience method to return this type as a resolvableMap
type.- See Also:
-
as
Return this type as aResolvableType
of the specified class. Searchessupertype
andinterface
hierarchies to find a match, returningNONE
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, orNONE
if not resolvable as that type - See Also:
-
getSuperType
Return aResolvableType
representing the direct supertype of this type.If no supertype is available this method returns
NONE
.Note: The resulting
ResolvableType
instance may not beSerializable
.- See Also:
-
getInterfaces
Return aResolvableType
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 beSerializable
.- See Also:
-
hasGenerics
public boolean hasGenerics()Returntrue
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 betrue
only in those two scenarios. -
getNested
Return aResolvableType
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 aResolvableType
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 theList
, level 2 theSet
, and level 3 theInteger
.The
typeIndexesPerLevel
map can be used to reference a specific generic for the given level. For example, an index of 0 would refer to aMap
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. aMap
value).Nesting levels may also apply to array types; for example given
String[]
, a nesting level of 2 refers toString
.If a type does not
contain
generics thesupertype
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 ontypeIndexesPerLevel
- a map containing the generic index for a given nesting level (can benull
)- Returns:
- a
ResolvableType
for the nested level, orNONE
-
getGeneric
Return aResolvableType
representing the generic parameter for the given indexes. Indexes are zero based; for example given the typeMap<Integer, List<String>>
,getGeneric(0)
will access theInteger
. Nested generics can be accessed by specifying multiple indexes; for examplegetGeneric(1, 0)
will access theString
from the nestedList
. 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, orNONE
- See Also:
-
getGenerics
Return an array ofResolvableType 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 thegetGeneric(int...)
method as it allows access to nested generics and protects againstIndexOutOfBoundsExceptions
.- Returns:
- an array of
ResolvableType ResolvableTypes
representing the generic parameters (nevernull
) - See Also:
-
resolveGenerics
- Returns:
- an array of resolved generic parameters (the resulting array
will never be
null
, but it may containnull
elements) - See Also:
-
resolveGenerics
Convenience method that willget
andresolve
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 Also:
-
resolveGeneric
- Parameters:
indexes
- the indexes that refer to the generic parameter (can be omitted to return the first generic)- Returns:
- a resolved
Class
ornull
- See Also:
-
resolve
Resolve this type to aClass
, returningnull
if the type cannot be resolved. This method will consider bounds ofTypeVariables
andWildcardTypes
if direct resolution fails; however, bounds ofObject.class
will be ignored.If this method returns a non-null
Class
andhasGenerics()
returnsfalse
, the given type effectively wraps a plainClass
, allowing for plainClass
processing if desirable.- Returns:
- the resolved
Class
, ornull
if not resolvable - See Also:
-
resolve
Resolve this type to aClass
, returning the specifiedfallback
if the type cannot be resolved. This method will consider bounds ofTypeVariables
andWildcardTypes
if direct resolution fails; however, bounds ofObject.class
will be ignored.- Parameters:
fallback
- the fallback class to use if resolution fails- Returns:
- the resolved
Class
or thefallback
- See Also:
-
equals
Check for full equality of all type resolution artifacts: type as well asTypeProvider
andVariableResolver
. -
equalsType
Check for type-level equality with anotherResolvableType
.In contrast to
equals(Object)
orisAssignableFrom(ResolvableType)
, this works between different sources as well, e.g. method parameters and return types.- Parameters:
otherType
- theResolvableType
to match against- Returns:
- whether the declared type and type variables match
- Since:
- 6.1
-
hashCode
public int hashCode() -
toString
Return a String representation of this type in its fully resolved form (including any generic parameters). -
forClass
Return aResolvableType
for the specifiedClass
, 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
ResolvableType
for the specified class - See Also:
-
forRawClass
Return aResolvableType
for the specifiedClass
, doing assignability checks against the raw class only (analogous toClass.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 toObject.class
for typical use cases here)- Returns:
- a
ResolvableType
for the specified class - Since:
- 4.2
- See Also:
-
forClass
Return aResolvableType
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 benull
)implementationClass
- the implementation class- Returns:
- a
ResolvableType
for the specified base type backed by the given implementation class - See Also:
-
forClassWithGenerics
Return aResolvableType
for the specifiedClass
with pre-declared generics.- Parameters:
clazz
- the class (or interface) to introspectgenerics
- 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 aResolvableType
for the specifiedClass
with pre-declared generics.- Parameters:
clazz
- the class (or interface) to introspectgenerics
- the generics of the class- Returns:
- a
ResolvableType
for the specific class and generics - See Also:
-
forInstance
Return aResolvableType
for the specified instance. The instance does not convey generic information but if it implementsResolvableTypeProvider
a more preciseResolvableType
can be used than the simple one based on theClass instance
.- Parameters:
instance
- the instance (possiblynull
)- Returns:
- a
ResolvableType
for the specified instance, orNONE
fornull
- Since:
- 4.2
- See Also:
-
forField
Return aResolvableType
for the specifiedField
.- Parameters:
field
- the source field- Returns:
- a
ResolvableType
for the specified field - See Also:
-
forField
Return aResolvableType
for the specifiedField
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 fieldimplementationClass
- the implementation class- Returns:
- a
ResolvableType
for the specified field - See Also:
-
forField
Return aResolvableType
for the specifiedField
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 fieldimplementationType
- the implementation type- Returns:
- a
ResolvableType
for the specified field - See Also:
-
forField
Return aResolvableType
for the specifiedField
with the given nesting level.- Parameters:
field
- the source fieldnestingLevel
- 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 aResolvableType
for the specifiedField
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 fieldnestingLevel
- 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 aResolvableType
for the specifiedConstructor
parameter.- Parameters:
constructor
- the source constructor (must not benull
)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 aResolvableType
for the specifiedConstructor
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 benull
)parameterIndex
- the parameter indeximplementationClass
- the implementation class- Returns:
- a
ResolvableType
for the specified constructor parameter - See Also:
-
forMethodReturnType
Return aResolvableType
for the specifiedMethod
return type.- Parameters:
method
- the source for the method return type- Returns:
- a
ResolvableType
for the specified method return - See Also:
-
forMethodReturnType
Return aResolvableType
for the specifiedMethod
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 typeimplementationClass
- the implementation class- Returns:
- a
ResolvableType
for the specified method return - See Also:
-
forMethodParameter
Return aResolvableType
for the specifiedMethod
parameter.- Parameters:
method
- the source method (must not benull
)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 aResolvableType
for the specifiedMethod
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 benull
)parameterIndex
- the parameter indeximplementationClass
- the implementation class- Returns:
- a
ResolvableType
for the specified method parameter - See Also:
-
forMethodParameter
Return aResolvableType
for the specifiedMethodParameter
.- Parameters:
methodParameter
- the source method parameter (must not benull
)- Returns:
- a
ResolvableType
for the specified method parameter - See Also:
-
forMethodParameter
public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable ResolvableType implementationType) Return aResolvableType
for the specifiedMethodParameter
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 benull
)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 aResolvableType
for the specifiedMethodParameter
, overriding the target type to resolve with a specific given type.- Parameters:
methodParameter
- the source method parameter (must not benull
)targetType
- the type to resolve (a part of the method parameter's type)- Returns:
- a
ResolvableType
for the specified method parameter - See Also:
-
forArrayComponent
Return aResolvableType
as an array of the specifiedcomponentType
.- Parameters:
componentType
- the component type- Returns:
- a
ResolvableType
as an array of the specified component type
-
forType
Return aResolvableType
for the specifiedType
.Note: The resulting
ResolvableType
instance may not beSerializable
.- Parameters:
type
- the source type (potentiallynull
)- Returns:
- a
ResolvableType
for the specifiedType
- See Also:
-
forType
Return aResolvableType
for the specifiedType
backed by the given owner type.Note: The resulting
ResolvableType
instance may not beSerializable
.- Parameters:
type
- the source type ornull
owner
- the owner type used to resolve variables- Returns:
- a
ResolvableType
for the specifiedType
and owner - See Also:
-
forType
Return aResolvableType
for the specifiedParameterizedTypeReference
.Note: The resulting
ResolvableType
instance may not beSerializable
.- Parameters:
typeReference
- the reference to obtain the source type from- Returns:
- a
ResolvableType
for the specifiedParameterizedTypeReference
- Since:
- 4.3.12
- See Also:
-
clearCache
public static void clearCache()Clear the internalResolvableType
/SerializableTypeWrapper
cache.- Since:
- 4.2
-