Class ReflectArgumentType<T>
- java.lang.Object
-
- net.sourceforge.argparse4j.impl.type.ReflectArgumentType<T>
-
- All Implemented Interfaces:
ArgumentType<T>,MetavarInference
public class ReflectArgumentType<T> extends java.lang.Object implements ArgumentType<T>, MetavarInference
This implementation converts String value into given type using type's
valueOf(java.lang.String)static method or its constructor. This class implementsMetavarInferenceinterface, and performs special handling whenBooleanclass is passed to the constructor. In that case,inferMetavar()returns convenient metavar string for Boolean values, and it is used whenArgument.metavar(String...)is not used.
-
-
Constructor Summary
Constructors Constructor Description ReflectArgumentType(java.lang.Class<T> type)CreatesReflectArgumentTypeobject with giventype.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tconvert(ArgumentParser parser, Argument arg, java.lang.String value)Convertsvalueto appropriate type.private TconvertUsingConstructor(ArgumentParser parser, Argument arg, java.lang.String value)private voidhandleInstantiationError(java.lang.Exception e)java.lang.String[]inferMetavar()Infers metavar based on given type.private voidthrowArgumentParserException(ArgumentParser parser, Argument arg, java.lang.String value, java.lang.Throwable t)
-
-
-
Field Detail
-
type_
private final java.lang.Class<T> type_
-
-
Constructor Detail
-
ReflectArgumentType
public ReflectArgumentType(java.lang.Class<T> type)
Creates
ReflectArgumentTypeobject with giventype.This object first tries to convert given String using
valueOf(java.lang.String)static method of giventype. If that failed, then use constructor of giventypefor conversion.valueOf()method and/or constructor must be declared as public. Otherwise, they cannot be invoked. The constructor oftypemust accept 1 String argument.If error occurred inside the
valueOfstatic method or constructor,ArgumentParserExceptionwill be thrown. If error occurred in other locations, subclass ofRuntimeExceptionwill be thrown.This object works with enums as well. The enums in its nature have limited number of members. In
convert(ArgumentParser, Argument, String), string value will be converted to one of them. If it cannot be converted,convert(ArgumentParser, Argument, String)will throwArgumentParserException. This means it already act like aArgument.choices(Object...). Please note that this conversion does not take into accountEnum.toString()override. If application passes enums with toString() overridden with the different value than enum name, it may not work like it expects. To take into accountEnum.toString()on conversion, useArguments.enumStringType(Class)instead.- Parameters:
type- The type String value should be converted to.
-
-
Method Detail
-
convert
public T convert(ArgumentParser parser, Argument arg, java.lang.String value) throws ArgumentParserException
Description copied from interface:ArgumentTypeConverts
valueto appropriate type.If the objects derived from
RuntimeExceptionare thrown in conversion because of invalid input from command line, subclass must catch these exceptions and wrap them inArgumentParserExceptionand give simple error message to explain what happened briefly.- Specified by:
convertin interfaceArgumentType<T>- Parameters:
parser- The parser.arg- The argument this type attached to.value- The attribute value.- Returns:
- Converted object.
- Throws:
ArgumentParserException- If conversion fails.
-
convertUsingConstructor
private T convertUsingConstructor(ArgumentParser parser, Argument arg, java.lang.String value) throws ArgumentParserException
- Throws:
ArgumentParserException
-
throwArgumentParserException
private void throwArgumentParserException(ArgumentParser parser, Argument arg, java.lang.String value, java.lang.Throwable t) throws ArgumentParserException
- Throws:
ArgumentParserException
-
handleInstantiationError
private void handleInstantiationError(java.lang.Exception e)
-
inferMetavar
public java.lang.String[] inferMetavar()
Infers metavar based on given type.
If
Booleanclass is passed to constructor, this method returns metavar string "{true,false}" for convenience.If enum type is passed to constructor, this method returns metavar containing all enum names defined in that type. This uses
Enum.name()method, instead ofObject.toString()method. If you are looking for the latter, consider to useEnumStringArgumentType.Otherwise, returns null.
- Specified by:
inferMetavarin interfaceMetavarInference- Returns:
- inferred array of metavar strings, or null if there is no metavar inferred.
- Since:
- 0.7.0
- See Also:
MetavarInference.inferMetavar()
-
-