Class TypeFactory
- java.lang.Object
-
- com.fasterxml.jackson.databind.type.TypeFactory
-
- All Implemented Interfaces:
Serializable
public class TypeFactory extends Object implements Serializable
Class used for creating concreteJavaType
instances, given various inputs.Instances of this class are accessible using
ObjectMapper
as well as many objects it constructs (likeDeserializationConfig
andSerializationConfig
)), 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);
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
clearCache()
Method that will clear up any cached type definitions that may be cached by thisTypeFactory
instance.ArrayType
constructArrayType(JavaType elementType)
Method for constructing anArrayType
.ArrayType
constructArrayType(Class<?> elementType)
Method for constructing anArrayType
.CollectionLikeType
constructCollectionLikeType(Class<?> collectionClass, JavaType elementType)
Method for constructing aCollectionLikeType
.CollectionLikeType
constructCollectionLikeType(Class<?> collectionClass, Class<?> elementClass)
Method for constructing aCollectionLikeType
.CollectionType
constructCollectionType(Class<? extends Collection> collectionClass, JavaType elementType)
Method for constructing aCollectionType
.CollectionType
constructCollectionType(Class<? extends Collection> collectionClass, Class<?> elementClass)
Method for constructing aCollectionType
.JavaType
constructFromCanonical(String canonical)
Factory method for constructing aJavaType
out of its canonical representation (seeResolvedType.toCanonical()
).JavaType
constructGeneralizedType(JavaType baseType, Class<?> superClass)
Method similar toconstructSpecializedType(com.fasterxml.jackson.databind.JavaType, java.lang.Class<?>)
, but that creates a less-specific type of given type.MapLikeType
constructMapLikeType(Class<?> mapClass, JavaType keyType, JavaType valueType)
Method for constructing aMapLikeType
instanceMapLikeType
constructMapLikeType(Class<?> mapClass, Class<?> keyClass, Class<?> valueClass)
Method for constructing aMapLikeType
instanceMapType
constructMapType(Class<? extends Map> mapClass, JavaType keyType, JavaType valueType)
Method for constructing aMapType
instanceMapType
constructMapType(Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass)
Method for constructing aMapType
instanceJavaType
constructParametricType(Class<?> rawType, JavaType... parameterTypes)
Factory method for constructingJavaType
that represents a parameterized type.JavaType
constructParametricType(Class<?> parametrized, Class<?>... parameterClasses)
Factory method for constructingJavaType
that represents a parameterized type.JavaType
constructParametrizedType(Class<?> parametrized, Class<?> parametersFor, JavaType... parameterTypes)
Deprecated.since 2.9 UseconstructParametricType(Class,JavaType...)
insteadJavaType
constructParametrizedType(Class<?> parametrized, Class<?> parametersFor, Class<?>... parameterClasses)
Deprecated.since 2.9 UseconstructParametricType(Class,Class...)
insteadCollectionLikeType
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 referredType)
Method for constructing aReferenceType
instance with given type parameter (type MUST take one and only one type parameter)JavaType
constructSimpleType(Class<?> rawType, JavaType[] parameterTypes)
Method for constructing a type instance with specified parameterization.JavaType
constructSimpleType(Class<?> rawType, Class<?> parameterTarget, JavaType[] parameterTypes)
Deprecated.Since 2.7JavaType
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
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.JavaType
constructType(TypeReference<?> typeRef)
JavaType
constructType(Type type)
JavaType
constructType(Type type, JavaType contextType)
Deprecated.Since 2.7 (accidentally removed in 2.7.0; added back in 2.7.1)JavaType
constructType(Type type, TypeBindings bindings)
JavaType
constructType(Type type, Class<?> contextClass)
Deprecated.Since 2.7 (accidentally removed in 2.7.0; added back in 2.7.1)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 fromClassUtil
, to allow for overriding of lookup functionality in environments like OSGi.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.JavaType[]
findTypeParameters(Class<?> clz, Class<?> expType)
Deprecated.Since 2.7 resolve raw type first, then find type parametersJavaType[]
findTypeParameters(Class<?> clz, Class<?> expType, TypeBindings bindings)
Deprecated.Since 2.7 resolve raw type first, then find type parametersClassLoader
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)
Deprecated.Since 2.8, to indicate users should never call this method.static JavaType
unknownType()
Method for constructing a marker type that indicates missing generic type information, which is handled same as simple type forjava.lang.Object
.TypeFactory
withCache(LRUMap<Object,JavaType> cache)
Mutant factory method that will construct newTypeFactory
with identical settings except for different cache; most likely one with bigger maximum size.TypeFactory
withClassLoader(ClassLoader classLoader)
"Mutant factory" method which will construct a new instance with specifiedClassLoader
to use byfindClass(java.lang.String)
.TypeFactory
withModifier(TypeModifier mod)
"Mutant factory" method which will construct a new instance with specifiedTypeModifier
added as the first modifier to call (in case there are multiple registered).
-
-
-
Method Detail
-
withModifier
public TypeFactory withModifier(TypeModifier mod)
"Mutant factory" method which will construct a new instance with specifiedTypeModifier
added as the first modifier to call (in case there are multiple registered).
-
withClassLoader
public TypeFactory withClassLoader(ClassLoader classLoader)
"Mutant factory" method which will construct a new instance with specifiedClassLoader
to use byfindClass(java.lang.String)
.
-
withCache
public TypeFactory withCache(LRUMap<Object,JavaType> cache)
Mutant factory method that will construct newTypeFactory
with identical settings except for different cache; most likely one with bigger maximum size.- Since:
- 2.8
-
defaultInstance
public static TypeFactory defaultInstance()
Method used to access the globally shared instance, which has no custom configuration. Used byObjectMapper
to get the default factory when constructed.
-
clearCache
public void clearCache()
Method that will clear up any cached type definitions that may be cached by thisTypeFactory
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 ofClass
instances viaJavaType
reference.- Since:
- 2.4.1
-
getClassLoader
public ClassLoader getClassLoader()
-
unknownType
public static JavaType unknownType()
Method for constructing a marker type that indicates missing generic type information, which is handled same as simple type forjava.lang.Object
.
-
rawClass
public static Class<?> rawClass(Type t)
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.
-
findClass
public Class<?> findClass(String className) throws ClassNotFoundException
Low-level lookup method moved fromClassUtil
, to allow for overriding of lookup functionality in environments like OSGi.- Throws:
ClassNotFoundException
- Since:
- 2.6
-
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.class
as 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.class
as 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
- Since:
- 2.11
-
constructGeneralizedType
public JavaType constructGeneralizedType(JavaType baseType, Class<?> superClass)
Method similar toconstructSpecializedType(com.fasterxml.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.- Parameters:
superClass
-- Since:
- 2.7
-
constructFromCanonical
public JavaType constructFromCanonical(String canonical) throws IllegalArgumentException
Factory method for constructing aJavaType
out 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
public 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. This could mean, for example, trying to figure out key and value types for Map implementations.- Parameters:
type
- Sub-type (leaf type) that implementsexpType
-
findTypeParameters
@Deprecated public JavaType[] findTypeParameters(Class<?> clz, Class<?> expType, TypeBindings bindings)
Deprecated.Since 2.7 resolve raw type first, then find type parameters
-
findTypeParameters
@Deprecated public JavaType[] findTypeParameters(Class<?> clz, Class<?> expType)
Deprecated.Since 2.7 resolve raw type first, then find type parameters
-
moreSpecificType
public 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.- Parameters:
type1
- Primary type to considertype2
- Secondary type to consider- Since:
- 2.2
-
constructType
public JavaType constructType(Type type, TypeBindings bindings)
-
constructType
public JavaType constructType(TypeReference<?> typeRef)
-
constructType
@Deprecated public JavaType constructType(Type type, Class<?> contextClass)
Deprecated.Since 2.7 (accidentally removed in 2.7.0; added back in 2.7.1)
-
constructType
@Deprecated public JavaType constructType(Type type, JavaType contextType)
Deprecated.Since 2.7 (accidentally removed in 2.7.0; added back in 2.7.1)
-
constructArrayType
public ArrayType constructArrayType(Class<?> elementType)
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
public ArrayType constructArrayType(JavaType elementType)
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 aMapType
instanceNOTE: 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 aMapType
instanceNOTE: type modifiers are NOT called on constructed type itself.
-
constructMapLikeType
public MapLikeType constructMapLikeType(Class<?> mapClass, Class<?> keyClass, Class<?> valueClass)
Method for constructing aMapLikeType
instanceNOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
-
constructMapLikeType
public MapLikeType constructMapLikeType(Class<?> mapClass, JavaType keyType, JavaType valueType)
Method for constructing aMapLikeType
instanceNOTE: type modifiers are NOT called on constructed type itself.
-
constructSimpleType
public JavaType constructSimpleType(Class<?> rawType, JavaType[] parameterTypes)
Method for constructing a type instance with specified parameterization.NOTE: type modifiers are NOT called on constructed type itself.
-
constructSimpleType
@Deprecated public JavaType constructSimpleType(Class<?> rawType, Class<?> parameterTarget, JavaType[] parameterTypes)
Deprecated.Since 2.7Method for constructing a type instance with specified parameterization.- Since:
- 2.6
-
constructReferenceType
public JavaType constructReferenceType(Class<?> rawType, JavaType referredType)
Method for constructing aReferenceType
instance with given type parameter (type MUST take one and only one type parameter)NOTE: type modifiers are NOT called on constructed type itself.
- Since:
- 2.6
-
uncheckedSimpleType
@Deprecated public JavaType uncheckedSimpleType(Class<?> cls)
Deprecated.Since 2.8, to indicate users should never call this method.Method that use by core Databind functionality, and that should NOT be called by application code outside databind package.Unchecked here not only means that no checks are made as to whether given class might be non-simple type (like
CollectionType
) but also that most of supertype information is not gathered. This means that unless called on primitive types orString
, results are probably not what you want to use.
-
constructParametricType
public JavaType constructParametricType(Class<?> parametrized, Class<?>... parameterClasses)
Factory method for constructingJavaType
that represents a parameterized type. For example, to represent typeList<Set<Integer>>
, you could callJavaType inner = TypeFactory.constructParametricType(Set.class, Set.class, Integer.class); return TypeFactory.constructParametricType(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.constructParametricType(ArrayList.class, List.class, Integer.class);
since parameterization is applied toList
. In most cases distinction does not matter, but there are types where it does; one such example is parameterization of types that implementIterator
.NOTE: type modifiers are NOT called on constructed type.
- Parameters:
parametrized
- Actual full typeparameterClasses
- Type parameters to apply- Since:
- 2.5 NOTE: was briefly deprecated for 2.6
-
constructParametricType
public JavaType constructParametricType(Class<?> rawType, JavaType... parameterTypes)
Factory method for constructingJavaType
that represents a parameterized type. For example, to represent typeList<Set<Integer>>
, you couldJavaType inner = TypeFactory.constructParametricType(Set.class, Set.class, Integer.class); return TypeFactory.constructParametricType(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.constructParametricType(ArrayList.class, List.class, Integer.class);
since parameterization is applied toList
. In most cases distinction does not matter, but there are types where it does; one such example is parameterization of types that implementIterator
.NOTE: since 2.11.2
TypeModifier
s ARE called on result (fix for [databind#2796])- Parameters:
rawType
- Actual type-erased typeparameterTypes
- Type parameters to apply- Returns:
- Fully resolved type for given base type and type parameters
-
constructParametrizedType
@Deprecated public JavaType constructParametrizedType(Class<?> parametrized, Class<?> parametersFor, JavaType... parameterTypes)
Deprecated.since 2.9 UseconstructParametricType(Class,JavaType...)
instead- Since:
- 2.5 -- but will probably deprecated in 2.7 or 2.8 (not needed with 2.7)
-
constructParametrizedType
@Deprecated public JavaType constructParametrizedType(Class<?> parametrized, Class<?> parametersFor, Class<?>... parameterClasses)
Deprecated.since 2.9 UseconstructParametricType(Class,Class...)
instead- Since:
- 2.5 -- but will probably deprecated in 2.7 or 2.8 (not needed with 2.7)
-
constructRawCollectionType
public CollectionType constructRawCollectionType(Class<? extends Collection> collectionClass)
Method that can be used to construct "raw" Collection type; meaning that its parameterization is unknown. This is similar to usingObject.class
parameterization, and is equivalent to calling:typeFactory.constructCollectionType(collectionClass, typeFactory.unknownType());
This method should only be used if parameterization is completely unavailable.
-
constructRawCollectionLikeType
public CollectionLikeType constructRawCollectionLikeType(Class<?> collectionClass)
Method that can be used to construct "raw" Collection-like type; meaning that its parameterization is unknown. This is similar to usingObject.class
parameterization, and is equivalent to calling:typeFactory.constructCollectionLikeType(collectionClass, typeFactory.unknownType());
This method should only be used if parameterization is completely unavailable.
-
constructRawMapType
public MapType constructRawMapType(Class<? extends Map> mapClass)
Method that can be used to construct "raw" Map type; meaning that its parameterization is unknown. This is similar to usingObject.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.
-
constructRawMapLikeType
public MapLikeType constructRawMapLikeType(Class<?> mapClass)
Method that can be used to construct "raw" Map-like type; meaning that its parameterization is unknown. This is similar to usingObject.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.
-
-