org.springframework.core.convert
Class TypeDescriptor

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

public class TypeDescriptor
extends java.lang.Object

Context about a type to convert to.

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

Field Summary
private  java.lang.annotation.Annotation[] annotations
           
private  TypeDescriptor elementType
           
private static java.lang.annotation.Annotation[] EMPTY_ANNOTATION_ARRAY
           
private  java.lang.reflect.Field field
           
private  int fieldNestingLevel
           
private  TypeDescriptor mapKeyType
           
private  TypeDescriptor mapValueType
           
private  MethodParameter methodParameter
           
static TypeDescriptor NULL
          Constant defining a TypeDescriptor for a null value
private  java.lang.Class<?> type
           
private static java.util.Map<java.lang.Class<?>,TypeDescriptor> typeDescriptorCache
           
private static TypeDescriptor UNKNOWN
          Constant defining a TypeDescriptor for 'unknown type'
private  java.lang.Object value
           
 
Constructor Summary
private TypeDescriptor()
          Internal constructor for a NULL descriptor.
private TypeDescriptor(java.lang.Class<?> type)
          Create a new descriptor for the given type.
  TypeDescriptor(java.lang.reflect.Field field)
          Create a new type descriptor for a field.
  TypeDescriptor(java.lang.reflect.Field field, java.lang.Class<?> type)
          Create a new type descriptor for a field.
private TypeDescriptor(java.lang.reflect.Field field, int nestingLevel, java.lang.Class<?> type)
          Create a new type descriptor for a field.
  TypeDescriptor(MethodParameter methodParameter)
          Create a new type descriptor from a method or constructor parameter.
  TypeDescriptor(MethodParameter methodParameter, java.lang.Class<?> type)
          Create a new type descriptor from a method or constructor parameter.
private TypeDescriptor(java.lang.Object value)
          Create a new descriptor for the type of the given value.
 
Method Summary
 java.lang.String asString()
          A textual representation of the type descriptor (eg.
 boolean equals(java.lang.Object obj)
           
 TypeDescriptor forElementType(java.lang.Class<?> elementType)
          Create a copy of this type descriptor, preserving the context information but exposing the specified element type (e.g.
static TypeDescriptor forObject(java.lang.Object object)
          Create a new type descriptor for the class of the given object.
 java.lang.annotation.Annotation getAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
          Obtain the annotation associated with the wrapped parameter/field, if any.
 java.lang.annotation.Annotation[] getAnnotations()
          Obtain the annotations associated with the wrapped parameter/field, if any.
 java.lang.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.
 TypeDescriptor getElementTypeDescriptor(java.lang.Object element)
          Return the element type as a type descriptor.
 java.lang.reflect.Field getField()
          Return the wrapped Field, if any.
 java.lang.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.
 TypeDescriptor getMapKeyTypeDescriptor(java.lang.Object key)
          Return the map key type as a type descriptor.
 java.lang.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.
 TypeDescriptor getMapValueTypeDescriptor(java.lang.Object value)
          Return the map value type as a type descriptor.
 MethodParameter getMethodParameter()
          Return the wrapped MethodParameter, if any.
 java.lang.String getName()
          Returns the name of this type: the fully qualified class name.
 java.lang.Class<?> getObjectType()
          Determine the declared type of the wrapped parameter/field.
 java.lang.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 isMapEntryTypeKnown()
          Is this descriptor for a map where the key type and value type are known?
 boolean isPrimitive()
          Is this type a primitive type?
private  java.lang.annotation.Annotation[] resolveAnnotations()
           
private  java.lang.Class<?> resolveCollectionElementType()
           
private  java.lang.Class<?> resolveElementType()
           
private  java.lang.Class<?> resolveMapKeyType()
           
private  java.lang.Class<?> resolveMapValueType()
           
 java.lang.String toString()
           
static TypeDescriptor valueOf(java.lang.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


UNKNOWN

private static final TypeDescriptor UNKNOWN
Constant defining a TypeDescriptor for 'unknown type'


typeDescriptorCache

private static final java.util.Map<java.lang.Class<?>,TypeDescriptor> typeDescriptorCache

EMPTY_ANNOTATION_ARRAY

private static final java.lang.annotation.Annotation[] EMPTY_ANNOTATION_ARRAY

type

private java.lang.Class<?> type

methodParameter

private MethodParameter methodParameter

field

private java.lang.reflect.Field field

fieldNestingLevel

private int fieldNestingLevel

value

private java.lang.Object value

elementType

private volatile TypeDescriptor elementType

mapKeyType

private volatile TypeDescriptor mapKeyType

mapValueType

private volatile TypeDescriptor mapValueType

annotations

private volatile java.lang.annotation.Annotation[] annotations
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(java.lang.reflect.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

public TypeDescriptor(MethodParameter methodParameter,
                      java.lang.Class<?> type)
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
type - the specific type to expose (may be an array/collection element)

TypeDescriptor

public TypeDescriptor(java.lang.reflect.Field field,
                      java.lang.Class<?> type)
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
type - the specific type to expose (may be an array/collection element)

TypeDescriptor

private TypeDescriptor(java.lang.reflect.Field field,
                       int nestingLevel,
                       java.lang.Class<?> type)
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
type - the specific type to expose (may be an array/collection element)

TypeDescriptor

private TypeDescriptor()
Internal constructor for a NULL descriptor.


TypeDescriptor

private TypeDescriptor(java.lang.Object value)
Create a new descriptor for the type of the given value.

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

Parameters:
value - the value to determine the actual type from

TypeDescriptor

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

Use this constructor when a conversion point comes from a plain source type, where no additional context is available.

Parameters:
type - the actual type 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 java.lang.reflect.Field getField()
Return the wrapped Field, if any.

Note: Either MethodParameter or Field is available.

Returns:
the Field, or null if none

getType

public java.lang.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 java.lang.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 java.lang.String getName()
Returns the name of this type: the fully qualified class name.


isPrimitive

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


isArray

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


isCollection

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


getElementType

public java.lang.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.


getElementTypeDescriptor

public TypeDescriptor getElementTypeDescriptor(java.lang.Object element)
Return the element type as a type descriptor. If the element type is null (cannot be determined), the type descriptor is derived from the element argument.

Parameters:
element - the element
Returns:
the element 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 java.lang.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.


getMapKeyTypeDescriptor

public TypeDescriptor getMapKeyTypeDescriptor(java.lang.Object key)
Return the map key type as a type descriptor. If the key type is null (cannot be determined), the type descriptor is derived from the key argument.

Parameters:
key - the key
Returns:
the map key type descriptor

getMapValueType

public java.lang.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.


getMapValueTypeDescriptor

public TypeDescriptor getMapValueTypeDescriptor(java.lang.Object value)
Return the map value type as a type descriptor. If the value type is null (cannot be determined), the type descriptor is derived from the value argument.

Parameters:
value - the value
Returns:
the map value type descriptor

getAnnotations

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


getAnnotation

public java.lang.annotation.Annotation getAnnotation(java.lang.Class<? extends java.lang.annotation.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

forElementType

public TypeDescriptor forElementType(java.lang.Class<?> elementType)
Create a copy of this type descriptor, preserving the context information but exposing the specified element type (e.g. an array/collection/map element).

Parameters:
elementType - the desired type to expose
Returns:
the type descriptor

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

asString

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


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

resolveElementType

private java.lang.Class<?> resolveElementType()

resolveCollectionElementType

private java.lang.Class<?> resolveCollectionElementType()

resolveMapKeyType

private java.lang.Class<?> resolveMapKeyType()

resolveMapValueType

private java.lang.Class<?> resolveMapValueType()

resolveAnnotations

private java.lang.annotation.Annotation[] resolveAnnotations()

forObject

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

Parameters:
object - the object
Returns:
the type descriptor

valueOf

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

Parameters:
type - the class
Returns:
the type descriptor