类 GenericConversionService
- 直接已知子类:
DefaultConversionService
ConversionService implementation suitable for use in most environments.
Indirectly implements ConverterRegistry as registration API through the
ConfigurableConversionService interface.- 从以下版本开始:
- 4.0 2022/2/17 16:39
- 作者:
- Keith Donald, Juergen Hoeller, Chris Beams, Phillip Webb, David Haraburda, Harry Yang
-
嵌套类概要
嵌套类修饰符和类型类说明private final classAdapts aConverterto aGenericConverter.private static final recordKey for use with the converter cache.private final classAdapts aConverterFactoryto aGenericConverter.private static final classManages all converters registered with the service.private static classManages converters registered with a specificGenericConverter.ConvertiblePair.private static final recordInternal converter that performs no operation. -
字段概要
字段修饰符和类型字段说明private final ConcurrentReferenceHashMap<GenericConversionService.ConverterCacheKey,GenericConverter> private final GenericConversionService.Convertersprivate static final GenericConverterUsed as a cache entry when no converter is available.private static final GenericConverterGeneral NO-OP converter used when conversion is not required. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明voidaddConverter(Converter<?, ?> converter) Add a plain converter to this registry.voidaddConverter(GenericConverter converter) Add a generic converter to this registry.<S,T> void addConverter(Class<S> sourceType, Class<T> targetType, Converter<? super S, ? extends T> converter) Add a plain converter to this registry.voidaddConverterFactory(ConverterFactory<?, ?> factory) Add a ranged converter factory to this registry.private voidassertNotPrimitiveTargetType(TypeDescriptor sourceType, TypeDescriptor targetType) booleancanBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType) Return whether conversion between the source type and the target type can be bypassed.booleancanConvert(TypeDescriptor sourceType, TypeDescriptor targetType) Returntrueif objects ofsourceTypecan be converted to thetargetType.booleancanConvert(Class<?> sourceType, Class<?> targetType) Returntrueif objects ofsourceTypecan be converted to thetargetType.<T> Tconvert(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.<T> TConvert the givensourceto the specifiedtargetType.protected ObjectconvertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) Template method to convert anullsource.getConverter(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.private ResolvableType[]getRequiredTypeInfo(Class<?> converterClass, Class<?> genericIfc) private ObjecthandleConverterNotFound(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) private ObjecthandleResult(TypeDescriptor sourceType, TypeDescriptor targetType, Object result) private voidvoidremoveConvertible(Class<?> sourceType, Class<?> targetType) Remove any converters fromsourceTypetotargetType.toString()从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait从接口继承的方法 cn.taketoday.core.conversion.ConversionService
getConverter, getConverter, getConverter, getConverter
-
字段详细资料
-
NO_OP_CONVERTER
General NO-OP converter used when conversion is not required. -
NO_MATCH
Used as a cache entry when no converter is available. This converter is never returned. -
converters
-
converterCache
private final ConcurrentReferenceHashMap<GenericConversionService.ConverterCacheKey,GenericConverter> converterCache
-
-
构造器详细资料
-
GenericConversionService
public GenericConversionService()
-
-
方法详细资料
-
addConverter
从接口复制的说明:ConverterRegistryAdd 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) 从接口复制的说明: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.
- 指定者:
addConverter在接口中ConverterRegistry
-
addConverter
从接口复制的说明:ConverterRegistryAdd a generic converter to this registry.- 指定者:
addConverter在接口中ConverterRegistry
-
addConverterFactory
从接口复制的说明:ConverterRegistryAdd a ranged converter factory to this registry. The convertible source/target type pair is derived from the ConverterFactory's parameterized types.- 指定者:
addConverterFactory在接口中ConverterRegistry
-
removeConvertible
从接口复制的说明:ConverterRegistryRemove any converters fromsourceTypetotargetType.- 指定者:
removeConvertible在接口中ConverterRegistry- 参数:
sourceType- the source typetargetType- the target type
-
canConvert
从接口复制的说明: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.- 指定者:
canConvert在接口中ConversionService- 参数:
sourceType- the source type to convert from (may benullif source isnull)targetType- the target type to convert to (required)- 返回:
trueif a conversion can be performed,falseif not
-
canConvert
从接口复制的说明: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.- 指定者:
canConvert在接口中ConversionService- 参数:
sourceType- context about the source type to convert from (may benullif source isnull)targetType- context about the target type to convert to (required)- 返回:
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.
- 参数:
sourceType- context about the source type to convert from (may benullif source isnull)targetType- context about the target type to convert to (required)- 返回:
trueif conversion can be bypassed;falseotherwise- 抛出:
IllegalArgumentException- if targetType isnull
-
convert
从接口复制的说明:ConversionServiceConvert the givensourceto 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
@Nullable public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) 从接口复制的说明: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.- 指定者:
convert在接口中ConversionService- 参数:
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)- 返回:
- 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).- 指定者:
convert在接口中ConversionService- 参数:
source- the source objecttargetType- the target type- 返回:
- the converted value
- 抛出:
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.- 参数:
sourceType- the source type to convert fromtargetType- the target type to convert to- 返回:
- the converted null object
-
getConverter
@Nullable public 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.- 指定者:
getConverter在接口中ConversionService- 参数:
sourceType- the source type to convert fromtargetType- the target type to convert to- 返回:
- the generic converter that will perform the conversion,
or
nullif no suitable converter was found - 另请参阅:
-
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.- 参数:
sourceType- the source type to convert fromtargetType- the target type to convert to- 返回:
- the default generic converter that will perform the conversion
-
getRequiredTypeInfo
@Nullable private ResolvableType[] getRequiredTypeInfo(Class<?> converterClass, Class<?> genericIfc) -
invalidateCache
private void invalidateCache() -
handleConverterNotFound
@Nullable private Object handleConverterNotFound(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) -
handleResult
@Nullable private Object handleResult(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType, @Nullable Object result) -
assertNotPrimitiveTargetType
private void assertNotPrimitiveTargetType(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType)
-