Module bus.core

Class CompositeConverter

java.lang.Object
org.miaixz.bus.core.convert.CompositeConverter
All Implemented Interfaces:
Serializable, Converter

public class CompositeConverter extends Object implements Converter, Serializable
复合转换器,融合了所有支持类型和自定义类型的转换规则 在此类中,存放着默认转换器和自定义转换器,默认转换器是预定义的一些转换器,自定义转换器存放用户自定的转换器。 转换过程类似于转换链,过程如下:

     处理null、Optional --> 自定义匹配转换器 --> 自定义类型转换器 --> 预注册的标准转换器 --> Map、集合、Enum等特殊转换器 --> Bean转换器
 
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Method Details

    • getInstance

      public static CompositeConverter getInstance()
      获得单例的 CompositeConverter
      Returns:
      CompositeConverter
    • register

      public CompositeConverter register(MatcherConverter converter)
      登记自定义转换器,符合MatcherConverter.match(Type, Class, Object)则使用其转换器 注意:如果单例使用,此方法会影响全局
      Parameters:
      converter - 转换器
      Returns:
      this
    • register

      public CompositeConverter register(Type type, Converter converter)
      登记自定义转换器,登记的目标类型必须一致 注意:如果单例使用,此方法会影响全局
      Parameters:
      type - 转换的目标类型
      converter - 转换器
      Returns:
      this
    • convert

      public Object convert(Type type, Object value) throws ConvertException
      转换值为指定类型
      Specified by:
      convert in interface Converter
      Parameters:
      type - 类型
      value - 值
      Returns:
      转换后的值,默认为null
      Throws:
      ConvertException - 转换器不存在
    • convert

      public <T> T convert(Type type, Object value, T defaultValue) throws ConvertException
      转换值为指定类型,自定义转换器优先
      Specified by:
      convert in interface Converter
      Type Parameters:
      T - 转换的目标类型(转换器转换到的类型)
      Parameters:
      type - 类型
      value - 值
      defaultValue - 默认值
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换器不存在
    • convert

      public <T> T convert(Type type, Object value, T defaultValue, boolean isCustomFirst) throws ConvertException
      转换值为指定类型
      Type Parameters:
      T - 转换的目标类型(转换器转换到的类型)
      Parameters:
      type - 类型目标
      value - 被转换值
      defaultValue - 默认值
      isCustomFirst - 是否自定义转换器优先
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换器不存在