Class ReflectArgumentType<T>
- java.lang.Object
-
- net.sourceforge.argparse4j.impl.type.ReflectArgumentType<T>
-
- All Implemented Interfaces:
ArgumentType<T>
public class ReflectArgumentType<T> extends java.lang.Object implements ArgumentType<T>
This implementation converts String value into given type using type's
valueOf(java.lang.String)
static method or its constructor.
-
-
Constructor Summary
Constructors Constructor Description ReflectArgumentType(java.lang.Class<T> type)
CreatesReflectArgumentType
object with giventype
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
convert(ArgumentParser parser, Argument arg, java.lang.String value)
Convertsvalue
to appropriate type.
-
-
-
Constructor Detail
-
ReflectArgumentType
public ReflectArgumentType(java.lang.Class<T> type)
Creates
ReflectArgumentType
object 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 giventype
for conversion.valueOf()
method and/or constructor must be declared as public. Otherwise, they cannot be invoked. The constructor oftype
must accept 1 String argument.If error occurred inside the
valueOf
static method or constructor,ArgumentParserException
will be thrown. If error occurred in other locations, subclass ofRuntimeException
will 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...)
.- 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:ArgumentType
Converts
value
to appropriate type.If the objects derived from
RuntimeException
are thrown in conversion because of invalid input from command line, subclass must catch these exceptions and wrap them inArgumentParserException
and give simple error message to explain what happened briefly.- Specified by:
convert
in interfaceArgumentType<T>
- Parameters:
parser
- The aprser.arg
- The argument this type attached to.value
- The attribute value.- Returns:
- Converted object.
- Throws:
ArgumentParserException
- If conversion fails.
-
-