Class DefaultTypeConverter

java.lang.Object
com.github.rvesse.airline.types.DefaultTypeConverterProvider
com.github.rvesse.airline.types.DefaultTypeConverter
All Implemented Interfaces:
TypeConverter, TypeConverterProvider
Direct Known Subclasses:
ExtendedTypeConverter

public class DefaultTypeConverter extends DefaultTypeConverterProvider implements TypeConverter
The default type converter

This converter supports all the basic Java types plus types. Additionally it supports any class that defines a static fromString(String) or valueOf(String) method. Finally it supports any class that defines a constructor that takes a string.

  • Field Details

  • Constructor Details

    • DefaultTypeConverter

      public DefaultTypeConverter()
      Creates a new instance of the default type converter with the default NumericTypeConverter configured
    • DefaultTypeConverter

      public DefaultTypeConverter(NumericTypeConverter numericConverter)
      Creates a new instance of the default type converter
      Parameters:
      numericConverter - Numeric type converter to use, if null then DefaultNumericConverter is used
  • Method Details

    • convert

      public Object convert(String name, Class<?> type, String value)
      Description copied from interface: TypeConverter
      Convert a string value into an appropriately typed value
      Specified by:
      convert in interface TypeConverter
      Parameters:
      name - Option Name
      type - Target Type
      value - String Value
      Returns:
      Typed value
    • checkArguments

      public static void checkArguments(String name, Class<?> type, String value)
      Checks that the arguments are all non-null
      Parameters:
      name - Option/Argument name
      type - Target type
      value - String to convert
    • tryConvertStringConstructor

      protected final ConvertResult tryConvertStringConstructor(String name, Class<?> type, String value)
      Tries to convert the value by invoking a constructor that takes a string on the type

      Considers two variants of the constructor, one which takes String as its parameter type and if that doesn't exist one that takes CharSequence as it's parameter type

      Parameters:
      type - Type
      value - value
      Returns:
      Conversion result
    • tryConvertConstructor

      protected final <T> ConvertResult tryConvertConstructor(String name, Class<?> type, String value, Class<T> parameterType)
      Tries to convert the value by invoking a constructor that takes a string on the type
      Parameters:
      type - Type
      value - value
      Returns:
      Conversion result
    • tryConvertFromValueOf

      protected final ConvertResult tryConvertFromValueOf(String name, Class<?> type, String value)
      Tries to convert the value by invoking a static valueOf(String) method on the type
      Parameters:
      type - Type
      value - Value
      Returns:
      Conversion result
    • tryConvertFromString

      protected final ConvertResult tryConvertFromString(String name, Class<?> type, String value)
      Tries to convert the value by invoking a static fromString(String) method on the type
      Parameters:
      type - Type
      value - Value
      Returns:
      Conversion result
    • tryConvertStringMethod

      protected final ConvertResult tryConvertStringMethod(String name, Class<?> type, String value, String methodName)
      Tries to convert the value by invoking a static method on the

      Considers two variants of the method, one which takes String as its parameter type and if that doesn't exist one that takes CharSequence as it's parameter type

      Parameters:
      type - Type
      value - Value
      methodName - Name of the method to invoke
      Returns:
      Conversion Result
    • tryConvertStringMethod

      protected final ConvertResult tryConvertStringMethod(Class<?> type, String value, String methodName, Class<?> parameterType)
      Tries to convert the value by invoking a static method on the type
      Parameters:
      type - Type
      value - Value
      methodName - Name of the method to invoke
      Returns:
      Conversion Result
    • tryConvertBasicTypes

      protected final ConvertResult tryConvertBasicTypes(String name, Class<?> type, String value)
      Tries to convert the value if it is one of the common Java types
      Parameters:
      type - Type
      value - Value
      Returns:
      Conversion result
    • setNumericConverter

      public void setNumericConverter(NumericTypeConverter converter)
      Description copied from interface: TypeConverter
      Sets the numeric type converter to use, if null then default behaviour of the type converter instance (whatever that might be) will be used
      Specified by:
      setNumericConverter in interface TypeConverter
      Parameters:
      converter - Numeric type converter