|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.fasterxml.classmate.TypeResolver
public class TypeResolver
Object that is used for resolving generic type information of a class so that it is accessible using simple API. Resolved types are also starting point for accessing resolved (generics aware) return and argument types of class members (methods, fields, constructors).
Note that resolver instances are stateful in that resolvers cache resolved types for efficiency. Since this is internal state and not directly visible to callers, access to state is fully synchronized so that access from multiple threads is safe.
Field Summary | |
---|---|
protected static HashMap<ClassKey,ResolvedType> |
_primitiveTypes
Since number of primitive types is small, and they are frequently needed, let's actually pre-create them for efficient reuse. |
protected ResolvedTypeCache |
_resolvedTypes
Simple cache of types resolved by this resolved; capped to last 200 resolved types. |
Constructor Summary | |
---|---|
TypeResolver()
|
Method Summary | |
---|---|
ResolvedArrayType |
arrayType(ResolvedType elementType)
Factory method for constructing array type of given element type. |
static boolean |
isSelfReference(ResolvedType type)
Helper method that can be used to checked whether given resolved type (with erased type of java.lang.Object ) is a placeholder
for "self-reference"; these are nasty recursive ("self") types
needed with some interfaces |
ResolvedType |
resolve(Class<?> rawType)
Factory method for resolving a type-erased class; in this case any generic type information has to come from super-types (via inheritance). |
ResolvedType |
resolve(Class<?> type,
Class<?>... typeParameters)
Factory method for resolving given type (specified by type-erased class), using specified types as type parameters. |
ResolvedType |
resolve(Class<?> type,
ResolvedType... typeParameters)
Factory method for resolving given type (specified by type-erased class), using specified types as type parameters. |
ResolvedType |
resolve(GenericType<?> generic)
Factory method for resolving given generic type, defined by using sub-class instance of GenericType |
ResolvedType |
resolve(Type jdkType,
TypeBindings typeBindings)
Factory method for resolving specified Java Type , given
TypeBindings needed to resolve any type variables. |
ResolvedType |
resolveSubtype(ResolvedType supertype,
Class<?> subtype)
Factory method for constructing sub-classing specified type; class specified as sub-class must be compatible according to basic Java inheritance rules (subtype must propery extend or implement specified supertype). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final HashMap<ClassKey,ResolvedType> _primitiveTypes
protected final ResolvedTypeCache _resolvedTypes
Constructor Detail |
---|
public TypeResolver()
Method Detail |
---|
public ResolvedType resolve(Class<?> rawType)
public ResolvedType resolve(Class<?> type, Class<?>... typeParameters)
ResolvedType type = TypeResolver.resolve(List.class, Integer.class);which would be equivalent to
ResolvedType type = TypeResolver.resolve(new GenericType<List<Integer>>() { });
public ResolvedType resolve(Class<?> type, ResolvedType... typeParameters)
ResolvedType valueType = TypeResolver.resolve(new GenericType<Set<String>>() { }); ResolvedType type = TypeResolver.resolve(List.class, valueType);which would be equivalent to
ResolvedType type = TypeResolver.resolve(new GenericType<List<Set<String>>() { });
public ResolvedType resolve(GenericType<?> generic)
GenericType
public ResolvedArrayType arrayType(ResolvedType elementType)
public ResolvedType resolve(Type jdkType, TypeBindings typeBindings)
Type
, given
TypeBindings
needed to resolve any type variables.
Use of this method is discouraged (use if and only if you really know what you
are doing!); but if used, type bindings passed should come from ResolvedType
instance of declaring class (or interface).
public ResolvedType resolveSubtype(ResolvedType supertype, Class<?> subtype) throws IllegalArgumentException, UnsupportedOperationException
A typical use case here is to refine a generic type; for example, given
that we have generic type like List<Integer>
, but we want
a more specific implementation type like
class ArrayList
but with same parameterization (here just Integer
),
we could achieve it by:
ResolvedType mapType = typeResolver.resolve(List.class, Integer.class); ResolveType concreteMapType = typeResolver.resolveSubType(mapType, ArrayList.class);(in this case, it would have been simpler to resolve directly; but in some cases we are handled supertype and want to refine it, in which case steps would be the same but separated by other code)
Note that this method will fail if extension can not succeed; either because
this type is not extendable (sub-classable) -- which is true for primitive
and array types -- or because given class is not a subtype of this type.
To check whether subtyping could succeed, you can call
ResolvedType.canCreateSubtypes()
to see if supertype can ever
be extended.
supertype
- Type to subtype (extend)subclass
- Type-erased sub-class or sub-interface
IllegalArgumentException
- If this type can be extended in general, but not into specified sub-class
UnsupportedOperationException
- If this type can not be sub-classedpublic static boolean isSelfReference(ResolvedType type)
java.lang.Object
) is a placeholder
for "self-reference"; these are nasty recursive ("self") types
needed with some interfaces
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |