Class TypeFactory
- java.lang.Object
-
- org.codehaus.jackson.map.type.TypeFactory
-
public final class TypeFactory extends java.lang.ObjectClass used for creating concreteJavaTypeinstances, given various inputs.As of Jackson 1.8, usage should be done using instance configured via
ObjectMapper(and exposed throughDeserializationConfigandSerializationConfig). However, old static-singleton access methods are supported as well; however, using those may cause issues with extension modules that register "type enchancers".Typical usage pattern before Jackson 1.8 was to statically import factory methods of this class, to allow convenient instantiation of structured types, especially
CollectionandMaptypes to represent generic types. For examplemapType(String.class, Integer.class)
to representMap<String,Integer>
This is an alternative to usingTypeReferencethat would be something likenew TypeReference<Map<String,Integer>>() { }
-
-
Field Summary
Fields Modifier and Type Field Description protected HierarchicType_cachedArrayListTypeLazily constructed copy of type hierarchy fromArrayListto its supertypes.protected HierarchicType_cachedHashMapTypeLazily constructed copy of type hierarchy fromHashMapto its supertypes.protected TypeModifier[]_modifiersRegisteredTypeModifiers: objects that can change details ofJavaTypeinstances factory constructs.protected TypeParser_parserstatic TypeFactoryinstanceDeprecated.As of 1.8, should use a per-ObjectMapper instance instead of global singleton
-
Constructor Summary
Constructors Modifier Constructor Description protectedTypeFactory(TypeParser p, TypeModifier[] mods)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected HierarchicType_arrayListSuperInterfaceChain(HierarchicType current)JavaType_constructType(java.lang.reflect.Type type, TypeBindings context)Factory method that can be used if type information is passed as Java typing returned fromgetGenericXxxmethods (usually for a return or argument type).protected HierarchicType_doFindSuperInterfaceChain(HierarchicType current, java.lang.Class<?> target)protected HierarchicType_findSuperClassChain(java.lang.reflect.Type currentType, java.lang.Class<?> target)protected HierarchicType_findSuperInterfaceChain(java.lang.reflect.Type currentType, java.lang.Class<?> target)protected HierarchicType_findSuperTypeChain(java.lang.Class<?> subtype, java.lang.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(java.lang.reflect.GenericArrayType type, TypeBindings context)protected JavaType_fromClass(java.lang.Class<?> clz, TypeBindings context)protected JavaType_fromParameterizedClass(java.lang.Class<?> clz, java.util.List<JavaType> paramTypes)Method used byTypeParserwhen generics-aware version is constructed.protected JavaType_fromParamType(java.lang.reflect.ParameterizedType type, TypeBindings context)This method deals with parameterized types, that is, first class generic classes.protected JavaType_fromVariable(java.lang.reflect.TypeVariable<?> type, TypeBindings context)protected JavaType_fromWildcard(java.lang.reflect.WildcardType type, TypeBindings context)protected HierarchicType_hashMapSuperInterfaceChain(HierarchicType current)protected JavaType_resolveVariableViaSubTypes(HierarchicType leafType, java.lang.String variableName, TypeBindings bindings)protected JavaType_unknownType()static JavaTypearrayType(java.lang.Class<?> elementType)Deprecated.static JavaTypearrayType(JavaType elementType)Deprecated.static JavaTypecollectionType(java.lang.Class<? extends java.util.Collection> collectionType, java.lang.Class<?> elementType)Deprecated.static JavaTypecollectionType(java.lang.Class<? extends java.util.Collection> collectionType, JavaType elementType)Deprecated.ArrayTypeconstructArrayType(java.lang.Class<?> elementType)Method for constructing anArrayType.ArrayTypeconstructArrayType(JavaType elementType)Method for constructing anArrayType.CollectionLikeTypeconstructCollectionLikeType(java.lang.Class<?> collectionClass, java.lang.Class<?> elementClass)Method for constructing aCollectionLikeType.CollectionLikeTypeconstructCollectionLikeType(java.lang.Class<?> collectionClass, JavaType elementType)Method for constructing aCollectionLikeType.CollectionTypeconstructCollectionType(java.lang.Class<? extends java.util.Collection> collectionClass, java.lang.Class<?> elementClass)Method for constructing aCollectionType.CollectionTypeconstructCollectionType(java.lang.Class<? extends java.util.Collection> collectionClass, JavaType elementType)Method for constructing aCollectionType.JavaTypeconstructFromCanonical(java.lang.String canonical)Factory method for constructing aJavaTypeout of its canonical representation (seeJavaType.toCanonical()).MapLikeTypeconstructMapLikeType(java.lang.Class<?> mapClass, java.lang.Class<?> keyClass, java.lang.Class<?> valueClass)Method for constructing aMapLikeTypeinstanceMapLikeTypeconstructMapLikeType(java.lang.Class<?> mapClass, JavaType keyType, JavaType valueType)Method for constructing aMapLikeTypeinstanceMapTypeconstructMapType(java.lang.Class<? extends java.util.Map> mapClass, java.lang.Class<?> keyClass, java.lang.Class<?> valueClass)Method for constructing aMapTypeinstanceMapTypeconstructMapType(java.lang.Class<? extends java.util.Map> mapClass, JavaType keyType, JavaType valueType)Method for constructing aMapTypeinstanceJavaTypeconstructParametricType(java.lang.Class<?> parametrized, java.lang.Class<?>... parameterClasses)Factory method for constructingJavaTypethat represents a parameterized type.JavaTypeconstructParametricType(java.lang.Class<?> parametrized, JavaType... parameterTypes)Factory method for constructingJavaTypethat represents a parameterized type.CollectionLikeTypeconstructRawCollectionLikeType(java.lang.Class<?> collectionClass)Method that can be used to construct "raw" Collection-like type; meaning that its parameterization is unknown.CollectionTypeconstructRawCollectionType(java.lang.Class<? extends java.util.Collection> collectionClass)Method that can be used to construct "raw" Collection type; meaning that its parameterization is unknown.MapLikeTypeconstructRawMapLikeType(java.lang.Class<?> mapClass)Method that can be used to construct "raw" Map-like type; meaning that its parameterization is unknown.MapTypeconstructRawMapType(java.lang.Class<? extends java.util.Map> mapClass)Method that can be used to construct "raw" Map type; meaning that its parameterization is unknown.JavaTypeconstructSimpleType(java.lang.Class<?> rawType, JavaType[] parameterTypes)Method for constructing a type instance with specified parameterization.JavaTypeconstructSpecializedType(JavaType baseType, java.lang.Class<?> subclass)Factory method for creating a subtype of given base type, as defined by specified subclass; but retaining generic type information if any.JavaTypeconstructType(java.lang.reflect.Type type)JavaTypeconstructType(java.lang.reflect.Type type, java.lang.Class<?> context)JavaTypeconstructType(java.lang.reflect.Type type, TypeBindings bindings)JavaTypeconstructType(java.lang.reflect.Type type, JavaType context)JavaTypeconstructType(TypeReference<?> typeRef)static TypeFactorydefaultInstance()Method used to access the globally shared instance, which has no custom configuration.static JavaTypefastSimpleType(java.lang.Class<?> cls)Deprecated.static JavaType[]findParameterTypes(java.lang.Class<?> clz, java.lang.Class<?> expType)Deprecated.static JavaType[]findParameterTypes(java.lang.Class<?> clz, java.lang.Class<?> expType, TypeBindings bindings)Deprecated.static JavaType[]findParameterTypes(JavaType type, java.lang.Class<?> expType)Deprecated.JavaType[]findTypeParameters(java.lang.Class<?> clz, java.lang.Class<?> expType)JavaType[]findTypeParameters(java.lang.Class<?> clz, java.lang.Class<?> expType, TypeBindings bindings)JavaType[]findTypeParameters(JavaType type, java.lang.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.static JavaTypefromCanonical(java.lang.String canonical)static JavaTypefromClass(java.lang.Class<?> clz)Deprecated.Usetype(Type)insteadstatic JavaTypefromType(java.lang.reflect.Type type)Deprecated.Usetype(Type)insteadstatic JavaTypefromTypeReference(TypeReference<?> ref)Deprecated.Usetype(Type)insteadstatic JavaTypemapType(java.lang.Class<? extends java.util.Map> mapClass, java.lang.Class<?> keyType, java.lang.Class<?> valueType)Deprecated.static JavaTypemapType(java.lang.Class<? extends java.util.Map> mapType, JavaType keyType, JavaType valueType)Deprecated.static JavaTypeparametricType(java.lang.Class<?> parametrized, java.lang.Class<?>... parameterClasses)Deprecated.static JavaTypeparametricType(java.lang.Class<?> parametrized, JavaType... parameterTypes)Deprecated.static java.lang.Class<?>rawClass(java.lang.reflect.Type t)static JavaTypespecialize(JavaType baseType, java.lang.Class<?> subclass)Deprecated.static JavaTypetype(java.lang.reflect.Type t)Deprecated.static JavaTypetype(java.lang.reflect.Type type, java.lang.Class<?> context)Deprecated.static JavaTypetype(java.lang.reflect.Type type, TypeBindings bindings)Deprecated.static JavaTypetype(java.lang.reflect.Type type, JavaType context)Deprecated.static JavaTypetype(TypeReference<?> ref)Deprecated.JavaTypeuncheckedSimpleType(java.lang.Class<?> cls)Method that will force construction of a simple type, without trying to check for more specialized types.static JavaTypeunknownType()Method for constructing a marker type that indicates missing generic type information, which is handled same as simple type forjava.lang.Object.TypeFactorywithModifier(TypeModifier mod)
-
-
-
Field Detail
-
instance
@Deprecated public static final TypeFactory instance
Deprecated.As of 1.8, should use a per-ObjectMapper instance instead of global singletonGlobally shared singleton. Should never be accessed directly; non-core code should use per-ObjectMapper instance (via configuration objects). Core Jackson code usesdefaultInstance()for accessing it.
-
_modifiers
protected final TypeModifier[] _modifiers
RegisteredTypeModifiers: objects that can change details ofJavaTypeinstances factory constructs.- Since:
- 1.8
-
_parser
protected final TypeParser _parser
-
_cachedHashMapType
protected HierarchicType _cachedHashMapType
Lazily constructed copy of type hierarchy fromHashMapto its supertypes.- Since:
- 1.9
-
_cachedArrayListType
protected HierarchicType _cachedArrayListType
Lazily constructed copy of type hierarchy fromArrayListto its supertypes.- Since:
- 1.9
-
-
Constructor Detail
-
TypeFactory
protected TypeFactory(TypeParser p, TypeModifier[] mods)
-
-
Method Detail
-
withModifier
public TypeFactory withModifier(TypeModifier mod)
-
defaultInstance
public static TypeFactory defaultInstance()
Method used to access the globally shared instance, which has no custom configuration. Used byObjectMapperto get the default factory when constructed.- Since:
- 1.8
-
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.- Since:
- 1.8
-
rawClass
public static java.lang.Class<?> rawClass(java.lang.reflect.Type t)
-
type
@Deprecated public static JavaType type(java.lang.reflect.Type t)
Deprecated.
-
type
@Deprecated public static JavaType type(java.lang.reflect.Type type, java.lang.Class<?> context)
Deprecated.
-
type
@Deprecated public static JavaType type(java.lang.reflect.Type type, JavaType context)
Deprecated.
-
type
@Deprecated public static JavaType type(java.lang.reflect.Type type, TypeBindings bindings)
Deprecated.
-
type
@Deprecated public static JavaType type(TypeReference<?> ref)
Deprecated.
-
arrayType
@Deprecated public static JavaType arrayType(java.lang.Class<?> elementType)
Deprecated.
-
collectionType
@Deprecated public static JavaType collectionType(java.lang.Class<? extends java.util.Collection> collectionType, java.lang.Class<?> elementType)
Deprecated.
-
collectionType
@Deprecated public static JavaType collectionType(java.lang.Class<? extends java.util.Collection> collectionType, JavaType elementType)
Deprecated.
-
mapType
@Deprecated public static JavaType mapType(java.lang.Class<? extends java.util.Map> mapClass, java.lang.Class<?> keyType, java.lang.Class<?> valueType)
Deprecated.
-
mapType
@Deprecated public static JavaType mapType(java.lang.Class<? extends java.util.Map> mapType, JavaType keyType, JavaType valueType)
Deprecated.
-
parametricType
@Deprecated public static JavaType parametricType(java.lang.Class<?> parametrized, java.lang.Class<?>... parameterClasses)
Deprecated.
-
parametricType
@Deprecated public static JavaType parametricType(java.lang.Class<?> parametrized, JavaType... parameterTypes)
Deprecated.
-
fromCanonical
public static JavaType fromCanonical(java.lang.String canonical) throws java.lang.IllegalArgumentException
- Throws:
java.lang.IllegalArgumentException
-
specialize
@Deprecated public static JavaType specialize(JavaType baseType, java.lang.Class<?> subclass)
Deprecated.
-
fastSimpleType
@Deprecated public static JavaType fastSimpleType(java.lang.Class<?> cls)
Deprecated.
-
findParameterTypes
@Deprecated public static JavaType[] findParameterTypes(java.lang.Class<?> clz, java.lang.Class<?> expType)
Deprecated.
-
findParameterTypes
@Deprecated public static JavaType[] findParameterTypes(java.lang.Class<?> clz, java.lang.Class<?> expType, TypeBindings bindings)
Deprecated.
-
findParameterTypes
@Deprecated public static JavaType[] findParameterTypes(JavaType type, java.lang.Class<?> expType)
Deprecated.
-
fromClass
@Deprecated public static JavaType fromClass(java.lang.Class<?> clz)
Deprecated.Usetype(Type)insteadFactory method that can be used if only type information available is of typeClass. This means that there will not be generic type information due to type erasure, but at least it will be possible to recognize array types and non-typed container types. And for other types (primitives/wrappers, beans), this is all that is needed.
-
fromTypeReference
@Deprecated public static JavaType fromTypeReference(TypeReference<?> ref)
Deprecated.Usetype(Type)insteadFactory method that can be used if the full generic type has been passed usingTypeReference. This only needs to be done if the root type to bind to is generic; but if so, it must be done to get proper typing.
-
fromType
@Deprecated public static JavaType fromType(java.lang.reflect.Type type)
Deprecated.Usetype(Type)insteadFactory method that can be used if type information is passed as Java typing returned fromgetGenericXxxmethods (usually for a return or argument type).
-
constructSpecializedType
public JavaType constructSpecializedType(JavaType baseType, java.lang.Class<?> subclass)
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.
-
constructFromCanonical
public JavaType constructFromCanonical(java.lang.String canonical) throws java.lang.IllegalArgumentException
Factory method for constructing aJavaTypeout of its canonical representation (seeJavaType.toCanonical()).- Parameters:
canonical- Canonical string representation of a type- Throws:
java.lang.IllegalArgumentException- If canonical representation is malformed, or class that type represents (including its generic parameters) is not found- Since:
- 1.8
-
findTypeParameters
public JavaType[] findTypeParameters(JavaType type, java.lang.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- Since:
- 1.6
-
findTypeParameters
public JavaType[] findTypeParameters(java.lang.Class<?> clz, java.lang.Class<?> expType)
-
findTypeParameters
public JavaType[] findTypeParameters(java.lang.Class<?> clz, java.lang.Class<?> expType, TypeBindings bindings)
-
constructType
public JavaType constructType(java.lang.reflect.Type type)
-
constructType
public JavaType constructType(java.lang.reflect.Type type, TypeBindings bindings)
-
constructType
public JavaType constructType(TypeReference<?> typeRef)
-
constructType
public JavaType constructType(java.lang.reflect.Type type, java.lang.Class<?> context)
-
_constructType
public JavaType _constructType(java.lang.reflect.Type type, TypeBindings context)
Factory method that can be used if type information is passed as Java typing returned fromgetGenericXxxmethods (usually for a return or argument type).
-
constructArrayType
public ArrayType constructArrayType(java.lang.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(java.lang.Class<? extends java.util.Collection> collectionClass, java.lang.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(java.lang.Class<? extends java.util.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(java.lang.Class<?> collectionClass, java.lang.Class<?> elementClass)
Method for constructing aCollectionLikeType.NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
- Since:
- 1.8
-
constructCollectionLikeType
public CollectionLikeType constructCollectionLikeType(java.lang.Class<?> collectionClass, JavaType elementType)
Method for constructing aCollectionLikeType.NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
- Since:
- 1.8
-
constructMapType
public MapType constructMapType(java.lang.Class<? extends java.util.Map> mapClass, JavaType keyType, JavaType valueType)
Method for constructing aMapTypeinstanceNOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
- Since:
- 1.8
-
constructMapType
public MapType constructMapType(java.lang.Class<? extends java.util.Map> mapClass, java.lang.Class<?> keyClass, java.lang.Class<?> valueClass)
Method for constructing aMapTypeinstanceNOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
- Since:
- 1.8
-
constructMapLikeType
public MapLikeType constructMapLikeType(java.lang.Class<?> mapClass, JavaType keyType, JavaType valueType)
Method for constructing aMapLikeTypeinstanceNOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
- Since:
- 1.8
-
constructMapLikeType
public MapLikeType constructMapLikeType(java.lang.Class<?> mapClass, java.lang.Class<?> keyClass, java.lang.Class<?> valueClass)
Method for constructing aMapLikeTypeinstanceNOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
- Since:
- 1.8
-
constructSimpleType
public JavaType constructSimpleType(java.lang.Class<?> rawType, JavaType[] parameterTypes)
Method for constructing a type instance with specified parameterization.- Since:
- 1.8
-
uncheckedSimpleType
public JavaType uncheckedSimpleType(java.lang.Class<?> cls)
Method that will force construction of a simple type, without trying to check for more specialized types.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...
- Since:
- 1.8
-
constructParametricType
public JavaType constructParametricType(java.lang.Class<?> parametrized, java.lang.Class<?>... parameterClasses)
Factory method for constructingJavaTypethat represents a parameterized type. For example, to represent typeList<Set<Integer>>, you could callTypeFactory.parametricType(List.class, Integer.class);
NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
- Since:
- 1.5
-
constructParametricType
public JavaType constructParametricType(java.lang.Class<?> parametrized, JavaType... parameterTypes)
Factory method for constructingJavaTypethat represents a parameterized type. For example, to represent typeList<Set<Integer>>, you could callJavaType inner = TypeFactory.parametricType(Set.class, Integer.class); TypeFactory.parametricType(List.class, inner);
NOTE: type modifiers are NOT called on constructed type itself; but are called for contained types.
- Since:
- 1.5
-
constructRawCollectionType
public CollectionType constructRawCollectionType(java.lang.Class<? extends java.util.Collection> collectionClass)
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.
- Since:
- 1.9
-
constructRawCollectionLikeType
public CollectionLikeType constructRawCollectionLikeType(java.lang.Class<?> collectionClass)
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.
- Since:
- 1.9
-
constructRawMapType
public MapType constructRawMapType(java.lang.Class<? extends java.util.Map> mapClass)
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.
- Since:
- 1.9
-
constructRawMapLikeType
public MapLikeType constructRawMapLikeType(java.lang.Class<?> mapClass)
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.
- Since:
- 1.9
-
_fromClass
protected JavaType _fromClass(java.lang.Class<?> clz, TypeBindings context)
- Parameters:
context- Mapping of formal parameter declarations (for generic types) into actual types
-
_fromParameterizedClass
protected JavaType _fromParameterizedClass(java.lang.Class<?> clz, java.util.List<JavaType> paramTypes)
Method used byTypeParserwhen generics-aware version is constructed.
-
_fromParamType
protected JavaType _fromParamType(java.lang.reflect.ParameterizedType type, TypeBindings context)
This method deals with parameterized types, that is, first class generic classes.Since version 1.2, this resolves all parameterized types, not just Maps or Collections.
-
_fromArrayType
protected JavaType _fromArrayType(java.lang.reflect.GenericArrayType type, TypeBindings context)
-
_fromVariable
protected JavaType _fromVariable(java.lang.reflect.TypeVariable<?> type, TypeBindings context)
-
_fromWildcard
protected JavaType _fromWildcard(java.lang.reflect.WildcardType type, TypeBindings context)
-
_resolveVariableViaSubTypes
protected JavaType _resolveVariableViaSubTypes(HierarchicType leafType, java.lang.String variableName, TypeBindings bindings)
-
_unknownType
protected JavaType _unknownType()
-
_findSuperTypeChain
protected HierarchicType _findSuperTypeChain(java.lang.Class<?> subtype, java.lang.Class<?> supertype)
Helper method used to find inheritance (implements, extends) path between given types, if one exists (caller generally checks before calling this method). Returned type represents given subtype, with supertype linkage extending to supertype.
-
_findSuperClassChain
protected HierarchicType _findSuperClassChain(java.lang.reflect.Type currentType, java.lang.Class<?> target)
-
_findSuperInterfaceChain
protected HierarchicType _findSuperInterfaceChain(java.lang.reflect.Type currentType, java.lang.Class<?> target)
-
_doFindSuperInterfaceChain
protected HierarchicType _doFindSuperInterfaceChain(HierarchicType current, java.lang.Class<?> target)
-
_hashMapSuperInterfaceChain
protected HierarchicType _hashMapSuperInterfaceChain(HierarchicType current)
-
_arrayListSuperInterfaceChain
protected HierarchicType _arrayListSuperInterfaceChain(HierarchicType current)
-
-