类 GenericConversionService
- java.lang.Object
- org.springframework.core.convert.support.GenericConversionService
public class GenericConversionService extends Object implements ConfigurableConversionService
BaseConversionService
implementation suitable for use in most environments. Indirectly implementsConverterRegistry
as registration API through theConfigurableConversionService
interface.- 从以下版本开始:
- 3.0
- 作者:
- Keith Donald, Juergen Hoeller, Chris Beams, Phillip Webb, David Haraburda
构造器概要
构造器 构造器 说明 GenericConversionService()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 <S,T>
voidaddConverter(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)
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 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 givensource
to the specifiedtargetType
.protected Object
convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType)
Template method to convert anull
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 fromsourceType
totargetType
.String
toString()
构造器详细资料
GenericConversionService
public GenericConversionService()
方法详细资料
addConverter
public void addConverter(Converter<?,?> converter)
从接口复制的说明:ConverterRegistry
Add a plain converter to this registry. The convertible source/target type pair is derived from the Converter's parameterized types.- 指定者:
addConverter
在接口中ConverterRegistry
addConverter
public <S,T> void addConverter(Class<S> sourceType, Class<T> targetType, Converter<? super S,? extends T> converter)
从接口复制的说明:ConverterRegistry
Add 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.
- 指定者:
addConverter
在接口中ConverterRegistry
addConverter
public void addConverter(GenericConverter converter)
从接口复制的说明:ConverterRegistry
Add a generic converter to this registry.- 指定者:
addConverter
在接口中ConverterRegistry
addConverterFactory
public void addConverterFactory(ConverterFactory<?,?> factory)
从接口复制的说明:ConverterRegistry
Add a ranged converter factory to this registry. The convertible source/target type pair is derived from the ConverterFactory's parameterized types.- 指定者:
addConverterFactory
在接口中ConverterRegistry
removeConvertible
public void removeConvertible(Class<?> sourceType, Class<?> targetType)
从接口复制的说明:ConverterRegistry
Remove any converters fromsourceType
totargetType
.- 指定者:
removeConvertible
在接口中ConverterRegistry
- 参数:
sourceType
- the source typetargetType
- the target type
canConvert
public boolean canConvert(Class<?> sourceType, Class<?> targetType)
从接口复制的说明:ConversionService
Returntrue
if objects ofsourceType
can be converted to thetargetType
.If this method returns
true
, it meansConversionService.convert(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.- 指定者:
canConvert
在接口中ConversionService
- 参数:
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
canConvert
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
从接口复制的说明:ConversionService
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 meansConversionService.convert(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.- 指定者:
canConvert
在接口中ConversionService
- 参数:
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
canBypassConvert
public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
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.
- 参数:
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 conversion can be bypassed;false
otherwise- 抛出:
IllegalArgumentException
- if targetType isnull
- 从以下版本开始:
- 3.2
convert
public <T> T convert(Object source, Class<T> targetType)
从接口复制的说明:ConversionService
Convert the givensource
to the specifiedtargetType
.- 指定者:
convert
在接口中ConversionService
- 参数:
source
- the source object to convert (may benull
)targetType
- the target type to convert to (required)- 返回:
- the converted object, an instance of targetType
convert
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
从接口复制的说明:ConversionService
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.- 指定者:
convert
在接口中ConversionService
- 参数:
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
convert
public 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. Simply delegates toconvert(Object, TypeDescriptor, TypeDescriptor)
and encapsulates the construction of the source type descriptor usingTypeDescriptor.forObject(Object)
.- 参数:
source
- the source objecttargetType
- the target type- 返回:
- the converted value
- 抛出:
ConversionException
- if a conversion exception occurredIllegalArgumentException
- if targetType isnull
, or sourceType isnull
but source is notnull
convertNullSource
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType)
Template method to convert anull
source.The default implementation returns
null
or the Java 8Optional.empty()
instance if the target type isjava.util.Optional
. Subclasses may override this to return customnull
objects for specific target types.- 参数:
sourceType
- the source type to convert fromtargetType
- the target type to convert to- 返回:
- the converted null object
getConverter
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.- 参数:
sourceType
- the source type to convert fromtargetType
- the target type to convert to- 返回:
- the generic converter that will perform the conversion, or
null
if no suitable converter was found - 另请参阅:
getDefaultConverter(TypeDescriptor, TypeDescriptor)
getDefaultConverter
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
null
otherwise, indicating no suitable converter could be found.- 参数:
sourceType
- the source type to convert fromtargetType
- the target type to convert to- 返回:
- the default generic converter that will perform the conversion