Top Description Fields Constructors Methods
com.fasterxml.classmate

public Class TypeResolver

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@SuppressWarnings:serial
Imports
java.io.Serializable, java.lang.reflect.*, java.util.*, com.fasterxml.classmate.types.*, com.fasterxml.classmate.util.ClassKey, .ClassStack, .ResolvedTypeCache, .ResolvedTypeKey

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

Modifier and TypeField and Description
protected static final 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 final ResolvedTypeCache
_resolvedTypes

Simple cache of types resolved by this resolver.

private static final ResolvedType[]
private static final ResolvedObjectType
sJavaLangObject

We will also need to return "unknown" type for cases where type variable binding is not found ('raw' instances of generic types); easiest way is to pre-create type for java.lang.Object

Constructor Summary

AccessConstructor and Description
public
TypeResolver()

Constructs type cache; equivalent to:

 
   TypeResolver(ResolvedTypeCache.lruCache(200));
public
TypeResolver(ResolvedTypeCache
Cache to use for avoiding repeated resolution of already resolved types
typeCache
)

Constructor that specifies type cache to use.

Method Summary

Modifier and TypeMethod and Description
private ResolvedType
_constructType(ClassStack context, Class<?> rawType, TypeBindings typeBindings)

private ResolvedType
_fromAny(ClassStack context, Type mainType, TypeBindings typeBindings)

private ResolvedType
_fromArrayType(ClassStack context, GenericArrayType arrayType, TypeBindings typeBindings)

private ResolvedType
_fromClass(ClassStack context, Class<?> rawType, TypeBindings typeBindings)

private ResolvedType
_fromGenericType(ClassStack context, GenericType<?> generic, TypeBindings typeBindings)

Factory method for resolving given generic type, defined by using sub-class instance of GenericType

private ResolvedType
_fromParamType(ClassStack context, ParameterizedType ptype, TypeBindings parentBindings)

private ResolvedType
_fromVariable(ClassStack context, TypeVariable<?> variable, TypeBindings typeBindings)

private ResolvedType
_fromWildcard(ClassStack context, WildcardType wildType, TypeBindings typeBindings)

private ResolvedType

Returns:

Usually a ResolvedObjectType, but possibly also ResolvedRecursiveType
_resolveSuperClass
(ClassStack context, Class<?> rawType, TypeBindings typeBindings)

Note

return type changed in 1.0.1 from ResolvedObjectType to ResolvedType, since it was found that other types may be returned...

private ResolvedType[]
_resolveSuperInterfaces(ClassStack context, Class<?> rawType, TypeBindings typeBindings)

private void
_resolveTypePlaceholders(ResolvedType
Original base type used for specification/refinement
sourceType
,
ResolvedType
Base type instance after re-resolving, possibly containing type placeholders
actualType
)

Method called to verify that types match; and if there are any placeholders, replace them in actualType.

private boolean
public ResolvedArrayType
arrayType(Type elementType)

Factory method for constructing array type of given element type.

public static boolean
isSelfReference(ResolvedType
Type to check
type
)

Convenience 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

public ResolvedType
resolve(Type type, Type... typeParameters)

Factory method for resolving given base type using specified types as type parameters.

public ResolvedType
resolve(TypeBindings typeBindings, Type jdkType)

Factory method for resolving specified Java java.lang.reflect.Type, given TypeBindings needed to resolve any type variables.

public ResolvedType

Returns:

Resolved subtype
resolveSubtype
(ResolvedType
Type to subtype (extend)
supertype
,
final Class<?>
Type-erased sub-class or sub-interface
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 properly extend or implement specified supertype).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_primitiveTypesback to summary
protected static final 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. Same goes for limited number of other "standard" types...

_resolvedTypesback to summary
protected final ResolvedTypeCache _resolvedTypes

Simple cache of types resolved by this resolver. Caching works because type instances themselves are mostly immutable; and properly synchronized in cases where transient data (raw members) are accessed.

NO_TYPESback to summary
private static final ResolvedType[] NO_TYPES
sJavaLangObjectback to summary
private static final ResolvedObjectType sJavaLangObject

We will also need to return "unknown" type for cases where type variable binding is not found ('raw' instances of generic types); easiest way is to pre-create type for java.lang.Object

Constructor Detail

TypeResolverback to summary
public TypeResolver()

Constructs type cache; equivalent to:

 
   TypeResolver(ResolvedTypeCache.lruCache(200));
TypeResolverback to summary
public TypeResolver(ResolvedTypeCache typeCache)

Constructor that specifies type cache to use.

Parameters
typeCache:ResolvedTypeCache

Cache to use for avoiding repeated resolution of already resolved types

Since
1.4

Method Detail

_constructTypeback to summary
private ResolvedType _constructType(ClassStack context, Class<?> rawType, TypeBindings typeBindings)
_fromAnyback to summary
private ResolvedType _fromAny(ClassStack context, Type mainType, TypeBindings typeBindings)
_fromArrayTypeback to summary
private ResolvedType _fromArrayType(ClassStack context, GenericArrayType arrayType, TypeBindings typeBindings)
_fromClassback to summary
private ResolvedType _fromClass(ClassStack context, Class<?> rawType, TypeBindings typeBindings)
_fromGenericTypeback to summary
private ResolvedType _fromGenericType(ClassStack context, GenericType<?> generic, TypeBindings typeBindings)

Factory method for resolving given generic type, defined by using sub-class instance of GenericType

_fromParamTypeback to summary
private ResolvedType _fromParamType(ClassStack context, ParameterizedType ptype, TypeBindings parentBindings)
_fromVariableback to summary
private ResolvedType _fromVariable(ClassStack context, TypeVariable<?> variable, TypeBindings typeBindings)
_fromWildcardback to summary
private ResolvedType _fromWildcard(ClassStack context, WildcardType wildType, TypeBindings typeBindings)
_resolveSuperClassback to summary
private ResolvedType _resolveSuperClass(ClassStack context, Class<?> rawType, TypeBindings typeBindings)

Note

return type changed in 1.0.1 from ResolvedObjectType to ResolvedType, since it was found that other types may be returned...

Returns:ResolvedType

Usually a ResolvedObjectType, but possibly also ResolvedRecursiveType

_resolveSuperInterfacesback to summary
private ResolvedType[] _resolveSuperInterfaces(ClassStack context, Class<?> rawType, TypeBindings typeBindings)
_resolveTypePlaceholdersback to summary
private void _resolveTypePlaceholders(ResolvedType sourceType, ResolvedType actualType) throws IllegalArgumentException

Method called to verify that types match; and if there are any placeholders, replace them in actualType.

Parameters
sourceType:ResolvedType

Original base type used for specification/refinement

actualType:ResolvedType

Base type instance after re-resolving, possibly containing type placeholders

_verifyAndResolveback to summary
private boolean _verifyAndResolve(ResolvedType exp, ResolvedType act)
arrayTypeback to summary
public ResolvedArrayType arrayType(Type elementType)

Factory method for constructing array type of given element type.

isSelfReferenceback to summary
public static boolean isSelfReference(ResolvedType type)

Convenience 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

Parameters
type:ResolvedType

Type to check

resolveback to summary
public ResolvedType resolve(Type type, Type... typeParameters)

Factory method for resolving given base type using specified types as type parameters. Sample usage would be:

  ResolvedType type = TypeResolver.resolve(List.class, Integer.class);
which would be equivalent to
  ResolvedType type = TypeResolver.resolve(new GenericType<List<Integer>>() { });
Note that you can mix different types of type parameters, whether already resolved (ResolvedType), type-erased (java.lang.Class) or generic type reference (GenericType).
resolveback to summary
public ResolvedType resolve(TypeBindings typeBindings, Type jdkType)

Factory method for resolving specified Java java.lang.reflect.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).

Note

order of arguments was reversed for 0.8, to avoid problems with overload varargs method.

resolveSubtypeback to summary
public ResolvedType resolveSubtype(ResolvedType supertype, final Class<?> subtype) throws IllegalArgumentException, UnsupportedOperationException

Factory method for constructing sub-classing specified type; class specified as sub-class must be compatible according to basic Java inheritance rules (subtype must properly extend or implement specified supertype).

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.

Parameters
supertype:ResolvedType

Type to subtype (extend)

subtype:Class<?>

Type-erased sub-class or sub-interface

Returns:ResolvedType

Resolved subtype

Exceptions
IllegalArgumentException:
If this type can be extended in general, but not into specified sub-class
UnsupportedOperationException:
If this type can not be sub-classed