Class GenericConversionService
- All Implemented Interfaces:
ConversionService,ConverterRegistry,ConfigurableConversionService
- Direct Known Subclasses:
DefaultConversionService,FormattingConversionService
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, Phillip Webb, David Haraburda
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<S,T> void addConverter(Class<S> sourceType, Class<T> targetType, Converter<? super S, ? extends T> converter) Add a plain converter to this registry.voidaddConverter(Converter<?, ?> converter) Add a plain converter to this registry.voidaddConverter(GenericConverter converter) Add a generic converter to this registry.voidaddConverterFactory(ConverterFactory<?, ?> factory) Add a ranged converter factory to this registry.booleancanBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType) Return whether conversion between the source type and the target type can be bypassed.booleancanConvert(Class<?> sourceType, Class<?> targetType) Returntrueif objects ofsourceTypecan be converted to thetargetType.booleancanConvert(TypeDescriptor sourceType, TypeDescriptor targetType) Returntrueif objects ofsourceTypecan be converted to thetargetType.<T> TConvert the givensourceto the specifiedtargetType.convert(Object source, TypeDescriptor targetType) Convenience operation for converting a source object to the specified targetType, where the target type is a descriptor that provides additional conversion context.convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) Convert the givensourceto the specifiedtargetType.protected ObjectconvertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) Template method to convert anullsource.protected GenericConvertergetConverter(TypeDescriptor sourceType, TypeDescriptor targetType) Hook method to lookup the converter for a given sourceType/targetType pair.protected GenericConvertergetDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType) Return the default converter if no converter is found for the given sourceType/targetType pair.voidremoveConvertible(Class<?> sourceType, Class<?> targetType) Remove any converters fromsourceTypetotargetType.toString()
-
Constructor Details
-
GenericConversionService
public GenericConversionService()
-
-
Method Details
-
addConverter
Description copied from interface:ConverterRegistryAdd a plain converter to this registry. The convertible source/target type pair is derived from the Converter's parameterized types.- Specified by:
addConverterin interfaceConverterRegistry
-
addConverter
public <S,T> void addConverter(Class<S> sourceType, Class<T> targetType, Converter<? super S, ? extends T> converter) Description copied from interface:ConverterRegistryAdd a plain converter to this registry. The convertible source/target type 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:
addConverterin interfaceConverterRegistry
-
addConverter
Description copied from interface:ConverterRegistryAdd a generic converter to this registry.- Specified by:
addConverterin interfaceConverterRegistry
-
addConverterFactory
Description copied from interface:ConverterRegistryAdd a ranged converter factory to this registry. The convertible source/target type pair is derived from the ConverterFactory's parameterized types.- Specified by:
addConverterFactoryin interfaceConverterRegistry
-
removeConvertible
Description copied from interface:ConverterRegistryRemove any converters fromsourceTypetotargetType.- Specified by:
removeConvertiblein interfaceConverterRegistry- Parameters:
sourceType- the source typetargetType- the target type
-
canConvert
Description copied from interface:ConversionServiceReturntrueif objects ofsourceTypecan be converted to thetargetType.If this method returns
true, it meansConversionService.convert(Object, Class)is capable of converting an instance ofsourceTypetotargetType.Special note on collections, arrays, and maps types: For conversion between collection, array, and map types, this method will return
trueeven though a convert invocation may still generate aConversionExceptionif the underlying elements are not convertible. Callers are expected to handle this exceptional case when working with collections and maps.- Specified by:
canConvertin interfaceConversionService- Parameters:
sourceType- the source type to convert from (may benullif source isnull)targetType- the target type to convert to (required)- Returns:
trueif a conversion can be performed,falseif not
-
canConvert
Description copied from interface:ConversionServiceReturntrueif objects ofsourceTypecan be converted to thetargetType. 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 meansConversionService.convert(Object, TypeDescriptor, TypeDescriptor)is capable of converting an instance ofsourceTypetotargetType.Special note on collections, arrays, and maps types: For conversion between collection, array, and map types, this method will return
trueeven though a convert invocation may still generate aConversionExceptionif the underlying elements are not convertible. Callers are expected to handle this exceptional case when working with collections and maps.- Specified by:
canConvertin interfaceConversionService- Parameters:
sourceType- context about the source type to convert from (may benullif source isnull)targetType- context about the target type to convert to (required)- Returns:
trueif a conversion can be performed between the source and target types,falseif not
-
canBypassConvert
Return whether conversion between the source type and the target type can be bypassed.More precisely, this method will return true if objects of sourceType can be converted to the target type by returning the source object unchanged.
- Parameters:
sourceType- context about the source type to convert from (may benullif source isnull)targetType- context about the target type to convert to (required)- Returns:
trueif conversion can be bypassed;falseotherwise- Throws:
IllegalArgumentException- if targetType isnull- Since:
- 3.2
-
convert
Description copied from interface:ConversionServiceConvert the givensourceto the specifiedtargetType.- Specified by:
convertin interfaceConversionService- Parameters:
source- the source object to convert (may benull)targetType- the target type to convert to (required)- Returns:
- the converted object, an instance of targetType
-
convert
@Nullable public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) Description copied from interface:ConversionServiceConvert the givensourceto the specifiedtargetType. The TypeDescriptors provide additional context about the source and target locations where conversion will occur, often object fields or property locations.- Specified by:
convertin interfaceConversionService- Parameters:
source- the source object to convert (may benull)sourceType- context about the source type to convert from (may benullif source isnull)targetType- context about the target type to convert to (required)- Returns:
- the converted object, an instance of
targetType
-
convert
Convenience operation for converting a source object to the specified targetType, where the target type is a descriptor that provides additional conversion context. Simply delegates toconvert(Object, TypeDescriptor, TypeDescriptor)and encapsulates the construction of the source type descriptor usingTypeDescriptor.forObject(Object).- Parameters:
source- the source objecttargetType- the target type- Returns:
- the converted value
- Throws:
ConversionException- if a conversion exception occurredIllegalArgumentException- if targetType isnull, or sourceType isnullbut source is notnull
-
toString
-
convertNullSource
@Nullable protected Object convertNullSource(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) Template method to convert anullsource.The default implementation returns
nullor the Java 8Optional.empty()instance if the target type isjava.util.Optional. Subclasses may override this to return customnullobjects for specific target types.- Parameters:
sourceType- the source type to convert fromtargetType- the target type to convert to- Returns:
- the converted null object
-
getConverter
@Nullable 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.- Parameters:
sourceType- the source type to convert fromtargetType- the target type to convert to- Returns:
- the generic converter that will perform the conversion,
or
nullif no suitable converter was found - See Also:
-
getDefaultConverter
@Nullable 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 source type is assignable to the target type. Returns
nullotherwise, indicating no suitable converter could be found.- Parameters:
sourceType- the source type to convert fromtargetType- the target type to convert to- Returns:
- the default generic converter that will perform the conversion
-