Interface ConversionService
- All Known Subinterfaces:
ConfigurableConversionService
- All Known Implementing Classes:
DefaultConversionService,DefaultFormattingConversionService,FormattingConversionService,GenericConversionService
convert(Object, Class) to perform a thread-safe type conversion using this system.- Since:
- 3.0
- Author:
- Keith Donald, Phillip Webb
-
Method Summary
Modifier and TypeMethodDescriptionbooleancanConvert(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.default Objectconvert(Object source, TypeDescriptor targetType) Convert the givensourceto the specifiedtargetType.convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) Convert the givensourceto the specifiedtargetType.
-
Method Details
-
canConvert
Returntrueif objects ofsourceTypecan be converted to thetargetType.If this method returns
true, it meansconvert(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.- 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- Throws:
IllegalArgumentException- iftargetTypeisnull
-
canConvert
Returntrueif 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 meansconvert(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.- 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- Throws:
IllegalArgumentException- iftargetTypeisnull
-
convert
Convert the givensourceto the specifiedtargetType.- 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
- Throws:
ConversionException- if a conversion exception occurredIllegalArgumentException- if targetType isnull
-
convert
Convert the givensourceto the specifiedtargetType.Delegates to
convert(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- Since:
- 6.1
-
convert
@Nullable Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) Convert 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.- 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 - Throws:
ConversionException- if a conversion exception occurredIllegalArgumentException- if targetType isnull, orsourceTypeisnullbut source is notnull
-