org.springframework.core.convert.support
Class GenericConversionService

java.lang.Object
  extended by org.springframework.core.convert.support.GenericConversionService
All Implemented Interfaces:
ConversionService, ConverterRegistry, ConfigurableConversionService
Direct Known Subclasses:
DefaultConversionService, FormattingConversionService

public class GenericConversionService
extends java.lang.Object
implements ConfigurableConversionService

Base ConversionService implementation suitable for use in most environments. Indirectly implements ConverterRegistry as registration API through the ConfigurableConversionService interface.

Since:
3.0
Author:
Keith Donald, Juergen Hoeller, Chris Beams

Nested Class Summary
private  class GenericConversionService.ConverterAdapter
           
private static class GenericConversionService.ConverterCacheKey
           
private  class GenericConversionService.ConverterFactoryAdapter
           
private static class GenericConversionService.MatchableConverters
           
 
Field Summary
private  java.util.Map<GenericConversionService.ConverterCacheKey,GenericConverter> converterCache
           
private  java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters>> converters
           
private static GenericConverter NO_MATCH
           
private static GenericConverter NO_OP_CONVERTER
           
 
Constructor Summary
GenericConversionService()
           
 
Method Summary
 void addConverter(java.lang.Class<?> sourceType, java.lang.Class<?> targetType, Converter<?,?> converter)
          Add a plain converter to this registry.
 void addConverter(Converter<?,?> converter)
          Add a plain converter to this registry.
 void addConverter(GenericConverter converter)
          Add a generic converter to this registry.
 void addConverterFactory(ConverterFactory<?,?> converterFactory)
          Add a ranged converter factory to this registry.
private  void addInterfaceHierarchy(java.lang.Class<?> interfaceType, java.util.Set<java.lang.Class<?>> interfaces)
           
private  void assertNotPrimitiveTargetType(TypeDescriptor sourceType, TypeDescriptor targetType)
           
 boolean canConvert(java.lang.Class<?> sourceType, java.lang.Class<?> targetType)
          Returns true if objects of sourceType can be converted to targetType.
 boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
          Returns true if objects of sourceType can be converted to the targetType.
<T> T
convert(java.lang.Object source, java.lang.Class<T> targetType)
          Convert the source to targetType.
 java.lang.Object convert(java.lang.Object source, TypeDescriptor targetType)
          Convenience operation for converting a source object to the specified targetType, where the targetType is a descriptor that provides additional conversion context.
 java.lang.Object convert(java.lang.Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
          Convert the source to targetType.
protected  java.lang.Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType)
          Template method to convert a null source.
private  GenericConverter findConverterForClassPair(TypeDescriptor sourceType, TypeDescriptor targetType)
           
protected  GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType)
          Hook method to lookup the converter for a given sourceType/targetType pair.
protected  GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType)
          Return the default converter if no converter is found for the given sourceType/targetType pair.
private  GenericConversionService.MatchableConverters getMatchableConverters(java.lang.Class<?> sourceType, java.lang.Class<?> targetType)
           
private  GenericConverter getMatchingConverterForTarget(TypeDescriptor sourceType, TypeDescriptor targetType, java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> converters)
           
private  GenericConverter.ConvertiblePair getRequiredTypeInfo(java.lang.Object converter, java.lang.Class<?> genericIfc)
           
private  java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> getSourceConverterMap(java.lang.Class<?> sourceType)
           
private  java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> getTargetConvertersForSource(java.lang.Class<?> sourceType)
           
private  java.lang.Object handleConverterNotFound(java.lang.Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
           
private  java.lang.Object handleResult(TypeDescriptor sourceType, TypeDescriptor targetType, java.lang.Object result)
           
private  void invalidateCache()
           
private  GenericConverter matchConverter(GenericConversionService.MatchableConverters matchable, TypeDescriptor sourceFieldType, TypeDescriptor targetFieldType)
           
 void removeConvertible(java.lang.Class<?> sourceType, java.lang.Class<?> targetType)
          Remove any converters from sourceType to targetType.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NO_OP_CONVERTER

private static final GenericConverter NO_OP_CONVERTER

NO_MATCH

private static final GenericConverter NO_MATCH

converters

private final java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters>> converters

converterCache

private final java.util.Map<GenericConversionService.ConverterCacheKey,GenericConverter> converterCache
Constructor Detail

GenericConversionService

public GenericConversionService()
Method Detail

addConverter

public void addConverter(Converter<?,?> converter)
Description copied from interface: ConverterRegistry
Add a plain converter to this registry. The convertible sourceType/targetType pair is derived from the Converter's parameterized types.

Specified by:
addConverter in interface ConverterRegistry

addConverter

public void addConverter(java.lang.Class<?> sourceType,
                         java.lang.Class<?> targetType,
                         Converter<?,?> converter)
Description copied from interface: ConverterRegistry
Add a plain converter to this registry. The convertible sourceType/targetType pair is specified explicitly. Allows for a Converter to be reused for multiple distinct pairs without having to create a Converter class for each pair.

Specified by:
addConverter in interface ConverterRegistry

addConverter

public void addConverter(GenericConverter converter)
Description copied from interface: ConverterRegistry
Add a generic converter to this registry.

Specified by:
addConverter in interface ConverterRegistry

addConverterFactory

public void addConverterFactory(ConverterFactory<?,?> converterFactory)
Description copied from interface: ConverterRegistry
Add a ranged converter factory to this registry. The convertible sourceType/rangeType pair is derived from the ConverterFactory's parameterized types.

Specified by:
addConverterFactory in interface ConverterRegistry

removeConvertible

public void removeConvertible(java.lang.Class<?> sourceType,
                              java.lang.Class<?> targetType)
Description copied from interface: ConverterRegistry
Remove any converters from sourceType to targetType.

Specified by:
removeConvertible in interface ConverterRegistry
Parameters:
sourceType - the source type
targetType - the target type

canConvert

public boolean canConvert(java.lang.Class<?> sourceType,
                          java.lang.Class<?> targetType)
Description copied from interface: ConversionService
Returns true if objects of sourceType can be converted to targetType. If this method returns true, it means ConversionService.convert(Object, Class) is capable of converting an instance of sourceType to targetType. Special note on collections, arrays, and maps types: For conversion between collection, array, and map types, this method will return 'true' even though a convert invocation may still generate a ConversionException if the underlying elements are not convertible. Callers are expected to handle this exceptional case when working with collections and maps.

Specified by:
canConvert in interface ConversionService
Parameters:
sourceType - the source type to convert from (may be null if source is null)
targetType - the target type to convert to (required)
Returns:
true if a conversion can be performed, false if not

canConvert

public boolean canConvert(TypeDescriptor sourceType,
                          TypeDescriptor targetType)
Description copied from interface: ConversionService
Returns true if objects of sourceType can be converted to the targetType. The TypeDescriptors provide additional context about the source and target locations where conversion would occur, often object fields or property locations. If this method returns true, it means ConversionService.convert(Object, TypeDescriptor, TypeDescriptor) is capable of converting an instance of sourceType to targetType. Special note on collections, arrays, and maps types: For conversion between collection, array, and map types, this method will return 'true' even though a convert invocation may still generate a ConversionException if the underlying elements are not convertible. Callers are expected to handle this exceptional case when working with collections and maps.

Specified by:
canConvert in interface ConversionService
Parameters:
sourceType - context about the source type to convert from (may be null if source is null)
targetType - context about the target type to convert to (required)
Returns:
true if a conversion can be performed between the source and target types, false if not

convert

public <T> T convert(java.lang.Object source,
                     java.lang.Class<T> targetType)
Description copied from interface: ConversionService
Convert the source to targetType.

Specified by:
convert in interface ConversionService
Parameters:
source - the source object to convert (may be null)
targetType - the target type to convert to (required)
Returns:
the converted object, an instance of targetType

convert

public java.lang.Object convert(java.lang.Object source,
                                TypeDescriptor sourceType,
                                TypeDescriptor targetType)
Description copied from interface: ConversionService
Convert the source to targetType. The TypeDescriptors provide additional context about the source and target locations where conversion will occur, often object fields or property locations.

Specified by:
convert in interface ConversionService
Parameters:
source - the source object to convert (may be null)
sourceType - context about the source type converting from (may be null if source is null)
targetType - context about the target type to convert to (required)
Returns:
the converted object, an instance of targetType

convert

public java.lang.Object convert(java.lang.Object source,
                                TypeDescriptor targetType)
Convenience operation for converting a source object to the specified targetType, where the targetType is a descriptor that provides additional conversion context. Simply delegates to convert(Object, TypeDescriptor, TypeDescriptor) and encapsulates the construction of the sourceType descriptor using TypeDescriptor.forObject(Object).

Parameters:
source - the source object
targetType - the target type
Returns:
the converted value
Throws:
ConversionException - if a conversion exception occurred
java.lang.IllegalArgumentException - if targetType is null
java.lang.IllegalArgumentException - if sourceType is null but source is not null

toString

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

convertNullSource

protected java.lang.Object convertNullSource(TypeDescriptor sourceType,
                                             TypeDescriptor targetType)
Template method to convert a null source.

Default implementation returns null. Subclasses may override to return custom null objects for specific target types.

Parameters:
sourceType - the sourceType to convert from
targetType - the targetType to convert to
Returns:
the converted null object

getConverter

protected GenericConverter getConverter(TypeDescriptor sourceType,
                                        TypeDescriptor targetType)
Hook method to lookup the converter for a given sourceType/targetType pair. First queries this ConversionService's converter cache. On a cache miss, then performs an exhaustive search for a matching converter. If no converter matches, returns the default converter. Subclasses may override.

Parameters:
sourceType - the source type to convert from
targetType - the target type to convert to
Returns:
the generic converter that will perform the conversion, or null if no suitable converter was found
See Also:
getDefaultConverter(TypeDescriptor, TypeDescriptor)

getDefaultConverter

protected GenericConverter getDefaultConverter(TypeDescriptor sourceType,
                                               TypeDescriptor targetType)
Return the default converter if no converter is found for the given sourceType/targetType pair. Returns a NO_OP Converter if the sourceType is assignable to the targetType. Returns null otherwise, indicating no suitable converter could be found. Subclasses may override.

Parameters:
sourceType - the source type to convert from
targetType - the target type to convert to
Returns:
the default generic converter that will perform the conversion

getRequiredTypeInfo

private GenericConverter.ConvertiblePair getRequiredTypeInfo(java.lang.Object converter,
                                                             java.lang.Class<?> genericIfc)

getMatchableConverters

private GenericConversionService.MatchableConverters getMatchableConverters(java.lang.Class<?> sourceType,
                                                                            java.lang.Class<?> targetType)

invalidateCache

private void invalidateCache()

getSourceConverterMap

private java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> getSourceConverterMap(java.lang.Class<?> sourceType)

findConverterForClassPair

private GenericConverter findConverterForClassPair(TypeDescriptor sourceType,
                                                   TypeDescriptor targetType)

getTargetConvertersForSource

private java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> getTargetConvertersForSource(java.lang.Class<?> sourceType)

getMatchingConverterForTarget

private GenericConverter getMatchingConverterForTarget(TypeDescriptor sourceType,
                                                       TypeDescriptor targetType,
                                                       java.util.Map<java.lang.Class<?>,GenericConversionService.MatchableConverters> converters)

addInterfaceHierarchy

private void addInterfaceHierarchy(java.lang.Class<?> interfaceType,
                                   java.util.Set<java.lang.Class<?>> interfaces)

matchConverter

private GenericConverter matchConverter(GenericConversionService.MatchableConverters matchable,
                                        TypeDescriptor sourceFieldType,
                                        TypeDescriptor targetFieldType)

handleConverterNotFound

private java.lang.Object handleConverterNotFound(java.lang.Object source,
                                                 TypeDescriptor sourceType,
                                                 TypeDescriptor targetType)

handleResult

private java.lang.Object handleResult(TypeDescriptor sourceType,
                                      TypeDescriptor targetType,
                                      java.lang.Object result)

assertNotPrimitiveTargetType

private void assertNotPrimitiveTargetType(TypeDescriptor sourceType,
                                          TypeDescriptor targetType)