org.springframework.core.convert
Class TypeDescriptor

java.lang.Object
  extended by org.springframework.core.convert.TypeDescriptor
Direct Known Subclasses:
PropertyTypeDescriptor

public class TypeDescriptor
extends Object

Context about a type to convert to.

Since:
3.0
Author:
Keith Donald, Andy Clement, Juergen Hoeller

Field Summary
static TypeDescriptor NULL
          Constant defining a TypeDescriptor for a null value
 
Constructor Summary
protected TypeDescriptor(Class<?> nestedType, MethodParameter methodParameter)
           
  TypeDescriptor(Field field)
          Create a new type descriptor for a field.
  TypeDescriptor(MethodParameter methodParameter)
          Create a new type descriptor from a method or constructor parameter.
 
Method Summary
 TypeDescriptor applyIndexedObject(Object object)
          Create a copy of this nested type descriptor and apply the specific type information from the indexed object.
 String asString()
          A textual representation of the type descriptor (eg.
 boolean equals(Object obj)
           
static TypeDescriptor forNestedType(Class<?> nestedType, MethodParameter methodParameter)
          Create a new type descriptor for a nested type declared on an array, collection, or map-based method parameter.
static TypeDescriptor forNestedType(MethodParameter methodParameter)
          Create a new type descriptor for a nested type declared on an array, collection, or map-based method parameter.
static TypeDescriptor forObject(Object object)
          Create a new type descriptor for an object.
 Annotation getAnnotation(Class<? extends Annotation> annotationType)
          Obtain the annotation associated with the wrapped parameter/field, if any.
 Annotation[] getAnnotations()
          Obtain the annotations associated with the wrapped parameter/field, if any.
 Class<?> getElementType()
          If this type is an array type or Collection type, returns the underlying element type.
 TypeDescriptor getElementTypeDescriptor()
          Return the element type as a type descriptor.
 Class<?> getMapKeyType()
          Determine the generic key type of the wrapped Map parameter/field, if any.
 TypeDescriptor getMapKeyTypeDescriptor()
          Returns map key type as a type descriptor.
 Class<?> getMapValueType()
          Determine the generic value type of the wrapped Map parameter/field, if any.
 TypeDescriptor getMapValueTypeDescriptor()
          Returns map value type as a type descriptor.
 MethodParameter getMethodParameter()
          Exposes the underlying MethodParameter providing context for this TypeDescriptor.
 String getName()
          Returns the name of this type: the fully qualified class name.
 Class<?> getObjectType()
          Determine the declared type of the wrapped parameter/field.
 Class<?> getType()
          Determine the declared (non-generic) type of the wrapped parameter/field.
 int hashCode()
           
 boolean isArray()
          Is this type an array type?
 boolean isAssignableTo(TypeDescriptor targetType)
          Returns true if an object of this type can be assigned to a reference of given targetType.
 boolean isCollection()
          Is this type a Collection type?
 boolean isMap()
          Is this type a Map type?
 boolean isPrimitive()
          Is this type a primitive type?
protected  TypeDescriptor newNestedTypeDescriptor(Class<?> nestedType, MethodParameter nested)
           
protected  Annotation[] resolveAnnotations()
           
protected static Class<?> resolveNestedType(MethodParameter methodParameter)
           
 String toString()
           
static TypeDescriptor valueOf(Class<?> type)
          Create a new type descriptor for the given class.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NULL

public static final TypeDescriptor NULL
Constant defining a TypeDescriptor for a null value

Constructor Detail

TypeDescriptor

public TypeDescriptor(MethodParameter methodParameter)
Create a new type descriptor from a method or constructor parameter. Use this constructor when a target conversion point originates from a method parameter, such as a setter method argument.

Parameters:
methodParameter - the MethodParameter to wrap

TypeDescriptor

public TypeDescriptor(Field field)
Create a new type descriptor for a field. Use this constructor when a target conversion point originates from a field.

Parameters:
field - the field to wrap

TypeDescriptor

protected TypeDescriptor(Class<?> nestedType,
                         MethodParameter methodParameter)
Method Detail

valueOf

public static TypeDescriptor valueOf(Class<?> type)
Create a new type descriptor for the given class. Use this to instruct the conversion system to convert to an object to a specific target type, when no type location such as a method parameter or field is available to provide additional conversion context. Generally prefer use of forObject(Object) for constructing source type descriptors for source objects.

Parameters:
type - the class
Returns:
the type descriptor

forObject

public static TypeDescriptor forObject(Object object)
Create a new type descriptor for an object. Use this factory method to introspect a source object's type before asking the conversion system to convert it to some another type. Builds in population of nested type descriptors for collection and map objects through object introspection. If the object is null, returns NULL. If the object is not a collection, simply calls valueOf(Class). If the object is a collection, this factory method will derive the element type(s) by introspecting the collection.

Parameters:
object - the source object
Returns:
the type descriptor
See Also:
ConversionService.convert(Object, Class), CollectionUtils.findCommonElementType(Collection)

forNestedType

public static TypeDescriptor forNestedType(MethodParameter methodParameter)
Create a new type descriptor for a nested type declared on an array, collection, or map-based method parameter. Use this factory method when you've resolved a nested source object such as a collection element or map value and wish to have it converted.

Parameters:
methodParameter - the method parameter declaring the collection or map
Returns:
the nested type descriptor

forNestedType

public static TypeDescriptor forNestedType(Class<?> nestedType,
                                           MethodParameter methodParameter)
Create a new type descriptor for a nested type declared on an array, collection, or map-based method parameter. Use this factory method when you've resolved a nested source object such as a collection element or map value and wish to have it converted.

Parameters:
nestedType - the nested type
methodParameter - the method parameter declaring the collection or map
Returns:
the nested type descriptor

getType

public Class<?> getType()
Determine the declared (non-generic) type of the wrapped parameter/field.

Returns:
the declared type, or null if this is NULL

getObjectType

public Class<?> getObjectType()
Determine the declared type of the wrapped parameter/field. Returns the Object wrapper type if the underlying type is a primitive.


getName

public String getName()
Returns the name of this type: the fully qualified class name.


isPrimitive

public boolean isPrimitive()
Is this type a primitive type?


getAnnotations

public Annotation[] getAnnotations()
Obtain the annotations associated with the wrapped parameter/field, if any.


getAnnotation

public Annotation getAnnotation(Class<? extends Annotation> annotationType)
Obtain the annotation associated with the wrapped parameter/field, if any.


isAssignableTo

public boolean isAssignableTo(TypeDescriptor targetType)
Returns true if an object of this type can be assigned to a reference of given targetType.

Parameters:
targetType - the target type
Returns:
true if this type is assignable to the target

asString

public String asString()
A textual representation of the type descriptor (eg. Map) for use in messages.


isArray

public boolean isArray()
Is this type an array type?


isCollection

public boolean isCollection()
Is this type a Collection type?


getElementType

public Class<?> getElementType()
If this type is an array type or Collection type, returns the underlying element type. Returns null if the type is neither an array or collection.


getElementTypeDescriptor

public TypeDescriptor getElementTypeDescriptor()
Return the element type as a type descriptor.


isMap

public boolean isMap()
Is this type a Map type?


getMapKeyType

public Class<?> getMapKeyType()
Determine the generic key type of the wrapped Map parameter/field, if any.

Returns:
the generic type, or null if none

getMapKeyTypeDescriptor

public TypeDescriptor getMapKeyTypeDescriptor()
Returns map key type as a type descriptor.


getMapValueType

public Class<?> getMapValueType()
Determine the generic value type of the wrapped Map parameter/field, if any.

Returns:
the generic type, or null if none

getMapValueTypeDescriptor

public TypeDescriptor getMapValueTypeDescriptor()
Returns map value type as a type descriptor.


getMethodParameter

public MethodParameter getMethodParameter()
Exposes the underlying MethodParameter providing context for this TypeDescriptor. Used to support legacy code scenarios where callers are already using the MethodParameter API (BeanWrapper). In general, favor use of the TypeDescriptor API over the MethodParameter API as it is independent of type context location. May be null if no MethodParameter was provided when this TypeDescriptor was constructed.


applyIndexedObject

public TypeDescriptor applyIndexedObject(Object object)
Create a copy of this nested type descriptor and apply the specific type information from the indexed object. Used to support collection and map indexing scenarios, where the indexer has a reference to the indexed type descriptor but needs to ensure its type actually represents the indexed object type. This is necessary to support type conversion during index object binding operations.


equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

resolveAnnotations

protected Annotation[] resolveAnnotations()

newNestedTypeDescriptor

protected TypeDescriptor newNestedTypeDescriptor(Class<?> nestedType,
                                                 MethodParameter nested)

resolveNestedType

protected static Class<?> resolveNestedType(MethodParameter methodParameter)