public class ReflectArgumentType<T> extends 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 implements MetavarInference
interface, and performs
special handling when Boolean
class is passed to the constructor. In
that case, inferMetavar()
returns convenient
metavar string for Boolean values, and it is used when
Argument.metavar(String...)
is not used.
Constructor and Description |
---|
ReflectArgumentType(Class<T> type)
Creates
ReflectArgumentType object with given type . |
Modifier and Type | Method and Description |
---|---|
T |
convert(ArgumentParser parser,
Argument arg,
String value)
Converts
value to appropriate type. |
String[] |
inferMetavar()
Infers metavar based on given type.
|
public ReflectArgumentType(Class<T> type)
Creates ReflectArgumentType
object with given type
.
This object first tries to convert given String using
valueOf(java.lang.String)
static method of given type
. If
that failed, then use constructor of given type
for conversion.
valueOf()
method and/or constructor must be declared as public.
Otherwise, they cannot be invoked. The constructor of type
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 of RuntimeException
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 throw
ArgumentParserException
. This means it already act like a
Argument.choices(Object...)
. Please note that this conversion
does not take into account Enum.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
account Enum.toString()
on conversion, use
Arguments.enumStringType(Class)
instead.
type
- The type String value should be converted to.public T convert(ArgumentParser parser, Argument arg, String value) throws ArgumentParserException
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 in ArgumentParserException
and give simple error message to explain what happened briefly.
convert
in interface ArgumentType<T>
parser
- The parser.arg
- The argument this type attached to.value
- The attribute value.ArgumentParserException
- If conversion fails.public String[] inferMetavar()
Infers metavar based on given type.
If Boolean
class 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 of Object.toString()
method.
If you are looking for the latter, consider to use
EnumStringArgumentType
.
Otherwise, returns null.
inferMetavar
in interface MetavarInference
MetavarInference.inferMetavar()
Copyright © 2012–2017. All rights reserved.