- All Implemented Interfaces:
Serializable,Snapshottable<TypeFactory>
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);
Note on optimizations: generic type parameters are resolved for all types, with following exceptions:
- For optimization purposes, type resolution is skipped for following commonly seen
types that do have type parameters, but ones that are rarely needed:
Enum: Self-referential type reference is simply dropped and Class is exposed as a simple, non-parameterizedSimpleTypeComparable: Type parameter is simply dropped and and interface is exposed as a simple, non-parameterizedSimpleType- Up until Jackson 2.13,
Classtype parameter was dropped; resolution was added back in Jackson 2.14.
- For
Collectionsubtypes, resolved type is ALWAYS the parameter for {link java.util.Collection} and not that of actually resolved subtype. This is usually (but not always) same parameter. - For
Mapsubtypes, resolved type is ALWAYS the parameter for {link java.util.Map} and not that of actually resolved subtype. These are usually (but not always) same parameters.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ClassLoaderClassLoader used by this factory.protected final TypeModifier[]RegisteredTypeModifiers: objects that can change details ofJavaTypeinstances factory constructs.protected final LookupCache<Object,JavaType> Since type resolution can be expensive (specifically when resolving actual generic types), we will use small cache to avoid repetitive resolution of core typesprotected static final SimpleTypeprotected static final SimpleTypeCacheComparablebecause it is both parametric (relatively costly to resolve) and mostly useless (no special handling), better handle directlyprotected static final SimpleTypeprotected static final SimpleTypeCacheEnumbecause it is parametric AND self-referential (costly to resolve) and useless in itself (no special handling).protected static final SimpleTypeprotected static final SimpleTypeCacheJsonNodebecause it is no critical path of simple tree model reading and does not have things to overrideprotected static final SimpleTypeprotected static final SimpleTypeprotected static final SimpleTypestatic final intDefault size used to construct_typeCache.protected static final TypeBindings -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedTypeFactory(LookupCache<Object, JavaType> typeCache) protectedTypeFactory(LookupCache<Object, JavaType> typeCache, TypeModifier[] mods, ClassLoader classLoader) -
Method Summary
Modifier and TypeMethodDescriptionprotected JavaType_applyModifiers(Type srcType, JavaType resolvedType) protected JavaType_constructSimple(Class<?> raw, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) Factory method to call when no specialJavaTypeis needed, no generic parameters are passed.protected Class<?>_findPrimitive(String className) protected static JavaType_findWellKnownSimple(Class<?> clz) Helper method called to see if requested, non-generic-parameterized type is one of common, "well-known" types, instances of which are pre-constructed and do not need dynamic caching.protected JavaType_fromAny(ClassStack context, Type srcType, TypeBindings bindings) Factory method that can be used if type information is passed as Java typing returned fromgetGenericXxxmethods (usually for a return or argument type).protected JavaType_fromArrayType(ClassStack context, GenericArrayType type, TypeBindings bindings) protected JavaType_fromClass(ClassStack context, Class<?> rawType, TypeBindings bindings) protected JavaType_fromParamType(ClassStack context, ParameterizedType ptype, TypeBindings parentBindings) This method deals with parameterized types, that is, first class generic classes.protected JavaType_fromVariable(ClassStack context, TypeVariable<?> var, TypeBindings bindings) protected JavaType_fromWellKnownClass(ClassStack context, Class<?> rawType, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) Helper class used to check whether exact class for which type is being constructed is one of well-known base interfaces or classes that indicates alternateJavaTypeimplementation.protected JavaType_fromWellKnownInterface(ClassStack context, Class<?> rawType, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) protected JavaType_fromWildcard(ClassStack context, WildcardType type, TypeBindings bindings) protected JavaType_newSimpleType(Class<?> raw, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) Factory method that is to create a newSimpleTypewith no checks whatsoever.protected JavaType_referenceType(Class<?> rawClass, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) protected JavaType_resolveSuperClass(ClassStack context, Class<?> rawType, TypeBindings parentBindings) protected JavaType[]_resolveSuperInterfaces(ClassStack context, Class<?> rawType, TypeBindings parentBindings) protected JavaTypeprotected Class<?>classForName(String name) protected Class<?>classForName(String name, boolean initialize, ClassLoader loader) voidMethod that will clear up any cached type definitions that may be cached by thisTypeFactoryinstance.constructArrayType(Class<?> elementType) Method for constructing anArrayType.constructArrayType(JavaType elementType) Method for constructing anArrayType.constructCollectionLikeType(Class<?> collectionClass, Class<?> elementClass) Method for constructing aCollectionLikeType.constructCollectionLikeType(Class<?> collectionClass, JavaType elementType) Method for constructing aCollectionLikeType.constructCollectionType(Class<? extends Collection> collectionClass, Class<?> elementClass) Method for constructing aCollectionType.constructCollectionType(Class<? extends Collection> collectionClass, JavaType elementType) Method for constructing aCollectionType.constructFromCanonical(String canonical) Factory method for constructing aJavaTypeout of its canonical representation (seeResolvedType.toCanonical()).constructGeneralizedType(JavaType baseType, Class<?> superClass) Method similar toconstructSpecializedType(tools.jackson.databind.JavaType, java.lang.Class<?>), but that creates a less-specific type of given type.constructMapLikeType(Class<?> mapClass, Class<?> keyClass, Class<?> valueClass) Method for constructing aMapLikeTypeinstance.constructMapLikeType(Class<?> mapClass, JavaType keyType, JavaType valueType) Method for constructing aMapLikeTypeinstanceconstructMapType(Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass) Method for constructing aMapTypeinstanceconstructMapType(Class<? extends Map> mapClass, JavaType keyType, JavaType valueType) Method for constructing aMapTypeinstanceconstructParametricType(Class<?> parametrized, Class<?>... parameterClasses) Factory method for constructingJavaTypethat represents a parameterized type.constructParametricType(Class<?> rawType, JavaType... parameterTypes) Factory method for constructingJavaTypethat represents a parameterized type.constructParametricType(Class<?> rawType, TypeBindings parameterTypes) Factory method for constructingJavaTypethat represents a parameterized type.constructRawCollectionLikeType(Class<?> collectionClass) Method that can be used to construct "raw" Collection-like type; meaning that its parameterization is unknown.constructRawCollectionType(Class<? extends Collection> collectionClass) Method that can be used to construct "raw" Collection type; meaning that its parameterization is unknown.constructRawMapLikeType(Class<?> mapClass) Method that can be used to construct "raw" Map-like type; meaning that its parameterization is unknown.constructRawMapType(Class<? extends Map> mapClass) Method that can be used to construct "raw" Map type; meaning that its parameterization is unknown.constructReferenceType(Class<?> rawType, JavaType referredType) Method for constructing aReferenceTypeinstance with given type parameter (type MUST take one and only one type parameter)constructSimpleType(Class<?> rawType, JavaType[] parameterTypes) Method for constructing a type instance with specified parameterization.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.constructSpecializedType(JavaType baseType, Class<?> subclass, boolean relaxedCompatibilityCheck) Factory method for creating a subtype of given base type, as defined by specified subclass; but retaining generic type information if any.constructType(Type type) constructType(TypeReference<?> typeRef) static TypeFactoryMethod used to construct a new default/standardTypeFactoryinstance; an instance which has no custom configuration.Class<?>Low-level lookup method moved fromClassUtil, to allow for overriding of lookup functionality in environments like OSGi.findFirstTypeParameter(JavaType type, Class<?> expType) Specialized alternative tofindTypeParameters(tools.jackson.databind.JavaType, java.lang.Class<?>)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.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<?>Static helper method that can be called to figure out type-erased call for given JDK type.resolveMemberType(Type type, TypeBindings contextBindings) Method to call when resolving types ofMembers like Fields, Methods and Constructor parameters and there is aTypeBindings(that describes binding of type parameters within context) to pass.snapshot()Need to make a copy on snapshot() to avoid accidental leakage via cache.static JavaTypeMethod for constructing a marker type that indicates missing generic type information, which is handled same as simple type forjava.lang.Object.static JavaTypeunsafeSimpleType(Class<?> cls) Method by core databind for constructing "simple"JavaTypes in cases where there is no access toTypeFactoryAND it is known that full resolution of the type is not needed (generally when type is just a placeholder).withCache(LookupCache<Object, JavaType> cache) Mutant factory method that will construct newTypeFactorywith identical settings except for different cache.withClassLoader(ClassLoader classLoader) "Mutant factory" method which will construct a new instance with specifiedClassLoaderto use byfindClass(java.lang.String).withModifier(TypeModifier mod) "Mutant factory" method which will construct a new instance with specifiedTypeModifieradded as the first modifier to call (in case there are multiple registered).
-
Field Details
-
DEFAULT_MAX_CACHE_SIZE
public static final int DEFAULT_MAX_CACHE_SIZEDefault size used to construct_typeCache.- See Also:
-
EMPTY_BINDINGS
-
CORE_TYPE_BOOL
-
CORE_TYPE_DOUBLE
-
CORE_TYPE_INT
-
CORE_TYPE_LONG
-
CORE_TYPE_STRING
-
CORE_TYPE_OBJECT
-
CORE_TYPE_COMPARABLE
CacheComparablebecause it is both parametric (relatively costly to resolve) and mostly useless (no special handling), better handle directly -
CORE_TYPE_ENUM
CacheEnumbecause it is parametric AND self-referential (costly to resolve) and useless in itself (no special handling). -
CORE_TYPE_JSON_NODE
CacheJsonNodebecause it is no critical path of simple tree model reading and does not have things to override -
_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 -
_modifiers
RegisteredTypeModifiers: objects that can change details ofJavaTypeinstances factory constructs. -
_classLoader
ClassLoader used by this factory.See [databind#624] for details.
-
-
Constructor Details
-
TypeFactory
public TypeFactory() -
TypeFactory
-
TypeFactory
protected TypeFactory(LookupCache<Object, JavaType> typeCache, TypeModifier[] mods, ClassLoader classLoader)
-
-
Method Details
-
createDefaultInstance
Method used to construct a new default/standardTypeFactoryinstance; an instance which has no custom configuration. Used byObjectMapperto get the default factory when constructed.- Since:
- 3.0
-
snapshot
Need to make a copy on snapshot() to avoid accidental leakage via cache. In theory only needed if there are modifiers, but since these are lightweight objects, let's recreate always.- Specified by:
snapshotin interfaceSnapshottable<TypeFactory>
-
withModifier
"Mutant factory" method which will construct a new instance with specifiedTypeModifieradded as the first modifier to call (in case there are multiple registered). -
withClassLoader
"Mutant factory" method which will construct a new instance with specifiedClassLoaderto use byfindClass(java.lang.String). -
withCache
Mutant factory method that will construct newTypeFactorywith identical settings except for different cache. -
clearCache
public void clearCache()Method that will clear up any cached type definitions that may be cached by thisTypeFactoryinstance. 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 ofClassinstances viaJavaTypereference. -
getClassLoader
-
unknownType
Method for constructing a marker type that indicates missing generic type information, which is handled same as simple type forjava.lang.Object. -
rawClass
Static helper method that can be called to figure out type-erased call for given JDK type. It can be called statically since type resolution process can never change actual type-erased class; thereby static default instance is used for determination. -
unsafeSimpleType
Method by core databind for constructing "simple"JavaTypes in cases where there is no access toTypeFactoryAND it is known that full resolution of the type is not needed (generally when type is just a placeholder). NOT TO BE USED by application code!- Since:
- 3.0
-
findClass
Low-level lookup method moved fromClassUtil, to allow for overriding of lookup functionality in environments like OSGi.- Throws:
ClassNotFoundException
-
classForName
protected Class<?> classForName(String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
classForName
- Throws:
ClassNotFoundException
-
_findPrimitive
-
constructSpecializedType
public JavaType constructSpecializedType(JavaType baseType, Class<?> subclass) throws IllegalArgumentException Factory method for creating a subtype of given base type, as defined by specified subclass; but retaining generic type information if any. Can be used, for example, to get equivalent of "HashMap<String,Integer>" from "Map<String,Integer>" by givingHashMap.classas subclass. Short-cut for:constructSpecializedType(baseType, subclass, class);
that is, will use "strict" compatibility checking, usually used for deserialization purposes (but often not for serialization).- Throws:
IllegalArgumentException
-
constructSpecializedType
public JavaType constructSpecializedType(JavaType baseType, Class<?> subclass, boolean relaxedCompatibilityCheck) throws IllegalArgumentException Factory method for creating a subtype of given base type, as defined by specified subclass; but retaining generic type information if any. Can be used, for example, to get equivalent of "HashMap<String,Integer>" from "Map<String,Integer>" by givingHashMap.classas subclass.- Parameters:
baseType- Declared base type with resolved type parameterssubclass- Runtime subtype to use for resolvingrelaxedCompatibilityCheck- Whether checking for type-assignment compatibility should be "relaxed" (true) or "strict" (false): typically serialization uses relaxed, deserialization strict checking.- Returns:
- Resolved sub-type
- Throws:
IllegalArgumentException
-
constructGeneralizedType
Method similar toconstructSpecializedType(tools.jackson.databind.JavaType, java.lang.Class<?>), but that creates a less-specific type of given type. Usually this is as simple as simply finding super-type with type erasure ofsuperClass, but there may be need for some additional work-arounds. -
constructFromCanonical
Factory method for constructing aJavaTypeout of its canonical representation (seeResolvedType.toCanonical()).- Parameters:
canonical- Canonical string representation of a type- Throws:
IllegalArgumentException- If canonical representation is malformed, or class that type represents (including its generic parameters) is not found
-
findTypeParameters
Method that is to figure out actual type parameters that given class binds to generic types defined by given (generic) interface or class. This could mean, for example, trying to figure out key and value types for Map implementations.- Parameters:
type- Sub-type (leaf type) that implementsexpType
-
findFirstTypeParameter
Specialized alternative tofindTypeParameters(tools.jackson.databind.JavaType, java.lang.Class<?>)- Since:
- 3.0
-
moreSpecificType
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.- Parameters:
type1- Primary type to considertype2- Secondary type to consider
-
constructType
-
constructType
-
resolveMemberType
Method to call when resolving types ofMembers like Fields, Methods and Constructor parameters and there is aTypeBindings(that describes binding of type parameters within context) to pass. This is typically used only by code in databind itself.- Parameters:
type- Type of aMemberto resolvecontextBindings- Type bindings from the context, often class in which member declared but may be sub-type of that type (to bind actual bound type parameters). Not used iftypeis of typeClass<?>.- Returns:
- Fully resolved type
-
constructArrayType
Method for constructing anArrayType.NOTE: type modifiers are NOT called on array type itself; but are called for element type (and other contained types)
-
constructArrayType
Method for constructing anArrayType.NOTE: type modifiers are NOT called on array type itself; but are called for contained types.
-
constructCollectionType
public CollectionType constructCollectionType(Class<? extends Collection> collectionClass, Class<?> elementClass) Method for constructing aCollectionType.NOTE: type modifiers are NOT called on Collection type itself; but are called for contained types.
-
constructCollectionType
public CollectionType constructCollectionType(Class<? extends Collection> collectionClass, JavaType elementType) Method for constructing aCollectionType.NOTE: type modifiers are NOT called on Collection type itself; but are called for contained types.
-
constructCollectionLikeType
public CollectionLikeType constructCollectionLikeType(Class<?> collectionClass, Class<?> elementClass) Method for constructing aCollectionLikeType.NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
-
constructCollectionLikeType
public CollectionLikeType constructCollectionLikeType(Class<?> collectionClass, JavaType elementType) Method for constructing aCollectionLikeType.NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
-
constructMapType
public MapType constructMapType(Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass) Method for constructing aMapTypeinstanceNOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
-
constructMapType
public MapType constructMapType(Class<? extends Map> mapClass, JavaType keyType, JavaType valueType) Method for constructing aMapTypeinstanceNOTE: type modifiers are NOT called on constructed type itself.
-
constructMapLikeType
Method for constructing aMapLikeTypeinstance.Do not use this method to create a true Map type -- use
constructMapType(java.lang.Class<? extends java.util.Map>, java.lang.Class<?>, java.lang.Class<?>)instead. Map-like types are only meant for supporting things that do not implement Map interface and as such cannot use standard Map handlers.NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
-
constructMapLikeType
Method for constructing aMapLikeTypeinstanceDo not use this method to create a true Map type -- use
constructMapType(java.lang.Class<? extends java.util.Map>, java.lang.Class<?>, java.lang.Class<?>)instead. Map-like types are only meant for supporting things that do not implement Map interface and as such cannot use standard Map handlers.NOTE: type modifiers are NOT called on constructed type itself.
-
constructSimpleType
Method for constructing a type instance with specified parameterization.NOTE: type modifiers are NOT called on constructed type itself.
-
constructReferenceType
Method for constructing aReferenceTypeinstance with given type parameter (type MUST take one and only one type parameter)NOTE: type modifiers are NOT called on constructed type itself.
-
constructParametricType
Factory method for constructingJavaTypethat represents a parameterized type. For example, to represent typeFoo<Bar, Baz>, you could callreturn typeFactory.constructParametricType(Foo.class, Bar.class, Baz.class);
- Parameters:
parametrized- Type-erased type to parameterizeparameterClasses- Type parameters to apply
-
constructParametricType
Factory method for constructingJavaTypethat represents a parameterized type. For example, to represent typeList<Set<Integer>>, you couldJavaType inner = typeFactory.constructParametricType(Set.class, Integer.class); return typeFactory.constructParametricType(List.class, inner);
- Parameters:
rawType- Actual type-erased typeparameterTypes- Type parameters to apply- Returns:
- Fully resolved type for given base type and type parameters
-
constructParametricType
Factory method for constructingJavaTypethat represents a parameterized type. The type's parameters are specified as an instance ofTypeBindings. This is useful if you already have the type's parameters such as those found onJavaType. For example, you could callreturn typeFactory.constructParametricType(ArrayList.class, javaType.getBindings());
This effectively applies the parameterized types from oneJavaTypeto another class.- Parameters:
rawType- Actual type-erased typeparameterTypes- Type bindings for the raw type- Since:
- 3.0
-
constructRawCollectionType
Method that can be used to construct "raw" Collection type; meaning that its parameterization is unknown. This is similar to usingObject.classparameterization, and is equivalent to calling:typeFactory.constructCollectionType(collectionClass, typeFactory.unknownType());
This method should only be used if parameterization is completely unavailable.
-
constructRawCollectionLikeType
Method that can be used to construct "raw" Collection-like type; meaning that its parameterization is unknown. This is similar to usingObject.classparameterization, and is equivalent to calling:typeFactory.constructCollectionLikeType(collectionClass, typeFactory.unknownType());
This method should only be used if parameterization is completely unavailable.
-
constructRawMapType
Method that can be used to construct "raw" Map type; meaning that its parameterization is unknown. This is similar to usingObject.classparameterization, and is equivalent to calling:typeFactory.constructMapType(collectionClass, typeFactory.unknownType(), typeFactory.unknownType());
This method should only be used if parameterization is completely unavailable.
-
constructRawMapLikeType
Method that can be used to construct "raw" Map-like type; meaning that its parameterization is unknown. This is similar to usingObject.classparameterization, and is equivalent to calling:typeFactory.constructMapLikeType(collectionClass, typeFactory.unknownType(), typeFactory.unknownType());
This method should only be used if parameterization is completely unavailable.
-
_referenceType
protected JavaType _referenceType(Class<?> rawClass, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) -
_constructSimple
protected JavaType _constructSimple(Class<?> raw, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) Factory method to call when no specialJavaTypeis needed, no generic parameters are passed. Default implementation may check pre-constructed values for "well-known" types, but if none found will simply call_newSimpleType(java.lang.Class<?>, tools.jackson.databind.type.TypeBindings, tools.jackson.databind.JavaType, tools.jackson.databind.JavaType[]) -
_newSimpleType
protected JavaType _newSimpleType(Class<?> raw, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) Factory method that is to create a newSimpleTypewith no checks whatsoever. Default implementation calls the single argument constructor ofSimpleType. -
_unknownType
-
_findWellKnownSimple
Helper method called to see if requested, non-generic-parameterized type is one of common, "well-known" types, instances of which are pre-constructed and do not need dynamic caching. -
_fromAny
Factory method that can be used if type information is passed as Java typing returned fromgetGenericXxxmethods (usually for a return or argument type). -
_applyModifiers
-
_fromClass
- Parameters:
bindings- Mapping of formal parameter declarations (for generic types) into actual types
-
_resolveSuperClass
protected JavaType _resolveSuperClass(ClassStack context, Class<?> rawType, TypeBindings parentBindings) -
_resolveSuperInterfaces
protected JavaType[] _resolveSuperInterfaces(ClassStack context, Class<?> rawType, TypeBindings parentBindings) -
_fromWellKnownClass
protected JavaType _fromWellKnownClass(ClassStack context, Class<?> rawType, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) Helper class used to check whether exact class for which type is being constructed is one of well-known base interfaces or classes that indicates alternateJavaTypeimplementation. -
_fromWellKnownInterface
protected JavaType _fromWellKnownInterface(ClassStack context, Class<?> rawType, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) -
_fromParamType
protected JavaType _fromParamType(ClassStack context, ParameterizedType ptype, TypeBindings parentBindings) This method deals with parameterized types, that is, first class generic classes. -
_fromArrayType
-
_fromVariable
-
_fromWildcard
-