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
FieldsModifier and TypeFieldDescriptionstatic final ResolvableTypeResolvableTypereturned when no value is available. -
Method Summary
Modifier and TypeMethodDescriptionReturn this type as aResolvableTypeof the specified class.Convenience method to return this type as a resolvableCollectiontype.asMap()Convenience method to return this type as a resolvableMaptype.static voidClear the internalResolvableType/SerializableTypeWrappercache.booleanCheck for full equality of all type resolution artifacts: type as well asTypeProviderandVariableResolver.booleanequalsType(ResolvableType otherType) Check for type-level equality with anotherResolvableType.static ResolvableTypeforArrayComponent(ResolvableType componentType) Return aResolvableTypeas an array of the specifiedcomponentType.static ResolvableTypeReturn aResolvableTypefor the specifiedClass, using the full generic type information for assignability checks.static ResolvableTypeReturn aResolvableTypefor the specified base type (interface or base class) with a given implementation class.static ResolvableTypeforClassWithGenerics(Class<?> clazz, Class<?>... generics) Return aResolvableTypefor the specifiedClasswith pre-declared generics.static ResolvableTypeforClassWithGenerics(Class<?> clazz, ResolvableType... generics) Return aResolvableTypefor the specifiedClasswith pre-declared generics.static ResolvableTypeforConstructorParameter(Constructor<?> constructor, int parameterIndex) Return aResolvableTypefor the specifiedConstructorparameter.static ResolvableTypeforConstructorParameter(Constructor<?> constructor, int parameterIndex, Class<?> implementationClass) Return aResolvableTypefor the specifiedConstructorparameter with a given implementation.static ResolvableTypeReturn aResolvableTypefor the specifiedField.static ResolvableTypeReturn aResolvableTypefor the specifiedFieldwith the given nesting level.static ResolvableTypeReturn aResolvableTypefor the specifiedFieldwith a given implementation and the given nesting level.static ResolvableTypeReturn aResolvableTypefor the specifiedFieldwith a given implementation.static ResolvableTypeforField(Field field, ResolvableType implementationType) Return aResolvableTypefor the specifiedFieldwith a given implementation.static ResolvableTypeforInstance(Object instance) Return aResolvableTypefor the specified instance.static ResolvableTypeforMethodParameter(Method method, int parameterIndex) Return aResolvableTypefor the specifiedMethodparameter.static ResolvableTypeforMethodParameter(Method method, int parameterIndex, Class<?> implementationClass) Return aResolvableTypefor the specifiedMethodparameter with a given implementation.static ResolvableTypeforMethodParameter(MethodParameter methodParameter) Return aResolvableTypefor the specifiedMethodParameter.static ResolvableTypeforMethodParameter(MethodParameter methodParameter, Type targetType) Return aResolvableTypefor the specifiedMethodParameter, overriding the target type to resolve with a specific given type.static ResolvableTypeforMethodParameter(MethodParameter methodParameter, ResolvableType implementationType) Return aResolvableTypefor the specifiedMethodParameterwith a given implementation type.static ResolvableTypeforMethodReturnType(Method method) Return aResolvableTypefor the specifiedMethodreturn type.static ResolvableTypeforMethodReturnType(Method method, Class<?> implementationClass) Return aResolvableTypefor the specifiedMethodreturn type.static ResolvableTypeforRawClass(Class<?> clazz) Return aResolvableTypefor the specifiedClass, doing assignability checks against the raw class only (analogous toClass.isAssignableFrom(java.lang.Class<?>), which this serves as a wrapper for).static ResolvableTypeReturn aResolvableTypefor the specifiedType.static ResolvableTypeforType(Type type, ResolvableType owner) Return aResolvableTypefor the specifiedTypebacked by the given owner type.static ResolvableTypeforType(ParameterizedTypeReference<?> typeReference) Return aResolvableTypefor the specifiedParameterizedTypeReference.Return the ResolvableType representing the component type of the array orNONEif this type does not represent an array.getGeneric(int... indexes) Return aResolvableTyperepresenting the generic parameter for the given indexes.Return an array ofResolvableType ResolvableTypesrepresenting the generic parameters of this type.Return aResolvableTypearray representing the direct interfaces implemented by this type.getNested(int nestingLevel) Return aResolvableTypefor the specified nesting level.Return aResolvableTypefor the specified nesting level.Class<?>Return the underlying JavaClassbeing managed, if available; otherwisenull.Return the underlying source of the resolvable type.Return aResolvableTyperepresenting the direct supertype of this type.getType()Return the underling JavaTypebeing managed.booleanReturntrueif this type contains generic parameters.inthashCode()booleanDetermine 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.booleanisArray()Returntrueif this type resolves to a Class that represents an array.booleanisAssignableFrom(Class<?> other) Determine whether thisResolvableTypeis assignable from the specified other type.booleanisAssignableFrom(ResolvableType other) Determine whether thisResolvableTypeis assignable from the specified other type.booleanisInstance(Object obj) Determine whether the given object is an instance of thisResolvableType.Class<?>resolve()Resolve this type to aClass, returningnullif the type cannot be resolved.Class<?>Resolve this type to aClass, returning the specifiedfallbackif the type cannot be resolved.Class<?>resolveGeneric(int... indexes) Class<?>[]Class<?>[]resolveGenerics(Class<?> fallback) Class<?>toClass()Return this type as a resolvedClass, falling back toObjectif 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
ResolvableTypereturned when no value is available.NONEis used in preference tonullso that multiple method calls can be safely chained.
-
-
Method Details
-
getType
Return the underling JavaTypebeing managed. -
getRawClass
Return the underlying JavaClassbeing managed, if available; otherwisenull. -
getSource
Return the underlying source of the resolvable type. Will return aField,MethodParameterorTypedepending on how theResolvableTypewas 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 toObjectif no specific class can be resolved.- Returns:
- the resolved
Classor theObjectfallback - 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 thisResolvableTypeis assignable from the specified other type.- Parameters:
other- the type to be checked against (as aClass)- Since:
- 4.2
- See Also:
-
isAssignableFrom
Determine whether thisResolvableTypeis assignable from the specified other type.Attempts to follow the same rules as the Java compiler, considering whether both the
resolvedClassisassignable fromthe given type as well as whether allgenericsare assignable.- Parameters:
other- the type to be checked against (as aResolvableType)- Returns:
trueif the specified other type can be assigned to thisResolvableType;falseotherwise
-
isArray
public boolean isArray()Returntrueif this type resolves to a Class that represents an array.- See Also:
-
getComponentType
Return the ResolvableType representing the component type of the array orNONEif this type does not represent an array.- See Also:
-
asCollection
Convenience method to return this type as a resolvableCollectiontype.Returns
NONEif this type does not implement or extendCollection. -
asMap
Convenience method to return this type as a resolvableMaptype.- See Also:
-
as
Return this type as aResolvableTypeof the specified class. Searchessupertypeandinterfacehierarchies to find a match, returningNONEif this type does not implement or extend the specified class.- Parameters:
type- the required type (typically narrowed)- Returns:
- a
ResolvableTyperepresenting this object as the specified type, orNONEif not resolvable as that type - See Also:
-
getSuperType
Return aResolvableTyperepresenting the direct supertype of this type.If no supertype is available this method returns
NONE.Note: The resulting
ResolvableTypeinstance may not beSerializable.- See Also:
-
getInterfaces
Return aResolvableTypearray representing the direct interfaces implemented by this type. If this type does not implement any interfaces an empty array is returned.Note: The resulting
ResolvableTypeinstances may not beSerializable.- See Also:
-
hasGenerics
public boolean hasGenerics()Returntrueif 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 betrueonly in those two scenarios. -
getNested
Return aResolvableTypefor the specified nesting level.See
getNested(int, Map)for details.- Parameters:
nestingLevel- the nesting level- Returns:
- the
ResolvableTypetype, or#NONE
-
getNested
public ResolvableType getNested(int nestingLevel, @Nullable Map<Integer, Integer> typeIndexesPerLevel) Return aResolvableTypefor 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
typeIndexesPerLevelmap can be used to reference a specific generic for the given level. For example, an index of 0 would refer to aMapkey; 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. aMapvalue).Nesting levels may also apply to array types; for example given
String[], a nesting level of 2 refers toString.If a type does not
containgenerics thesupertypehierarchy 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
ResolvableTypefor the nested level, orNONE
-
getGeneric
Return aResolvableTyperepresenting 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 theStringfrom the nestedList. For convenience, if no indexes are specified the first generic is returned.If no generic is available at the specified indexes
NONEis returned.- Parameters:
indexes- the indexes that refer to the generic parameter (can be omitted to return the first generic)- Returns:
- a
ResolvableTypefor the specified generic, orNONE - See Also:
-
getGenerics
Return an array ofResolvableType ResolvableTypesrepresenting 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 ResolvableTypesrepresenting the generic parameters (nevernull) - See Also:
-
resolveGenerics
- Returns:
- an array of resolved generic parameters (the resulting array
will never be
null, but it may containnullelements) - See Also:
-
resolveGenerics
Convenience method that willgetandresolvegeneric parameters, using the specifiedfallbackif 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
Classornull - See Also:
-
resolve
Resolve this type to aClass, returningnullif the type cannot be resolved. This method will consider bounds ofTypeVariablesandWildcardTypesif direct resolution fails; however, bounds ofObject.classwill be ignored.If this method returns a non-null
ClassandhasGenerics()returnsfalse, the given type effectively wraps a plainClass, allowing for plainClassprocessing if desirable.- Returns:
- the resolved
Class, ornullif not resolvable - See Also:
-
resolve
Resolve this type to aClass, returning the specifiedfallbackif the type cannot be resolved. This method will consider bounds ofTypeVariablesandWildcardTypesif direct resolution fails; however, bounds ofObject.classwill be ignored.- Parameters:
fallback- the fallback class to use if resolution fails- Returns:
- the resolved
Classor thefallback - See Also:
-
equals
Check for full equality of all type resolution artifacts: type as well asTypeProviderandVariableResolver. -
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- theResolvableTypeto 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 aResolvableTypefor the specifiedClass, using the full generic type information for assignability checks.For example:
ResolvableType.forClass(MyArrayList.class).- Parameters:
clazz- the class to introspect (nullis semantically equivalent toObject.classfor typical use cases here)- Returns:
- a
ResolvableTypefor the specified class - See Also:
-
forRawClass
Return aResolvableTypefor 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 (nullis semantically equivalent toObject.classfor typical use cases here)- Returns:
- a
ResolvableTypefor the specified class - Since:
- 4.2
- See Also:
-
forClass
Return aResolvableTypefor 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
ResolvableTypefor the specified base type backed by the given implementation class - See Also:
-
forClassWithGenerics
Return aResolvableTypefor the specifiedClasswith pre-declared generics.- Parameters:
clazz- the class (or interface) to introspectgenerics- the generics of the class- Returns:
- a
ResolvableTypefor the specific class and generics - See Also:
-
forClassWithGenerics
public static ResolvableType forClassWithGenerics(Class<?> clazz, @Nullable ResolvableType... generics) Return aResolvableTypefor the specifiedClasswith pre-declared generics.- Parameters:
clazz- the class (or interface) to introspectgenerics- the generics of the class- Returns:
- a
ResolvableTypefor the specific class and generics - See Also:
-
forInstance
Return aResolvableTypefor the specified instance. The instance does not convey generic information but if it implementsResolvableTypeProvidera more preciseResolvableTypecan be used than the simple one based on theClass instance.- Parameters:
instance- the instance (possiblynull)- Returns:
- a
ResolvableTypefor the specified instance, orNONEfornull - Since:
- 4.2
- See Also:
-
forField
Return aResolvableTypefor the specifiedField.- Parameters:
field- the source field- Returns:
- a
ResolvableTypefor the specified field - See Also:
-
forField
Return aResolvableTypefor the specifiedFieldwith 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
ResolvableTypefor the specified field - See Also:
-
forField
Return aResolvableTypefor the specifiedFieldwith 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
ResolvableTypefor the specified field - See Also:
-
forField
Return aResolvableTypefor the specifiedFieldwith 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 aResolvableTypefor the specifiedFieldwith 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
ResolvableTypefor the specified field - See Also:
-
forConstructorParameter
public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex) Return aResolvableTypefor the specifiedConstructorparameter.- Parameters:
constructor- the source constructor (must not benull)parameterIndex- the parameter index- Returns:
- a
ResolvableTypefor the specified constructor parameter - See Also:
-
forConstructorParameter
public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex, Class<?> implementationClass) Return aResolvableTypefor the specifiedConstructorparameter 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
ResolvableTypefor the specified constructor parameter - See Also:
-
forMethodReturnType
Return aResolvableTypefor the specifiedMethodreturn type.- Parameters:
method- the source for the method return type- Returns:
- a
ResolvableTypefor the specified method return - See Also:
-
forMethodReturnType
Return aResolvableTypefor the specifiedMethodreturn 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
ResolvableTypefor the specified method return - See Also:
-
forMethodParameter
Return aResolvableTypefor the specifiedMethodparameter.- Parameters:
method- the source method (must not benull)parameterIndex- the parameter index- Returns:
- a
ResolvableTypefor the specified method parameter - See Also:
-
forMethodParameter
public static ResolvableType forMethodParameter(Method method, int parameterIndex, Class<?> implementationClass) Return aResolvableTypefor the specifiedMethodparameter 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
ResolvableTypefor the specified method parameter - See Also:
-
forMethodParameter
Return aResolvableTypefor the specifiedMethodParameter.- Parameters:
methodParameter- the source method parameter (must not benull)- Returns:
- a
ResolvableTypefor the specified method parameter - See Also:
-
forMethodParameter
public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable ResolvableType implementationType) Return aResolvableTypefor the specifiedMethodParameterwith 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
ResolvableTypefor the specified method parameter - See Also:
-
forMethodParameter
public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable Type targetType) Return aResolvableTypefor 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
ResolvableTypefor the specified method parameter - See Also:
-
forArrayComponent
Return aResolvableTypeas an array of the specifiedcomponentType.- Parameters:
componentType- the component type- Returns:
- a
ResolvableTypeas an array of the specified component type
-
forType
Return aResolvableTypefor the specifiedType.Note: The resulting
ResolvableTypeinstance may not beSerializable.- Parameters:
type- the source type (potentiallynull)- Returns:
- a
ResolvableTypefor the specifiedType - See Also:
-
forType
Return aResolvableTypefor the specifiedTypebacked by the given owner type.Note: The resulting
ResolvableTypeinstance may not beSerializable.- Parameters:
type- the source type ornullowner- the owner type used to resolve variables- Returns:
- a
ResolvableTypefor the specifiedTypeand owner - See Also:
-
forType
Return aResolvableTypefor the specifiedParameterizedTypeReference.Note: The resulting
ResolvableTypeinstance may not beSerializable.- Parameters:
typeReference- the reference to obtain the source type from- Returns:
- a
ResolvableTypefor the specifiedParameterizedTypeReference - Since:
- 4.3.12
- See Also:
-
clearCache
public static void clearCache()Clear the internalResolvableType/SerializableTypeWrappercache.- Since:
- 4.2
-