接口 ConversionService
- 所有已知子接口:
ConfigurableConversionService
- 所有已知实现类:
DefaultConversionService
,DefaultFormattingConversionService
,FormattingConversionService
,GenericConversionService
public interface ConversionService
A service interface for type conversion. This is the entry point into the convert system. Callconvert(Object, Class)
to perform a thread-safe type conversion using this system.- 从以下版本开始:
- 3.0
- 作者:
- Keith Donald, Phillip Webb
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 boolean
canConvert(Class<?> sourceType, Class<?> targetType)
Returntrue
if objects ofsourceType
can be converted to thetargetType
.boolean
canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
Returntrue
if objects ofsourceType
can be converted to thetargetType
.<T> T
convert(Object source, Class<T> targetType)
Convert the givensource
to the specifiedtargetType
.Object
convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
Convert the givensource
to the specifiedtargetType
.
方法详细资料
canConvert
boolean canConvert(@Nullable Class<?> sourceType, Class<?> targetType)
Returntrue
if objects ofsourceType
can be converted to thetargetType
.If this method returns
true
, it meansconvert(Object, Class)
is capable of converting an instance ofsourceType
totargetType
.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 aConversionException
if the underlying elements are not convertible. Callers are expected to handle this exceptional case when working with collections and maps.- 参数:
sourceType
- the source type to convert from (may benull
if source isnull
)targetType
- the target type to convert to (required)- 返回:
true
if a conversion can be performed,false
if not- 抛出:
IllegalArgumentException
- iftargetType
isnull
canConvert
boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType)
Returntrue
if objects ofsourceType
can 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 ofsourceType
totargetType
.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 aConversionException
if the underlying elements are not convertible. Callers are expected to handle this exceptional case when working with collections and maps.- 参数:
sourceType
- context about the source type to convert from (may benull
if source isnull
)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 not- 抛出:
IllegalArgumentException
- iftargetType
isnull
convert
@Nullable <T> T convert(@Nullable Object source, Class<T> targetType)
Convert the givensource
to the specifiedtargetType
.- 参数:
source
- the source object to convert (may benull
)targetType
- the target type to convert to (required)- 返回:
- the converted object, an instance of targetType
- 抛出:
ConversionException
- if a conversion exception occurredIllegalArgumentException
- if targetType isnull
convert
@Nullable Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType)
Convert the givensource
to the specifiedtargetType
. The TypeDescriptors provide additional context about the source and target locations where conversion will occur, often object fields or property locations.- 参数:
source
- the source object to convert (may benull
)sourceType
- context about the source type to convert from (may benull
if source isnull
)targetType
- context about the target type to convert to (required)- 返回:
- the converted object, an instance of
targetType
- 抛出:
ConversionException
- if a conversion exception occurredIllegalArgumentException
- if targetType isnull
, orsourceType
isnull
but source is notnull