Class LegacyParserAdapter

java.lang.Object
com.ultikits.ultitools.config.convert.LegacyParserAdapter
All Implemented Interfaces:
ConfigConverter<Object>

@Internal public final class LegacyParserAdapter extends Object implements ConfigConverter<Object>
Frozen legacy parser semantics behind a detached input and explicit plain-output boundary.
  • Field Details

    • WIDENING_ORDER

      private static final List<Class<?>> WIDENING_ORDER
      The numeric wrappers in JLS 5.1.2 widening order: every conversion from an earlier entry to a later one is a widening primitive conversion, and no other conversion between them is.
    • WIDENERS

      private static final List<Function<Number,Object>> WIDENERS
      Converts a Number to the wrapper at the same index of WIDENING_ORDER.
    • parserClass

      private final Class<? extends ConfigParser> parserClass
  • Constructor Details

    • LegacyParserAdapter

      public LegacyParserAdapter(Class<? extends ConfigParser> parserClass)
      Parameters:
      parserClass - legacy parser's public no-argument implementation
  • Method Details

    • toPlain

      public Object toPlain(Object value, ConversionContext context) throws ConversionException
      Description copied from interface: ConfigConverter
      Converts a Java value to plain data.
      Specified by:
      toPlain in interface ConfigConverter<Object>
      Parameters:
      value - the Java value
      context - the immutable conversion location and recursive dispatcher
      Returns:
      plain data
      Throws:
      ConversionException - when conversion cannot preserve the value
    • fromPlain

      public Object fromPlain(Object plain, ConversionContext context) throws ConversionException
      Description copied from interface: ConfigConverter
      Converts plain data to a Java value.
      Specified by:
      fromPlain in interface ConfigConverter<Object>
      Parameters:
      plain - the value read from the document
      context - the immutable conversion location and recursive dispatcher
      Returns:
      the Java value
      Throws:
      ConversionException - when the input cannot be converted
    • parser

      Throws:
      ConversionException
    • widenToFieldType

      static Object widenToFieldType(Class<?> fieldType, Object value)
      Gives a boxed numeric field exactly the widening conversions its primitive already gets.

      SnakeYAML hands back an Integer for a whole number such as 1800. Field.set widens that into a long or double field, but it refuses the same value for a Long, Double or Float field (measured: IllegalArgumentException: Can not set java.lang.Long field ... to java.lang.Integer). A boxed field therefore loaded on the first boot, when the key was missing and the field default was written, and threw on every later boot and on every reload (#531, gate-1 CR-01).

      Only the JLS 5.1.2 widening primitive conversions are applied, so a boxed field accepts exactly what its primitive accepts: Short from Byte; Integer from Byte/Short; Long from Byte/Short/Integer; Float from any integral value; Double from any integral value or a Float. Anything else -- a narrowing conversion, a non-numeric value, a field that is not a numeric wrapper -- is returned unchanged, so Field.set refuses it exactly as before.

      Parameters:
      fieldType - the declared type of the target field
      value - the parsed value
      Returns:
      value widened to fieldType, or value itself
    • boxed

      private static Class<?> boxed(Class<?> type)
    • legacyInput

      private static Object legacyInput(Object plain, ConverterRegistry.Context ctx) throws ConversionException
      Throws:
      ConversionException