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.| Modifier and Type | Class and Description |
|---|---|
static interface |
ResolvableType.VariableResolver
Strategy interface used to resolve
TypeVariables. |
| Modifier and Type | Field and Description |
|---|---|
static ResolvableType |
NONE
ResolvableType returned when no value is available. |
| Modifier and Type | Method and Description |
|---|---|
ResolvableType |
as(Class<?> type)
Return this type as a
ResolvableType of the specified class. |
ResolvableType |
asCollection()
Convenience method to return this type as a resolvable
Collection type. |
ResolvableType |
asMap()
Convenience method to return this type as a resolvable
Map type. |
boolean |
equals(Object other) |
static ResolvableType |
forArrayComponent(ResolvableType componentType)
Return a
ResolvableType as a array of the specified componentType. |
static ResolvableType |
forClass(Class<?> sourceClass)
Return a
ResolvableType for the specified Class, using the full generic type information for
assignability checks. |
static ResolvableType |
forClass(Class<?> sourceClass,
Class<?> implementationClass)
Return a
ResolvableType for the specified Class with a given implementation. |
static ResolvableType |
forClassWithGenerics(Class<?> sourceClass,
Class<?>... generics)
Return a
ResolvableType for the specified Class with pre-declared generics. |
static ResolvableType |
forClassWithGenerics(Class<?> sourceClass,
ResolvableType... generics)
Return a
ResolvableType for the specified Class with pre-declared generics. |
static ResolvableType |
forMethodParameter(Method method,
int parameterIndex)
Return a
ResolvableType for the specified Method parameter. |
static ResolvableType |
forMethodParameter(Method method,
int parameterIndex,
Class<?> implementationClass)
Return a
ResolvableType for the specified Method parameter with a given implementation. |
static ResolvableType |
forMethodParameter(MethodParameter methodParameter)
Return a
ResolvableType for the specified MethodParameter. |
static ResolvableType |
forMethodParameter(MethodParameter methodParameter,
ResolvableType implementationType)
Return a
ResolvableType for the specified MethodParameter with a given implementation type. |
static ResolvableType |
forMethodParameter(MethodParameter methodParameter,
Type targetType)
Return a
ResolvableType for the specified MethodParameter, overriding the target type to resolve with a
specific given type. |
static ResolvableType |
forMethodReturnType(Method method)
Return a
ResolvableType for the specified Method return type. |
static ResolvableType |
forMethodReturnType(Method method,
Class<?> implementationClass)
Return a
ResolvableType for the specified Method return type. |
static ResolvableType |
forRawClass(Class<?> sourceClass)
Return a
ResolvableType for the specified Class, doing assignability checks against the raw class only
(analogous to Class.isAssignableFrom(java.lang.Class<?>), which this serves as a wrapper for. |
static ResolvableType |
forType(Type type)
Return a
ResolvableType for the specified Type. |
static ResolvableType |
forType(Type type,
ResolvableType.VariableResolver variableResolver)
|
static ResolvableType |
forType(Type type,
ResolvableType owner)
Return a
ResolvableType for the specified Type backed by the given owner type. |
ResolvableType |
getComponentType()
Return the ResolvableType representing the component type of the array or
NONE if this type does not represent
an array. |
ResolvableType |
getGeneric(int... indexes)
Return a
ResolvableType representing the generic parameter for the given indexes. |
ResolvableType[] |
getGenerics()
Return an array of
ResolvableTypes representing the generic parameters of this type. |
ResolvableType[] |
getInterfaces()
Return a
ResolvableType array representing the direct interfaces implemented by this type. |
ResolvableType |
getNested(int nestingLevel)
Return a
ResolvableType for the specified nesting level. |
ResolvableType |
getNested(int nestingLevel,
Map<Integer,Integer> typeIndexesPerLevel)
Return a
ResolvableType for the specified nesting level. |
Class<?> |
getRawClass()
Return the underlying Java
Class being managed, if available; otherwise null. |
Object |
getSource()
Return the underlying source of the resolvable type.
|
ResolvableType |
getSuperType()
Return a
ResolvableType representing the direct supertype of this type. |
Type |
getType()
Return the underling Java
Type being managed. |
boolean |
hasGenerics()
Return
true if this type contains generic parameters. |
int |
hashCode() |
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.
|
boolean |
isArray()
Return
true if this type resolves to a Class that represents an array. |
boolean |
isAssignableFrom(Class<?> other)
Determine whether this
ResolvableType is assignable from the specified other type. |
boolean |
isAssignableFrom(ResolvableType other)
Determine whether this
ResolvableType is assignable from the specified other type. |
boolean |
isInstance(Object obj)
Determine whether the given object is an instance of this
ResolvableType. |
Class<?> |
resolve()
Resolve this type to a
Class, returning null if the type cannot be resolved. |
Class<?> |
resolve(Class<?> fallback)
Resolve this type to a
Class, returning the specified fallback if the type cannot be resolved. |
Class<?> |
resolveGeneric(int... indexes)
|
Class<?>[] |
resolveGenerics()
|
Class<?>[] |
resolveGenerics(Class<?> fallback)
|
ResolvableType |
resolveType()
Resolve this type by a single level, returning the resolved value or
NONE. |
String |
toString()
Return a String representation of this type in its fully resolved form (including any generic parameters).
|
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.public Type getType()
public Class<?> getRawClass()
Class being managed, if available; otherwise null.public Object getSource()
Field, MethodParameter or Type
depending on how the ResolvableType was constructed. With the exception of the NONE constant, this
method will never return null. This method is primarily to provide access to additional type information or
meta-data that alternative JVM languages may provide.public boolean isInstance(Object obj)
ResolvableType.obj - the object to check.isAssignableFrom(Class)public boolean isAssignableFrom(Class<?> other)
ResolvableType is assignable from the specified other type.other - the type to be checked against (as a Class).isAssignableFrom(ResolvableType)public boolean isAssignableFrom(ResolvableType other)
ResolvableType is assignable from the specified other type.
Attempts to follow the same rules as the Java compiler, considering whether both the resolved
Class is assignable from the given type as well as whether all
generics are assignable.
other - the type to be checked against (as a ResolvableType).true if the specified other type can be assigned to this ResolvableType; false otherwise.public boolean isArray()
true if this type resolves to a Class that represents an array.getComponentType()public ResolvableType getComponentType()
NONE if this type does not represent
an array.isArray()public ResolvableType asCollection()
Collection type. Returns NONE if this type does
not implement or extend Collection.public ResolvableType asMap()
Map type. Returns NONE if this type does not
implement or extend Map.as(Class),
asCollection()public ResolvableType as(Class<?> type)
ResolvableType of the specified class. Searches supertype and
interface hierarchies to find a match, returning NONE if this type does not implement
or extend the specified class.type - the required class type.ResolvableType representing this object as the specified type, or NONE if not resolvable as
that type.asCollection(),
asMap(),
getSuperType(),
getInterfaces()public ResolvableType getSuperType()
ResolvableType representing the direct supertype of this type. If no supertype is available this method
returns NONE.getInterfaces()public ResolvableType[] getInterfaces()
ResolvableType array representing the direct interfaces implemented by this type. If this type does not
implement any interfaces an empty array is returned.getSuperType()public boolean hasGenerics()
true if this type contains generic parameters.getGeneric(int...),
getGenerics()public boolean hasUnresolvableGenerics()
true only in those two scenarios.public ResolvableType getNested(int nestingLevel)
ResolvableType for the specified nesting level. See getNested(int, Map) for details.nestingLevel - the nesting level.ResolvableType type, or #NONE.public ResolvableType getNested(int nestingLevel, Map<Integer,Integer> typeIndexesPerLevel)
ResolvableType 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 the List, level 2 the
Set, and level 3 the Integer.
The typeIndexesPerLevel map can be used to reference a specific generic for the given level. For example, an
index of 0 would refer to a Map 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. a Map value).
Nesting levels may also apply to array types; for example given String[], a nesting level of 2 refers to
String.
If a type does not contain generics the supertype hierarchy will be
considered.
nestingLevel - the required nesting level, indexed from 1 for the current type, 2 for the first nested generic, 3
for the second and so on.typeIndexesPerLevel - a map containing the generic index for a given nesting level (may be null).ResolvableType for the nested level or NONE.public ResolvableType getGeneric(int... indexes)
ResolvableType representing the generic parameter for the given indexes. Indexes are zero based; for
example given the type Map<Integer, List<String>>, getGeneric(0) will access the Integer. Nested
generics can be accessed by specifying multiple indexes; for example getGeneric(1, 0) will access the
String from the nested List. For convenience, if no indexes are specified the first generic is returned.
If no generic is available at the specified indexes NONE is returned.
indexes - the indexes that refer to the generic parameter (may be omitted to return the first generic).ResolvableType for the specified generic or NONE.hasGenerics(),
getGenerics(),
resolveGeneric(int...),
resolveGenerics()public ResolvableType[] getGenerics()
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 getGeneric(int...)
method as it allows access to nested generics and protects against IndexOutOfBoundsExceptions.ResolvableTypes representing the generic parameters (never null).hasGenerics(),
getGeneric(int...),
resolveGeneric(int...),
resolveGenerics()public Class<?>[] resolveGenerics()
null, but it may contain
null elements}).getGenerics(),
resolve()public Class<?>[] resolveGenerics(Class<?> fallback)
get and resolve generic parameters, using the
specified fallback if any type cannot be resolved.fallback - the fallback class to use if resolution fails (may be null).null, but it may contain
null elements}).getGenerics(),
resolve()public Class<?> resolveGeneric(int... indexes)
indexes - the indexes that refer to the generic parameter (may be omitted to return the first generic).Class or null.getGeneric(int...),
resolve()public Class<?> resolve()
Class, returning null if the type cannot be resolved. This method will
consider bounds of TypeVariables and WildcardTypes if direct resolution fails; however, bounds of
Object.class will be ignored.Class, or null if not resolvable.resolve(Class),
resolveGeneric(int...),
resolveGenerics()public Class<?> resolve(Class<?> fallback)
Class, returning the specified fallback if the type cannot be resolved.
This method will consider bounds of TypeVariables and WildcardTypes if direct resolution fails; however,
bounds of Object.class will be ignored.fallback - the fallback class to use if resolution fails (may be null).Class or the fallback.resolve(),
resolveGeneric(int...),
resolveGenerics()public ResolvableType resolveType()
NONE.
Note: The returned ResolvableType should only be used as an intermediary as it cannot be serialized.
public String toString()
public static ResolvableType forClass(Class<?> sourceClass)
ResolvableType for the specified Class, using the full generic type information for
assignability checks. For example: ResolvableType.forClass(MyArrayList.class).sourceClass - the source class (null is semantically equivalent to Object.class for typical use
cases here}.ResolvableType for the specified class.forClass(Class, Class),
forClassWithGenerics(Class, Class...)public static ResolvableType forRawClass(Class<?> sourceClass)
ResolvableType for the specified Class, doing assignability checks against the raw class only
(analogous to Class.isAssignableFrom(java.lang.Class<?>), which this serves as a wrapper for. For example:
ResolvableType.forClass(MyArrayList.class).sourceClass - the source class (null is semantically equivalent to Object.class for typical use
cases here}.ResolvableType for the specified class.forClass(Class),
getRawClass()public static ResolvableType forClass(Class<?> sourceClass, Class<?> implementationClass)
ResolvableType for the specified Class with a given implementation. For example:
ResolvableType.forClass(List.class, MyArrayList.class).sourceClass - the source class (must not be null.implementationClass - the implementation class.ResolvableType for the specified class backed by the given implementation class.forClass(Class),
forClassWithGenerics(Class, Class...)public static ResolvableType forClassWithGenerics(Class<?> sourceClass, Class<?>... generics)
ResolvableType for the specified Class with pre-declared generics.sourceClass - the source class.generics - the generics of the class.ResolvableType for the specific class and generics.forClassWithGenerics(Class, ResolvableType...)public static ResolvableType forClassWithGenerics(Class<?> sourceClass, ResolvableType... generics)
ResolvableType for the specified Class with pre-declared generics.sourceClass - the source class.generics - the generics of the class.ResolvableType for the specific class and generics.forClassWithGenerics(Class, Class...)public static ResolvableType forMethodReturnType(Method method)
ResolvableType for the specified Method return type.method - the source for the method return type.ResolvableType for the specified method return.forMethodReturnType(Method, Class)public static ResolvableType forMethodReturnType(Method method, Class<?> implementationClass)
ResolvableType for the specified Method return type. Use this variant when the class that
declares the method includes generic parameter variables that are satisfied by the implementation class.method - the source for the method return type.implementationClass - the implementation class.ResolvableType for the specified method return.forMethodReturnType(Method)public static ResolvableType forMethodParameter(Method method, int parameterIndex)
ResolvableType for the specified Method parameter.method - the source method (must not be null).parameterIndex - the parameter index.ResolvableType for the specified method parameter.forMethodParameter(Method, int, Class),
forMethodParameter(MethodParameter)public static ResolvableType forMethodParameter(Method method, int parameterIndex, Class<?> implementationClass)
ResolvableType for the specified Method 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.method - the source method (must not be null).parameterIndex - the parameter index.implementationClass - the implementation class.ResolvableType for the specified method parameter.forMethodParameter(Method, int, Class),
forMethodParameter(MethodParameter)public static ResolvableType forMethodParameter(MethodParameter methodParameter)
ResolvableType for the specified MethodParameter.methodParameter - the source method parameter (must not be null).ResolvableType for the specified method parameter.forMethodParameter(Method, int)public static ResolvableType forMethodParameter(MethodParameter methodParameter, ResolvableType implementationType)
ResolvableType for the specified MethodParameter 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.methodParameter - the source method parameter (must not be null).implementationType - the implementation type.ResolvableType for the specified method parameter.forMethodParameter(MethodParameter)public static ResolvableType forMethodParameter(MethodParameter methodParameter, Type targetType)
ResolvableType for the specified MethodParameter, overriding the target type to resolve with a
specific given type.methodParameter - the source method parameter (must not be null).targetType - the type to resolve (a part of the method parameter's type).ResolvableType for the specified method parameter.forMethodParameter(Method, int)public static ResolvableType forArrayComponent(ResolvableType componentType)
ResolvableType as a array of the specified componentType.componentType - the component type.ResolvableType as an array of the specified component type.public static ResolvableType forType(Type type)
ResolvableType for the specified Type. Note: The resulting ResolvableType may not be
Serializable.type - the source type or null.ResolvableType for the specified Type.forType(Type, ResolvableType)public static ResolvableType forType(Type type, ResolvableType owner)
ResolvableType for the specified Type backed by the given owner type. Note: The resulting
ResolvableType may not be Serializable.type - the source type or null.owner - the owner type used to resolve variables.ResolvableType for the specified Type and owner.forType(Type)public static ResolvableType forType(Type type, ResolvableType.VariableResolver variableResolver)
type - the source type or null.variableResolver - the variable resolver or null.ResolvableType for the specified Type and ResolvableType.VariableResolver.Copyright © 2020 lettuce.io. All rights reserved.