public class

GenericConversionService

extends Object
implements ConversionService ConverterRegistry
java.lang.Object
   ↳ org.springframework.core.convert.support.GenericConversionService
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

Base ConversionService implementation suitable for use in most environments. Implements ConverterRegistry as registration API.

Summary

Public Constructors
GenericConversionService()
Public Methods
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.
boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
Returns true if objects of sourceType can be converted to the targetType.
boolean canConvert(Class<?> sourceType, Class<?> targetType)
Returns true if objects of sourceType can be converted to 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.
void removeConvertible(Class<?> sourceType, Class<?> targetType)
Remove any converters from sourceType to targetType.
String toString()
Protected Methods
Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType)
Template method to convert a null source.
GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType)
Hook method to lookup the converter for a given sourceType/targetType pair.
GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType)
Return the default converter if no converter is found for the given sourceType/targetType pair.
[Expand]
Inherited Methods
From class java.lang.Object
From interface org.springframework.core.convert.ConversionService
From interface org.springframework.core.convert.converter.ConverterRegistry

Public Constructors

public GenericConversionService ()

Also: SpringCore

Public Methods

public void addConverter (Converter<?, ?> converter)

Also: SpringCore

Add a plain converter to this registry.

public void addConverter (GenericConverter converter)

Also: SpringCore

Add a generic converter to this registry.

public void addConverterFactory (ConverterFactory<?, ?> converterFactory)

Also: SpringCore

Add a ranged converter factory to this registry.

public boolean canConvert (TypeDescriptor sourceType, TypeDescriptor targetType)

Also: SpringCore

Returns true if objects of sourceType can be converted to the targetType. The TypeDescriptors provide additional context about the field 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.

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

public boolean canConvert (Class<?> sourceType, Class<?> targetType)

Also: SpringCore

Returns true if objects of sourceType can be converted to targetType.

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

public T convert (Object source, Class<T> targetType)

Also: SpringCore

Convert the source to targetType.

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

public Object convert (Object source, TypeDescriptor sourceType, TypeDescriptor targetType)

Also: SpringCore

Convert the source to targetType. The TypeDescriptors provide additional context about the field 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.

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

public void removeConvertible (Class<?> sourceType, Class<?> targetType)

Also: SpringCore

Remove any converters from sourceType to targetType.

Parameters
sourceType the source type
targetType the target type

public String toString ()

Protected Methods

protected Object convertNullSource (TypeDescriptor sourceType, TypeDescriptor targetType)

Also: SpringCore

Template method to convert a null source.

Default implementation returns null. Throws a ConversionFailedException if the targetType is a primitive type, as null cannot be assigned to a primitive type. 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

protected GenericConverter getConverter (TypeDescriptor sourceType, TypeDescriptor targetType)

Also: SpringCore

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

protected GenericConverter getDefaultConverter (TypeDescriptor sourceType, TypeDescriptor targetType)

Also: SpringCore

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