类 GenericConversionService

java.lang.Object
cn.taketoday.core.conversion.support.GenericConversionService
所有已实现的接口:
ConversionService, ConverterRegistry, ConfigurableConversionService
直接已知子类:
DefaultConversionService

public class GenericConversionService extends Object implements ConfigurableConversionService
Base 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
  • 构造器详细资料

    • 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 from sourceType to targetType.
      指定者:
      removeConvertible 在接口中 ConverterRegistry
      参数:
      sourceType - the source type
      targetType - the target type
    • canConvert

      public boolean canConvert(@Nullable Class<?> sourceType, Class<?> targetType)
      从接口复制的说明: ConversionService
      Return 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 在接口中 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 not
    • canConvert

      public boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType)
      从接口复制的说明: ConversionService
      Return 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 在接口中 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 not
    • canBypassConvert

      public boolean canBypassConvert(@Nullable 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 be null if source is null)
      targetType - context about the target type to convert to (required)
      返回:
      true if conversion can be bypassed; false otherwise
      抛出:
      IllegalArgumentException - if targetType is null
    • convert

      @Nullable public <T> T convert(@Nullable Object source, Class<T> targetType)
      从接口复制的说明: ConversionService
      Convert the given source to the specified targetType.
      指定者:
      convert 在接口中 ConversionService
      参数:
      source - the source object to convert (may be null)
      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)
      从接口复制的说明: ConversionService
      Convert the given 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 在接口中 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)
      返回:
      the converted object, an instance of targetType
    • convert

      @Nullable public <T> T convert(@Nullable 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 to convert(Object, TypeDescriptor, TypeDescriptor) and encapsulates the construction of the source type descriptor using TypeDescriptor.fromObject(Object).
      指定者:
      convert 在接口中 ConversionService
      参数:
      source - the source object
      targetType - the target type
      返回:
      the converted value
      抛出:
      ConversionException - if a conversion exception occurred
      IllegalArgumentException - if targetType is null, or sourceType is null but source is not null
    • toString

      public String toString()
      覆盖:
      toString 在类中 Object
    • convertNullSource

      @Nullable protected Object convertNullSource(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType)
      Template method to convert a 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 from
      targetType - 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 from
      targetType - the target type to convert to
      返回:
      the generic converter that will perform the conversion, or null if 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 null otherwise, indicating no suitable converter could be found.

      参数:
      sourceType - the source type to convert from
      targetType - the target type to convert to
      返回:
      the default generic converter that will perform the conversion