public class GenericConversionService extends Object implements ConfigurableConversionService
ConversionService
implementation suitable for use in most environments.
Indirectly implements ConverterRegistry
as registration API through the
ConfigurableConversionService
interface.Constructor and Description |
---|
GenericConversionService() |
Modifier and Type | Method and Description |
---|---|
<S,T> void |
addConverter(Class<S> sourceType,
Class<T> targetType,
Converter<? super S,? extends T> 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<?,?> factory)
Add a ranged converter factory to this registry.
|
boolean |
canBypassConvert(TypeDescriptor sourceType,
TypeDescriptor targetType)
Return whether conversion between the source type and the target type can be bypassed.
|
boolean |
canConvert(Class<?> sourceType,
Class<?> targetType)
Return
true if objects of sourceType can be converted to the targetType . |
boolean |
canConvert(TypeDescriptor sourceType,
TypeDescriptor targetType)
Return
true if objects of sourceType can be converted to the targetType . |
<T> T |
convert(Object source,
Class<T> targetType)
Convert the given
source to the specified targetType . |
Object |
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.
|
Object |
convert(Object source,
TypeDescriptor sourceType,
TypeDescriptor targetType)
Convert the given
source to the specified targetType . |
protected Object |
convertNullSource(TypeDescriptor sourceType,
TypeDescriptor targetType)
Template method to convert a
null source. |
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.
|
void |
removeConvertible(Class<?> sourceType,
Class<?> targetType)
Remove any converters from
sourceType to targetType . |
String |
toString() |
public void addConverter(Converter<?,?> converter)
ConverterRegistry
addConverter
in interface ConverterRegistry
public <S,T> void addConverter(Class<S> sourceType, Class<T> targetType, Converter<? super S,? extends T> converter)
ConverterRegistry
Allows for a Converter to be reused for multiple distinct pairs without having to create a Converter class for each pair.
addConverter
in interface ConverterRegistry
public void addConverter(GenericConverter converter)
ConverterRegistry
addConverter
in interface ConverterRegistry
public void addConverterFactory(ConverterFactory<?,?> factory)
ConverterRegistry
addConverterFactory
in interface ConverterRegistry
public void removeConvertible(Class<?> sourceType, Class<?> targetType)
ConverterRegistry
sourceType
to targetType
.removeConvertible
in interface ConverterRegistry
sourceType
- the source typetargetType
- the target typepublic boolean canConvert(@Nullable Class<?> sourceType, Class<?> targetType)
ConversionService
true
if objects of sourceType
can be converted to the 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.
canConvert
in interface ConversionService
sourceType
- the source type to convert from (may be null
if source is null
)targetType
- the target type to convert to (required)true
if a conversion can be performed, false
if notpublic boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType)
ConversionService
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.
canConvert
in interface ConversionService
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)true
if a conversion can be performed between the source and target types,
false
if notpublic boolean canBypassConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType)
More precisely, this method will return true if objects of sourceType can be converted to the target type by returning the source object unchanged.
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)true
if conversion can be bypassed; false
otherwiseIllegalArgumentException
- if targetType is null
@Nullable public <T> T convert(@Nullable Object source, Class<T> targetType)
ConversionService
source
to the specified targetType
.convert
in interface ConversionService
source
- the source object to convert (may be null
)targetType
- the target type to convert to (required)@Nullable public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType)
ConversionService
source
to the specified targetType
.
The TypeDescriptors provide additional context about the source and target locations
where conversion will occur, often object fields or property locations.convert
in interface ConversionService
source
- the source object to convert (may be null
)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)targetType
@Nullable public Object convert(@Nullable Object source, TypeDescriptor targetType)
convert(Object, TypeDescriptor, TypeDescriptor)
and
encapsulates the construction of the source type descriptor using
TypeDescriptor.forObject(Object)
.source
- the source objecttargetType
- the target typeConversionException
- if a conversion exception occurredIllegalArgumentException
- if targetType is null
,
or sourceType is null
but source is not null
@Nullable protected Object convertNullSource(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType)
null
source.
The default implementation returns null
or the Java 8
Optional.empty()
instance if the target type is
java.util.Optional
. Subclasses may override this to return
custom null
objects for specific target types.
sourceType
- the source type to convert fromtargetType
- the target type to convert to@Nullable protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType)
sourceType
- the source type to convert fromtargetType
- the target type to convert tonull
if no suitable converter was foundgetDefaultConverter(TypeDescriptor, TypeDescriptor)
@Nullable protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType)
Returns a NO_OP Converter if the source type is assignable to the target type.
Returns null
otherwise, indicating no suitable converter could be found.
sourceType
- the source type to convert fromtargetType
- the target type to convert to