org.springframework.core.convert
Class TypeDescriptor

java.lang.Object
  extended by org.springframework.core.convert.TypeDescriptor

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 an 'unknown' TypeDescriptor.
 
Constructor Summary
TypeDescriptor(Class<?> type)
          Create a new descriptor for the given type.
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
 String asString()
           
static TypeDescriptor collection(Class<?> type, TypeDescriptor elementType)
           
static TypeDescriptor forObject(Object object)
          Creates a new type descriptor for the class of the given object.
 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.
 Field getField()
          Return the wrapped Field, if any.
 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()
          Return the wrapped MethodParameter, if any.
 String getName()
          Returns the name of this type; the fully qualified classname.
 Class<?> getObjectType()
          Determine the declared type of the wrapped parameter/field.
 Class<?> getType()
          Determine the declared (non-generic) type of the wrapped parameter/field.
 boolean isAbstractClass()
          Returns true if this type is an abstract class.
 boolean isArray()
          Is this type an array type?
 boolean isAssignableTo(TypeDescriptor targetType)
          Returns true if an object this type can be assigned to a rereference of given targetType.
 boolean isAssignableValue(Object obj)
          Is the obj an instance of this type?
 boolean isCollection()
          Is this type a Collection type?
 boolean isMap()
          Is this type a Map type?
 boolean isMapEntryTypeKnown()
          Is this descriptor for a map where the key type and value type are known?
 String toString()
           
 boolean typeEquals(Class<?> type)
          Does the underyling declared type equal the type provided?
static TypeDescriptor valueOf(Class<?> type)
          Creates a new type descriptor for the given class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NULL

public static final TypeDescriptor NULL
Constant defining an 'unknown' TypeDescriptor.

Constructor Detail

TypeDescriptor

public TypeDescriptor(Class<?> type)
Create a new descriptor for the given type.

Use this constructor when a conversion point comes from a source such as a Map or Collection, where no additional context is available.

Parameters:
type - the actual type to wrap

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
Method Detail

getMethodParameter

public MethodParameter getMethodParameter()
Return the wrapped MethodParameter, if any.

Note: Either MethodParameter or Field is available.

Returns:
the MethodParameter, or null if none

getField

public Field getField()
Return the wrapped Field, if any.

Note: Either MethodParameter or Field is available.

Returns:
the Field, or null if none

getType

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

Returns:
the declared type

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.


typeEquals

public boolean typeEquals(Class<?> type)
Does the underyling declared type equal the type provided?

Parameters:
type - the type to test against

getName

public String getName()
Returns the name of this type; the fully qualified classname.


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?


isMapEntryTypeKnown

public boolean isMapEntryTypeKnown()
Is this descriptor for a map where the key type and value type are known?


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

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

getMapKeyTypeDescriptor

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


getMapValueTypeDescriptor

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


getAnnotations

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


isAbstractClass

public boolean isAbstractClass()
Returns true if this type is an abstract class.


isAssignableValue

public boolean isAssignableValue(Object obj)
Is the obj an instance of this type?


isAssignableTo

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

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

asString

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

valueOf

public static TypeDescriptor valueOf(Class<?> type)
Creates a new type descriptor for the given class.

Parameters:
type - the class
Returns:
the type descriptor

forObject

public static TypeDescriptor forObject(Object object)
Creates a new type descriptor for the class of the given object.

Parameters:
object - the object
Returns:
the type descriptor

toString

public String toString()
Overrides:
toString in class Object

collection

public static TypeDescriptor collection(Class<?> type,
                                        TypeDescriptor elementType)