Fixed long-standing bug reported by Mauricio where @Arguments assigned to
primitive types were properly validated and throw the proper MissingArgumentValue UserException. Before this fix, the error reported was the infamous DePristo BSOD (Could not create module String because an exception of type NullPointerException occurred caused by exception null). Thanks Mauricio! git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@4980 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
parent
6d855041ec
commit
edebbb5aa0
|
|
@ -299,6 +299,7 @@ class SimpleArgumentTypeDescriptor extends ArgumentTypeDescriptor {
|
|||
public Object parse(ParsingEngine parsingEngine, ArgumentSource source, Class type, ArgumentMatches matches) {
|
||||
if (source.isFlag())
|
||||
return true;
|
||||
|
||||
ArgumentDefinition defaultDefinition = createDefaultArgumentDefinition(source);
|
||||
String value = getArgumentValue( defaultDefinition, matches );
|
||||
Object result;
|
||||
|
|
@ -308,6 +309,8 @@ class SimpleArgumentTypeDescriptor extends ArgumentTypeDescriptor {
|
|||
try {
|
||||
if (type.isPrimitive()) {
|
||||
Method valueOf = primitiveToWrapperMap.get(type).getMethod("valueOf",String.class);
|
||||
if(value == null)
|
||||
throw new MissingArgumentValueException(createDefaultArgumentDefinition(source));
|
||||
result = valueOf.invoke(null,value.trim());
|
||||
} else if (type.isEnum()) {
|
||||
Object[] vals = type.getEnumConstants();
|
||||
|
|
|
|||
|
|
@ -125,6 +125,20 @@ public class ParsingEngineUnitTest extends BaseTest {
|
|||
Assert.assertEquals(argProvider.foo, 5, "Argument is not correctly initialized");
|
||||
}
|
||||
|
||||
@Test(expectedExceptions=MissingArgumentValueException.class)
|
||||
public void primitiveArgumentNoValueTest() {
|
||||
final String[] commandLine = new String[] {"--foo"};
|
||||
|
||||
parsingEngine.addArgumentSource( PrimitiveArgProvider.class );
|
||||
parsingEngine.parse( commandLine );
|
||||
parsingEngine.validate();
|
||||
|
||||
PrimitiveArgProvider argProvider = new PrimitiveArgProvider();
|
||||
parsingEngine.loadArgumentsIntoObject( argProvider );
|
||||
|
||||
Assert.assertEquals(argProvider.foo, 5, "Argument is not correctly initialized");
|
||||
}
|
||||
|
||||
private class PrimitiveArgProvider {
|
||||
@Argument(doc="simple integer")
|
||||
int foo;
|
||||
|
|
|
|||
Loading…
Reference in New Issue