org.springframework.core.convert.support
Class GenericConversionService

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

public class GenericConversionService
extends Object
implements ConversionService, ConverterRegistry

Base implementation of a conversion service.

Since:
3.0
Author:
Keith Donald, Juergen Hoeller
See Also:
addConverter(Converter), addConverterFactory(ConverterFactory)

Constructor Summary
GenericConversionService()
           
 
Method Summary
 void addConverter(Converter<?,?> converter)
          Add a converter to this registry.
 void addConverterFactory(ConverterFactory<?,?> converterFactory)
          Add a ranged converter factory to this registry.
protected  void addGenericConverter(Class<?> sourceType, Class<?> targetType, GenericConverter converter)
          Registers a GenericConverter.
 boolean canConvert(Class<?> sourceType, 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(Object source, Class<T> targetType)
          Convert the source to targetType.
 Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
          Convert the source to targetType.
protected  Object convertNull(TypeDescriptor sourceType, TypeDescriptor targetType)
          Hook method to convert a null value.
protected  GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType)
          Hook method to lookup the converter for a given sourceType/targetType pair.
 ConversionService getParent()
          Returns the parent of this conversion service.
 void removeConvertible(Class<?> sourceType, Class<?> targetType)
          Remove the conversion logic for the sourceType to the targetType.
 void setConverterFactories(Set<ConverterFactory> converters)
          Registers the converter factories in the set provided.
 void setConverters(Set<Converter> converters)
          Registers the converters in the set provided.
 void setParent(ConversionService parent)
          Set the parent of this conversion service.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericConversionService

public GenericConversionService()
Method Detail

setConverters

public void setConverters(Set<Converter> converters)
Registers the converters in the set provided. JavaBean-friendly alternative to calling addConverter(Converter).

See Also:
addConverter(Converter)

setConverterFactories

public void setConverterFactories(Set<ConverterFactory> converters)
Registers the converter factories in the set provided. JavaBean-friendly alternative to calling addConverterFactory(ConverterFactory).

See Also:
addConverterFactory(ConverterFactory)

setParent

public void setParent(ConversionService parent)
Set the parent of this conversion service. This is optional.


getParent

public ConversionService getParent()
Returns the parent of this conversion service. Could be null.


addConverter

public void addConverter(Converter<?,?> converter)
Description copied from interface: ConverterRegistry
Add a 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.

Specified by:
addConverterFactory in interface ConverterRegistry

removeConvertible

public void removeConvertible(Class<?> sourceType,
                              Class<?> targetType)
Description copied from interface: ConverterRegistry
Remove the conversion logic for the sourceType to the targetType.

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

canConvert

public boolean canConvert(Class<?> sourceType,
                          Class<?> targetType)
Description copied from interface: ConversionService
Returns true if objects of sourceType can be converted to targetType.

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

convert

public <T> T convert(Object source,
                     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

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 variable locations where conversion would occur, often object property locations. This flavor of the canConvert operation exists mainly for use by a general purpose data mapping framework, and not for use by user code.

Specified by:
canConvert in interface ConversionService
Parameters:
sourceType - context about the source type to convert from (required)
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 Object convert(Object source,
                      TypeDescriptor sourceType,
                      TypeDescriptor targetType)
Description copied from interface: ConversionService
Convert the source to targetType. The TypeDescriptors provide additional context about the variable locations where conversion will occur, often object property locations. This flavor of the convert operation exists mainly for use by a general purpose data mapping framework, and not for use by user code.

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

addGenericConverter

protected void addGenericConverter(Class<?> sourceType,
                                   Class<?> targetType,
                                   GenericConverter converter)
Registers a GenericConverter.

Parameters:
sourceType - the source type to convert from
targetType - the target type to convert to
converter - the generic converter.

convertNull

protected Object convertNull(TypeDescriptor sourceType,
                             TypeDescriptor targetType)
Hook method to convert a null value. Default implementation simply returns null. Subclasses may override to return a custom null objects for specific target types.

Parameters:
sourceType - the sourceType
targetType - the tagetType
Returns:
the 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 map. If no suitable Converter is found, and a parent is set, then queries the parent. If still no suitable Converter is found, returns a NO_OP Converter if the sourceType and targetType are assignable. Returns null if this ConversionService simply cannot convert between sourceType and targetType. 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