public final class TypeFactory extends Object implements Serializable
JavaType
instances,
given various inputs.
Instances of this class are accessible using ObjectMapper
as well as many objects it constructs (like
DeserializationConfig
and
SerializationConfig
)),
but usually those objects also
expose convenience methods (constructType
).
So, you can do for example:
JavaType stringType = mapper.constructType(String.class);However, more advanced methods are only exposed by factory so that you may need to use:
JavaType stringCollection = mapper.getTypeFactory().constructCollectionType(List.class, String.class);
Modifier and Type | Field and Description |
---|---|
protected HierarchicType |
_cachedArrayListType
Lazily constructed copy of type hierarchy from
ArrayList
to its supertypes. |
protected HierarchicType |
_cachedHashMapType
Lazily constructed copy of type hierarchy from
HashMap
to its supertypes. |
protected ClassLoader |
_classLoader
ClassLoader used by this factory (Issue #624)
|
protected TypeModifier[] |
_modifiers
Registered
TypeModifier s: objects that can change details
of JavaType instances factory constructs. |
protected TypeParser |
_parser |
protected LRUMap<ClassKey,JavaType> |
_typeCache
Since type resolution can be expensive (specifically when resolving
actual generic types), we will use small cache to avoid repetitive
resolution of core types
|
protected static SimpleType |
CORE_TYPE_BOOL |
protected static SimpleType |
CORE_TYPE_INT |
protected static SimpleType |
CORE_TYPE_LONG |
protected static SimpleType |
CORE_TYPE_STRING |
protected static TypeFactory |
instance
Globally shared singleton.
|
Modifier | Constructor and Description |
---|---|
protected |
TypeFactory(TypeParser p,
TypeModifier[] mods) |
protected |
TypeFactory(TypeParser p,
TypeModifier[] mods,
ClassLoader classLoader) |
Modifier and Type | Method and Description |
---|---|
protected HierarchicType |
_arrayListSuperInterfaceChain(HierarchicType current) |
protected JavaType |
_constructType(Type type,
TypeBindings context)
Factory method that can be used if type information is passed
as Java typing returned from
getGenericXxx methods
(usually for a return or argument type). |
protected HierarchicType |
_doFindSuperInterfaceChain(HierarchicType current,
Class<?> target) |
protected Class<?> |
_findPrimitive(String className) |
protected HierarchicType |
_findSuperClassChain(Type currentType,
Class<?> target) |
protected HierarchicType |
_findSuperInterfaceChain(Type currentType,
Class<?> target) |
protected HierarchicType |
_findSuperTypeChain(Class<?> subtype,
Class<?> supertype)
Helper method used to find inheritance (implements, extends) path
between given types, if one exists (caller generally checks before
calling this method).
|
protected JavaType |
_fromArrayType(GenericArrayType type,
TypeBindings context) |
protected JavaType |
_fromClass(Class<?> clz,
TypeBindings context) |
protected JavaType |
_fromParameterizedClass(Class<?> clz,
List<JavaType> paramTypes)
Method used by
TypeParser when generics-aware version
is constructed. |
protected JavaType |
_fromParamType(ParameterizedType type,
TypeBindings context)
This method deals with parameterized types, that is,
first class generic classes.
|
protected JavaType |
_fromVariable(TypeVariable<?> type,
TypeBindings context) |
protected JavaType |
_fromWildcard(WildcardType type,
TypeBindings context) |
protected HierarchicType |
_hashMapSuperInterfaceChain(HierarchicType current) |
protected JavaType |
_resolveVariableViaSubTypes(HierarchicType leafType,
String variableName,
TypeBindings bindings) |
protected JavaType |
_unknownType() |
protected Class<?> |
classForName(String name) |
protected Class<?> |
classForName(String name,
boolean initialize,
ClassLoader loader) |
void |
clearCache()
Method that will clear up any cached type definitions that may
be cached by this
TypeFactory instance. |
ArrayType |
constructArrayType(Class<?> elementType)
Method for constructing an
ArrayType . |
ArrayType |
constructArrayType(JavaType elementType)
Method for constructing an
ArrayType . |
CollectionLikeType |
constructCollectionLikeType(Class<?> collectionClass,
Class<?> elementClass)
Method for constructing a
CollectionLikeType . |
CollectionLikeType |
constructCollectionLikeType(Class<?> collectionClass,
JavaType elementType)
Method for constructing a
CollectionLikeType . |
CollectionType |
constructCollectionType(Class<? extends Collection> collectionClass,
Class<?> elementClass)
Method for constructing a
CollectionType . |
CollectionType |
constructCollectionType(Class<? extends Collection> collectionClass,
JavaType elementType)
Method for constructing a
CollectionType . |
JavaType |
constructFromCanonical(String canonical)
Factory method for constructing a
JavaType out of its canonical
representation (see ResolvedType.toCanonical() ). |
MapLikeType |
constructMapLikeType(Class<?> mapClass,
Class<?> keyClass,
Class<?> valueClass)
Method for constructing a
MapLikeType instance |
MapLikeType |
constructMapLikeType(Class<?> mapClass,
JavaType keyType,
JavaType valueType)
Method for constructing a
MapLikeType instance |
MapType |
constructMapType(Class<? extends Map> mapClass,
Class<?> keyClass,
Class<?> valueClass)
Method for constructing a
MapType instance |
MapType |
constructMapType(Class<? extends Map> mapClass,
JavaType keyType,
JavaType valueType)
Method for constructing a
MapType instance |
JavaType |
constructParametricType(Class<?> parametrized,
Class<?>... parameterClasses)
Deprecated.
Since 2.5, use
constructParametrizedType(java.lang.Class<?>, java.lang.Class<?>, java.lang.Class<?>...) instead. |
JavaType |
constructParametricType(Class<?> parametrized,
JavaType... parameterTypes)
Deprecated.
Since 2.5, use
constructParametrizedType(java.lang.Class<?>, java.lang.Class<?>, java.lang.Class<?>...) instead. |
JavaType |
constructParametrizedType(Class<?> parametrized,
Class<?> parametersFor,
Class<?>... parameterClasses)
Factory method for constructing
JavaType that
represents a parameterized type. |
JavaType |
constructParametrizedType(Class<?> parametrized,
Class<?> parametersFor,
JavaType... parameterTypes)
Factory method for constructing
JavaType that
represents a parameterized type. |
CollectionLikeType |
constructRawCollectionLikeType(Class<?> collectionClass)
Method that can be used to construct "raw" Collection-like type; meaning that its
parameterization is unknown.
|
CollectionType |
constructRawCollectionType(Class<? extends Collection> collectionClass)
Method that can be used to construct "raw" Collection type; meaning that its
parameterization is unknown.
|
MapLikeType |
constructRawMapLikeType(Class<?> mapClass)
Method that can be used to construct "raw" Map-like type; meaning that its
parameterization is unknown.
|
MapType |
constructRawMapType(Class<? extends Map> mapClass)
Method that can be used to construct "raw" Map type; meaning that its
parameterization is unknown.
|
JavaType |
constructReferenceType(Class<?> rawType,
JavaType refType) |
JavaType |
constructSimpleType(Class<?> rawType,
Class<?> parameterTarget,
JavaType[] parameterTypes)
Method for constructing a type instance with specified parameterization.
|
JavaType |
constructSimpleType(Class<?> rawType,
JavaType[] parameterTypes)
Deprecated.
Since 2.5, use variant that takes one more argument
|
JavaType |
constructSpecializedType(JavaType baseType,
Class<?> subclass)
Factory method for creating a subtype of given base type, as defined
by specified subclass; but retaining generic type information if any.
|
JavaType |
constructType(Type type) |
JavaType |
constructType(Type type,
Class<?> context) |
JavaType |
constructType(Type type,
JavaType context) |
JavaType |
constructType(com.fasterxml.jackson.core.type.TypeReference<?> typeRef) |
JavaType |
constructType(Type type,
TypeBindings bindings) |
static TypeFactory |
defaultInstance()
Method used to access the globally shared instance, which has
no custom configuration.
|
Class<?> |
findClass(String className)
Low-level lookup method moved from
ClassUtil ,
to allow for overriding of lookup functionality in environments like OSGi. |
JavaType[] |
findTypeParameters(Class<?> clz,
Class<?> expType) |
JavaType[] |
findTypeParameters(Class<?> clz,
Class<?> expType,
TypeBindings bindings) |
JavaType[] |
findTypeParameters(JavaType type,
Class<?> expType)
Method that is to figure out actual type parameters that given
class binds to generic types defined by given (generic)
interface or class.
|
ClassLoader |
getClassLoader() |
JavaType |
moreSpecificType(JavaType type1,
JavaType type2)
Method that can be called to figure out more specific of two
types (if they are related; that is, one implements or extends the
other); or if not related, return the primary type.
|
static Class<?> |
rawClass(Type t)
Static helper method that can be called to figure out type-erased
call for given JDK type.
|
JavaType |
uncheckedSimpleType(Class<?> cls)
Method that will force construction of a simple type, without trying to
check for more specialized types.
|
static JavaType |
unknownType()
Method for constructing a marker type that indicates missing generic
type information, which is handled same as simple type for
java.lang.Object . |
TypeFactory |
withClassLoader(ClassLoader classLoader) |
TypeFactory |
withModifier(TypeModifier mod) |
protected static final TypeFactory instance
defaultInstance()
for accessing it.protected static final SimpleType CORE_TYPE_STRING
protected static final SimpleType CORE_TYPE_BOOL
protected static final SimpleType CORE_TYPE_INT
protected static final SimpleType CORE_TYPE_LONG
protected final LRUMap<ClassKey,JavaType> _typeCache
protected transient HierarchicType _cachedHashMapType
HashMap
to its supertypes.protected transient HierarchicType _cachedArrayListType
ArrayList
to its supertypes.protected final TypeModifier[] _modifiers
TypeModifier
s: objects that can change details
of JavaType
instances factory constructs.protected final TypeParser _parser
protected final ClassLoader _classLoader
protected TypeFactory(TypeParser p, TypeModifier[] mods)
protected TypeFactory(TypeParser p, TypeModifier[] mods, ClassLoader classLoader)
public TypeFactory withModifier(TypeModifier mod)
public TypeFactory withClassLoader(ClassLoader classLoader)
public static TypeFactory defaultInstance()
ObjectMapper
to
get the default factory when constructed.public void clearCache()
TypeFactory
instance.
This method should not be commonly used, that is, only use it
if you know there is a problem with retention of type definitions;
the most likely (and currently only known) problem is retention
of Class
instances via JavaType
reference.public ClassLoader getClassLoader()
public static JavaType unknownType()
java.lang.Object
.public static Class<?> rawClass(Type t)
public Class<?> findClass(String className) throws ClassNotFoundException
ClassUtil
,
to allow for overriding of lookup functionality in environments like OSGi.ClassNotFoundException
protected Class<?> classForName(String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException
ClassNotFoundException
protected Class<?> classForName(String name) throws ClassNotFoundException
ClassNotFoundException
public JavaType constructSpecializedType(JavaType baseType, Class<?> subclass)
HashMap.class
as subclass.public JavaType constructFromCanonical(String canonical) throws IllegalArgumentException
JavaType
out of its canonical
representation (see ResolvedType.toCanonical()
).canonical
- Canonical string representation of a typeIllegalArgumentException
- If canonical representation is malformed,
or class that type represents (including its generic parameters) is
not foundpublic JavaType[] findTypeParameters(JavaType type, Class<?> expType)
type
- Sub-type (leaf type) that implements expType
public JavaType[] findTypeParameters(Class<?> clz, Class<?> expType, TypeBindings bindings)
public JavaType moreSpecificType(JavaType type1, JavaType type2)
type1
- Primary type to considertype2
- Secondary type to considerpublic JavaType constructType(Type type, TypeBindings bindings)
public JavaType constructType(com.fasterxml.jackson.core.type.TypeReference<?> typeRef)
protected JavaType _constructType(Type type, TypeBindings context)
getGenericXxx
methods
(usually for a return or argument type).public ArrayType constructArrayType(Class<?> elementType)
ArrayType
.
NOTE: type modifiers are NOT called on array type itself; but are called for element type (and other contained types)
public ArrayType constructArrayType(JavaType elementType)
ArrayType
.
NOTE: type modifiers are NOT called on array type itself; but are called for contained types.
public CollectionType constructCollectionType(Class<? extends Collection> collectionClass, Class<?> elementClass)
CollectionType
.
NOTE: type modifiers are NOT called on Collection type itself; but are called for contained types.
public CollectionType constructCollectionType(Class<? extends Collection> collectionClass, JavaType elementType)
CollectionType
.
NOTE: type modifiers are NOT called on Collection type itself; but are called for contained types.
public CollectionLikeType constructCollectionLikeType(Class<?> collectionClass, Class<?> elementClass)
CollectionLikeType
.
NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
public CollectionLikeType constructCollectionLikeType(Class<?> collectionClass, JavaType elementType)
CollectionLikeType
.
NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
public MapType constructMapType(Class<? extends Map> mapClass, JavaType keyType, JavaType valueType)
MapType
instance
NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
public MapType constructMapType(Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass)
MapType
instance
NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
public MapLikeType constructMapLikeType(Class<?> mapClass, JavaType keyType, JavaType valueType)
MapLikeType
instance
NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
public MapLikeType constructMapLikeType(Class<?> mapClass, Class<?> keyClass, Class<?> valueClass)
MapLikeType
instance
NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
@Deprecated public JavaType constructSimpleType(Class<?> rawType, JavaType[] parameterTypes)
public JavaType constructSimpleType(Class<?> rawType, Class<?> parameterTarget, JavaType[] parameterTypes)
public JavaType constructReferenceType(Class<?> rawType, JavaType refType)
public JavaType uncheckedSimpleType(Class<?> cls)
NOTE: no type modifiers are called on type either, so calling this method should only be used if caller really knows what it's doing...
public JavaType constructParametrizedType(Class<?> parametrized, Class<?> parametersFor, Class<?>... parameterClasses)
JavaType
that
represents a parameterized type. For example, to represent
type List<Integer>
, you could
call
TypeFactory.constructParametrizedType(List.class, List.class, Integer.class);
The reason for first two arguments to be separate is that parameterization may
apply to a super-type. For example, if generic type was instead to be
constructed for ArrayList<Integer>
, the usual call would be:
TypeFactory.constructParametrizedType(ArrayList.class, List.class, Integer.class);since parameterization is applied to
List
.
In most cases distinction does not matter, but there are types where it does;
one such example is parameterization of types that implement Iterator
.
NOTE: type modifiers are NOT called on constructed type itself; but are called
when resolving parameterClasses
into JavaType
.
parametrized
- Type-erased type of instance being constructedparametersFor
- class or interface for which type parameters are applied; either
parametrized
or one of its supertypesparameterClasses
- Type parameters to apply@Deprecated public JavaType constructParametricType(Class<?> parametrized, Class<?>... parameterClasses)
constructParametrizedType(java.lang.Class<?>, java.lang.Class<?>, java.lang.Class<?>...)
instead.public JavaType constructParametrizedType(Class<?> parametrized, Class<?> parametersFor, JavaType... parameterTypes)
JavaType
that
represents a parameterized type. For example, to represent
type List<Set<Integer>>
, you could
call
JavaType inner = TypeFactory.constructParametrizedType(Set.class, Set.class, Integer.class); return TypeFactory.constructParametrizedType(ArrayList.class, List.class, inner);
The reason for first two arguments to be separate is that parameterization may
apply to a super-type. For example, if generic type was instead to be
constructed for ArrayList<Integer>
, the usual call would be:
TypeFactory.constructParametrizedType(ArrayList.class, List.class, Integer.class);since parameterization is applied to
List
.
In most cases distinction does not matter, but there are types where it does;
one such example is parameterization of types that implement Iterator
.
NOTE: type modifiers are NOT called on constructed type.
parametrized
- Actual full typeparametersFor
- class or interface for which type parameters are applied; either
parametrized
or one of its supertypesparameterTypes
- Type parameters to apply@Deprecated public JavaType constructParametricType(Class<?> parametrized, JavaType... parameterTypes)
constructParametrizedType(java.lang.Class<?>, java.lang.Class<?>, java.lang.Class<?>...)
instead.public CollectionType constructRawCollectionType(Class<? extends Collection> collectionClass)
Object.class
parameterization,
and is equivalent to calling:
typeFactory.constructCollectionType(collectionClass, typeFactory.unknownType());
This method should only be used if parameterization is completely unavailable.
public CollectionLikeType constructRawCollectionLikeType(Class<?> collectionClass)
Object.class
parameterization,
and is equivalent to calling:
typeFactory.constructCollectionLikeType(collectionClass, typeFactory.unknownType());
This method should only be used if parameterization is completely unavailable.
public MapType constructRawMapType(Class<? extends Map> mapClass)
Object.class
parameterization,
and is equivalent to calling:
typeFactory.constructMapType(collectionClass, typeFactory.unknownType(), typeFactory.unknownType());
This method should only be used if parameterization is completely unavailable.
public MapLikeType constructRawMapLikeType(Class<?> mapClass)
Object.class
parameterization,
and is equivalent to calling:
typeFactory.constructMapLikeType(collectionClass, typeFactory.unknownType(), typeFactory.unknownType());
This method should only be used if parameterization is completely unavailable.
protected JavaType _fromClass(Class<?> clz, TypeBindings context)
context
- Mapping of formal parameter declarations (for generic
types) into actual typesprotected JavaType _fromParameterizedClass(Class<?> clz, List<JavaType> paramTypes)
TypeParser
when generics-aware version
is constructed.protected JavaType _fromParamType(ParameterizedType type, TypeBindings context)
protected JavaType _fromArrayType(GenericArrayType type, TypeBindings context)
protected JavaType _fromVariable(TypeVariable<?> type, TypeBindings context)
protected JavaType _fromWildcard(WildcardType type, TypeBindings context)
protected JavaType _resolveVariableViaSubTypes(HierarchicType leafType, String variableName, TypeBindings bindings)
protected JavaType _unknownType()
protected HierarchicType _findSuperTypeChain(Class<?> subtype, Class<?> supertype)
protected HierarchicType _findSuperClassChain(Type currentType, Class<?> target)
protected HierarchicType _findSuperInterfaceChain(Type currentType, Class<?> target)
protected HierarchicType _doFindSuperInterfaceChain(HierarchicType current, Class<?> target)
protected HierarchicType _hashMapSuperInterfaceChain(HierarchicType current)
protected HierarchicType _arrayListSuperInterfaceChain(HierarchicType current)
Copyright © 2014–2015 FasterXML. All rights reserved.