org.codehaus.jackson.map.type
Class TypeFactory

java.lang.Object
  extended by org.codehaus.jackson.map.type.TypeFactory

public class TypeFactory
extends Object

Class used for constracting concrete JavaType instances, given various inputs.

Typical usage patterns is to statically import factory methods of this class, to allow convenient instantiation of structured types, especially Collection and Map types to represent generic types. For example

 mapType(String.class, Integer.class)
to represent
  Map<String,Integer>
This is an alternative to using TypeReference that would be something like
  new TypeReference<Map<String,Integer>>() { }


Field Summary
protected  TypeParser _parser
           
static TypeFactory instance
           
 
Method Summary
protected static SimpleType _constructSimple(Class<?> rawType, JavaType[] parameterTypes)
           
protected static HierarchicType _findSuperClassChain(Type currentType, Class<?> target)
           
protected static HierarchicType _findSuperInterfaceChain(Type currentType, Class<?> target)
           
protected static 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.
 JavaType _fromType(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  JavaType _fromVariable(TypeVariable<?> type, TypeBindings context)
           
protected  JavaType _fromWildcard(WildcardType type, TypeBindings context)
           
protected static JavaType _resolveVariableViaSubTypes(HierarchicType leafType, String variableName, TypeBindings bindings)
           
protected  JavaType _unknownType()
           
static JavaType arrayType(Class<?> elementType)
          Convenience factory method for constructing JavaType that represent array that contains elements of specified type.
static JavaType arrayType(JavaType elementType)
          Convenience factory method for constructing JavaType that represent array that contains elements of specified type.
static JavaType collectionType(Class<? extends Collection> collectionType, Class<?> elementType)
          Convenience factory method for constructing JavaType that represent Collection of specified type and contains elements of specified type
static JavaType collectionType(Class<? extends Collection> collectionType, JavaType elementType)
          Convenience factory method for constructing JavaType that represent Collection of specified type and contains elements of specified type
static JavaType fastSimpleType(Class<?> cls)
          Method that can be used if it is known for sure that given type is not a structured type (array, Map, Collection).
static JavaType[] findParameterTypes(Class<?> clz, 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 JavaType[] findParameterTypes(Class<?> clz, Class<?> expType, TypeBindings bindings)
           
static JavaType[] findParameterTypes(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.
static JavaType fromCanonical(String canonical)
          Factory method for constructing a JavaType out of its canonical representation (see JavaType.toCanonical()).
static JavaType fromClass(Class<?> clz)
          Deprecated. Use type(Type) instead
static JavaType fromType(Type type)
          Deprecated. Use type(Type) instead
static JavaType fromTypeReference(TypeReference<?> ref)
          Deprecated. Use type(Type) instead
static JavaType mapType(Class<? extends Map> mapType, Class<?> keyType, Class<?> valueType)
          Convenience factory method for constructing JavaType that represent Map of specified type and contains elements of specified type
static JavaType mapType(Class<? extends Map> mapType, JavaType keyType, JavaType valueType)
          Convenience factory method for constructing JavaType that represent Map of specified type and contains elements of specified type
static JavaType parametricType(Class<?> parametrized, Class<?>... parameterClasses)
          Factory method for constructing JavaType that represents a parameterized type.
static JavaType parametricType(Class<?> parametrized, JavaType... parameterTypes)
          Factory method for constructing JavaType that represents a parameterized type.
static JavaType specialize(JavaType baseType, Class<?> subclass)
          Method that tries to create specialized type given base type, and a sub-class thereof (which is assumed to use same parametrization as supertype).
static JavaType type(Type t)
          Factory method for constructing JavaType from given "raw" type; which may be anything from simple Class to full generic type.
static JavaType type(Type type, Class<?> context)
          Factory method that can use given context to resolve named generic types.
static JavaType type(Type type, JavaType context)
           
static JavaType type(TypeReference<?> ref)
          Factory method that can be used if the full generic type has been passed using TypeReference.
static JavaType type(Type type, TypeBindings bindings)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instance

public static final TypeFactory instance

_parser

protected final TypeParser _parser
Method Detail

type

public static JavaType type(Type t)
Factory method for constructing JavaType from given "raw" type; which may be anything from simple Class to full generic type.

Since:
1.3

type

public static JavaType type(Type type,
                            Class<?> context)
Factory method that can use given context to resolve named generic types.

Parameters:
context - Class used for resolving generic types; for example, for bean properties the actual bean class (not necessarily class that contains method or field, may be a sub-class thereof)

type

public static JavaType type(Type type,
                            JavaType context)
Since:
1.7.0

type

public static JavaType type(Type type,
                            TypeBindings bindings)

type

public static JavaType type(TypeReference<?> ref)
Factory method that can be used if the full generic type has been passed using TypeReference. 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.


arrayType

public static JavaType arrayType(Class<?> elementType)
Convenience factory method for constructing JavaType that represent array that contains elements of specified type.

Since:
1.3

arrayType

public static JavaType arrayType(JavaType elementType)
Convenience factory method for constructing JavaType that represent array that contains elements of specified type.

Since:
1.3

collectionType

public static JavaType collectionType(Class<? extends Collection> collectionType,
                                      Class<?> elementType)
Convenience factory method for constructing JavaType that represent Collection of specified type and contains elements of specified type

Since:
1.3

collectionType

public static JavaType collectionType(Class<? extends Collection> collectionType,
                                      JavaType elementType)
Convenience factory method for constructing JavaType that represent Collection of specified type and contains elements of specified type

Since:
1.3

mapType

public static JavaType mapType(Class<? extends Map> mapType,
                               Class<?> keyType,
                               Class<?> valueType)
Convenience factory method for constructing JavaType that represent Map of specified type and contains elements of specified type

Since:
1.3

mapType

public static JavaType mapType(Class<? extends Map> mapType,
                               JavaType keyType,
                               JavaType valueType)
Convenience factory method for constructing JavaType that represent Map of specified type and contains elements of specified type

Since:
1.3

parametricType

public static JavaType parametricType(Class<?> parametrized,
                                      Class<?>... parameterClasses)
Factory method for constructing JavaType that represents a parameterized type. For example, to represent type Iterator<String>, you could call
  TypeFactory.parametricType(Iterator.class, String.class)

Since:
1.5

parametricType

public static JavaType parametricType(Class<?> parametrized,
                                      JavaType... parameterTypes)
Factory method for constructing JavaType that represents a parameterized type. For example, to represent type List<Set<Integer>>, you could call
  JavaType inner = TypeFactory.parametricType(Set.class, Integer.class);
  TypeFactory.parametricType(List.class, inner);

Since:
1.5

fromCanonical

public static JavaType fromCanonical(String canonical)
                              throws IllegalArgumentException
Factory method for constructing a JavaType out of its canonical representation (see JavaType.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
Since:
1.5

specialize

public static JavaType specialize(JavaType baseType,
                                  Class<?> subclass)
Method that tries to create specialized type given base type, and a sub-class thereof (which is assumed to use same parametrization as supertype). Similar to calling JavaType.narrowBy(Class), but can change underlying JavaType (from simple to Map, for example), unliked narrowBy which assumes same logical type.


fastSimpleType

public static JavaType fastSimpleType(Class<?> cls)
Method that can be used if it is known for sure that given type is not a structured type (array, Map, Collection). NOTE: use of this method is discouraged due to its potential non-safety; in most cases you should just use basic type(Type) instead.

Since:
1.6

findParameterTypes

public static JavaType[] findParameterTypes(Class<?> clz,
                                            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.

Since:
1.6

findParameterTypes

public static JavaType[] findParameterTypes(Class<?> clz,
                                            Class<?> expType,
                                            TypeBindings bindings)

findParameterTypes

public static JavaType[] findParameterTypes(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 implements expType
Since:
1.6

fromClass

@Deprecated
public static JavaType fromClass(Class<?> clz)
Deprecated. Use type(Type) instead

Factory method that can be used if only type information available is of type Class. 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. Use type(Type) instead

Factory method that can be used if the full generic type has been passed using TypeReference. 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(Type type)
Deprecated. Use type(Type) instead

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).


_fromClass

protected JavaType _fromClass(Class<?> clz,
                              TypeBindings context)
Parameters:
context - Mapping of formal parameter declarations (for generic types) into actual types

_fromParameterizedClass

protected JavaType _fromParameterizedClass(Class<?> clz,
                                           List<JavaType> paramTypes)
Method used by TypeParser when generics-aware version is constructed.


_fromType

public JavaType _fromType(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).


_fromParamType

protected JavaType _fromParamType(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.


_constructSimple

protected static SimpleType _constructSimple(Class<?> rawType,
                                             JavaType[] parameterTypes)

_fromArrayType

protected JavaType _fromArrayType(GenericArrayType type,
                                  TypeBindings context)

_fromVariable

protected JavaType _fromVariable(TypeVariable<?> type,
                                 TypeBindings context)

_fromWildcard

protected JavaType _fromWildcard(WildcardType type,
                                 TypeBindings context)

_resolveVariableViaSubTypes

protected static JavaType _resolveVariableViaSubTypes(HierarchicType leafType,
                                                      String variableName,
                                                      TypeBindings bindings)

_unknownType

protected JavaType _unknownType()

_findSuperTypeChain

protected static 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). Returned type represents given subtype, with supertype linkage extending to supertype.


_findSuperClassChain

protected static HierarchicType _findSuperClassChain(Type currentType,
                                                     Class<?> target)

_findSuperInterfaceChain

protected static HierarchicType _findSuperInterfaceChain(Type currentType,
                                                         Class<?> target)