- All Implemented Interfaces:
Serializable
public class ResolvableType extends Object implements Serializable
Type, providing access to supertypes,
interfaces, and generic parameters along with the ability to ultimately
resolve to a Class.- See Also:
- Serialized Form
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceResolvableType.VariableResolverStrategy interface used to resolveTypeVariables. -
Field Summary
Fields Modifier and Type Field Description static ResolvableTypeNONEResolvableTypereturned when no value is available. -
Method Summary
Modifier and Type Method Description ResolvableTypeas(Class<?> type)Return this type as aResolvableTypeof the specified class.ResolvableTypeasCollection()Convenience method to return this type as a resolvableCollectiontype.ResolvableTypeasMap()Convenience method to return this type as a resolvableMaptype.booleanequals(Object other)static ResolvableTypeforArrayComponent(ResolvableType componentType)Return aResolvableTypeas a array of the specifiedcomponentType.static ResolvableTypeforClass(Class<?> sourceClass)Return aResolvableTypefor the specifiedClass, using the full generic type information for assignability checks.static ResolvableTypeforClass(Class<?> sourceClass, Class<?> implementationClass)Return aResolvableTypefor the specifiedClasswith a given implementation.static ResolvableTypeforClassWithGenerics(Class<?> sourceClass, ResolvableType... generics)Return aResolvableTypefor the specifiedClasswith pre-declared generics.static ResolvableTypeforClassWithGenerics(Class<?> sourceClass, Class<?>... generics)Return aResolvableTypefor the specifiedClasswith pre-declared generics.static ResolvableTypeforMethodParameter(MethodParameter methodParameter)Return aResolvableTypefor the specifiedMethodParameter.static ResolvableTypeforMethodParameter(MethodParameter methodParameter, ResolvableType implementationType)Return aResolvableTypefor the specifiedMethodParameterwith a given implementation type.static ResolvableTypeforMethodParameter(MethodParameter methodParameter, Type targetType)Return aResolvableTypefor the specifiedMethodParameter, overriding the target type to resolve with a specific given type.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 ResolvableTypeforMethodReturnType(Method method)Return aResolvableTypefor the specifiedMethodreturn type.static ResolvableTypeforMethodReturnType(Method method, Class<?> implementationClass)Return aResolvableTypefor the specifiedMethodreturn type.static ResolvableTypeforRawClass(Class<?> sourceClass)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 ResolvableTypeforType(Type type)Return aResolvableTypefor the specifiedType.static ResolvableTypeforType(Type type, ResolvableType owner)Return aResolvableTypefor the specifiedTypebacked by the given owner type.static ResolvableTypeforType(Type type, ResolvableType.VariableResolver variableResolver)ResolvableTypegetComponentType()Return the ResolvableType representing the component type of the array orNONEif this type does not represent an array.ResolvableTypegetGeneric(int... indexes)Return aResolvableTyperepresenting the generic parameter for the given indexes.ResolvableType[]getGenerics()Return an array ofResolvableTypes representing the generic parameters of this type.ResolvableType[]getInterfaces()Return aResolvableTypearray representing the direct interfaces implemented by this type.ResolvableTypegetNested(int nestingLevel)Return aResolvableTypefor the specified nesting level.ResolvableTypegetNested(int nestingLevel, Map<Integer,Integer> typeIndexesPerLevel)Return aResolvableTypefor the specified nesting level.Class<?>getRawClass()Return the underlying JavaClassbeing managed, if available; otherwisenull.ObjectgetSource()Return the underlying source of the resolvable type.ResolvableTypegetSuperType()Return aResolvableTyperepresenting the direct supertype of this type.TypegetType()Return the underling JavaTypebeing managed.booleanhasGenerics()Returntrueif this type contains generic parameters.inthashCode()booleanhasUnresolvableGenerics()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.booleanisArray()Returntrueif this type resolves to a Class that represents an array.booleanisAssignableFrom(ResolvableType other)Determine whether thisResolvableTypeis assignable from the specified other type.booleanisAssignableFrom(Class<?> 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(Class<?> fallback)Resolve this type to aClass, returning the specifiedfallbackif the type cannot be resolved.Class<?>resolveGeneric(int... indexes)Class<?>[]resolveGenerics()Class<?>[]resolveGenerics(Class<?> fallback)ResolvableTyperesolveType()Resolve this type by a single level, returning the resolved value orNONE.StringtoString()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
-
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. With the exception of theNONEconstant, this method will never returnnull. This method is primarily to provide access to additional type information or meta-data that alternative JVM languages may provide. -
isInstance
Determine whether the given object is an instance of thisResolvableType.- Parameters:
obj- the object to check- See Also:
isAssignableFrom(Class)
-
isAssignableFrom
Determine whether thisResolvableTypeis assignable from the specified other type.- Parameters:
other- the type to be checked against (as aClass)- See Also:
isAssignableFrom(ResolvableType)
-
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()
-
getComponentType
Return the ResolvableType representing the component type of the array orNONEif this type does not represent an array.- See Also:
isArray()
-
asCollection
Convenience method to return this type as a resolvableCollectiontype. ReturnsNONEif this type does not implement or extendCollection. -
asMap
Convenience method to return this type as a resolvableMaptype. ReturnsNONEif this type does not implement or extendMap.- See Also:
as(Class),asCollection()
-
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 class type- Returns:
- a
ResolvableTyperepresenting this object as the specified type, orNONEif not resolvable as that type - See Also:
asCollection(),asMap(),getSuperType(),getInterfaces()
-
getSuperType
Return aResolvableTyperepresenting the direct supertype of this type. If no supertype is available this method returnsNONE.- See Also:
getInterfaces()
-
getInterfaces
Return aResolvableTypearray representing the direct interfaces implemented by this type. If this type does not implement any interfaces an empty array is returned.- See Also:
getSuperType()
-
hasGenerics
public boolean hasGenerics()Returntrueif this type contains generic parameters.- See Also:
getGeneric(int...),getGenerics()
-
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. SeegetNested(int, Map)for details.- Parameters:
nestingLevel- the nesting level- Returns:
- the
ResolvableTypetype, or#NONE
-
getNested
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, givenList<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 (may 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 (may be omitted to return the first generic)- Returns:
- a
ResolvableTypefor the specified generic orNONE - See Also:
hasGenerics(),getGenerics(),resolveGeneric(int...),resolveGenerics()
-
getGenerics
Return an array ofResolvableTypes 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
ResolvableTypes representing the generic parameters (nevernull) - See Also:
hasGenerics(),getGeneric(int...),resolveGeneric(int...),resolveGenerics()
-
resolveGenerics
- Returns:
- an array of resolved generic parameters (the resulting array will never be
null, but it may containnullelements}) - See Also:
getGenerics(),resolve()
-
resolveGenerics
Convenience method that willgetandresolvegeneric parameters, using the specifiedfallbackif any type cannot be resolved.- Parameters:
fallback- the fallback class to use if resolution fails (may benull)- Returns:
- an array of resolved generic parameters (the resulting array will never be
null, but it may containnullelements}) - See Also:
getGenerics(),resolve()
-
resolveGeneric
- Parameters:
indexes- the indexes that refer to the generic parameter (may be omitted to return the first generic)- Returns:
- a resolved
Classornull - See Also:
getGeneric(int...),resolve()
-
resolve
Resolve this type to aClass, returningnullif the type cannot be resolved. This method will consider bounds ofTypeVariables andWildcardTypes if direct resolution fails; however, bounds ofObject.classwill be ignored.- Returns:
- the resolved
Class, ornullif not resolvable - See Also:
resolve(Class),resolveGeneric(int...),resolveGenerics()
-
resolve
Resolve this type to aClass, returning the specifiedfallbackif the type cannot be resolved. This method will consider bounds ofTypeVariables andWildcardTypes if direct resolution fails; however, bounds ofObject.classwill be ignored.- Parameters:
fallback- the fallback class to use if resolution fails (may benull)- Returns:
- the resolved
Classor thefallback - See Also:
resolve(),resolveGeneric(int...),resolveGenerics()
-
resolveType
Resolve this type by a single level, returning the resolved value orNONE.Note: The returned
ResolvableTypeshould only be used as an intermediary as it cannot be serialized. -
equals
-
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:
sourceClass- the source class (nullis semantically equivalent toObject.classfor typical use cases here}- Returns:
- a
ResolvableTypefor the specified class - See Also:
forClass(Class, Class),forClassWithGenerics(Class, Class...)
-
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.forClass(MyArrayList.class).- Parameters:
sourceClass- the source class (nullis semantically equivalent toObject.classfor typical use cases here}- Returns:
- a
ResolvableTypefor the specified class - See Also:
forClass(Class),getRawClass()
-
forClass
Return aResolvableTypefor the specifiedClasswith a given implementation. For example:ResolvableType.forClass(List.class, MyArrayList.class).- Parameters:
sourceClass- the source class (must not benullimplementationClass- the implementation class- Returns:
- a
ResolvableTypefor the specified class backed by the given implementation class - See Also:
forClass(Class),forClassWithGenerics(Class, Class...)
-
forClassWithGenerics
Return aResolvableTypefor the specifiedClasswith pre-declared generics.- Parameters:
sourceClass- the source classgenerics- the generics of the class- Returns:
- a
ResolvableTypefor the specific class and generics - See Also:
forClassWithGenerics(Class, ResolvableType...)
-
forClassWithGenerics
public static ResolvableType forClassWithGenerics(Class<?> sourceClass, ResolvableType... generics)Return aResolvableTypefor the specifiedClasswith pre-declared generics.- Parameters:
sourceClass- the source classgenerics- the generics of the class- Returns:
- a
ResolvableTypefor the specific class and generics - See Also:
forClassWithGenerics(Class, Class...)
-
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(Method, Class)
-
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:
forMethodReturnType(Method)
-
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(Method, int, Class),forMethodParameter(MethodParameter)
-
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(Method, int, Class),forMethodParameter(MethodParameter)
-
forMethodParameter
Return aResolvableTypefor the specifiedMethodParameter.- Parameters:
methodParameter- the source method parameter (must not benull)- Returns:
- a
ResolvableTypefor the specified method parameter - See Also:
forMethodParameter(Method, int)
-
forMethodParameter
public static ResolvableType forMethodParameter(MethodParameter methodParameter, 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(MethodParameter)
-
forMethodParameter
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:
forMethodParameter(Method, int)
-
forArrayComponent
Return aResolvableTypeas a 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 resultingResolvableTypemay not beSerializable.- Parameters:
type- the source type ornull- Returns:
- a
ResolvableTypefor the specifiedType - See Also:
forType(Type, ResolvableType)
-
forType
Return aResolvableTypefor the specifiedTypebacked by the given owner type. Note: The resultingResolvableTypemay not beSerializable.- Parameters:
type- the source type ornullowner- the owner type used to resolve variables- Returns:
- a
ResolvableTypefor the specifiedTypeand owner - See Also:
forType(Type)
-
forType
- Parameters:
type- the source type ornullvariableResolver- the variable resolver ornull- Returns:
- a
ResolvableTypefor the specifiedTypeandResolvableType.VariableResolver
-