Skip to content

Commit

Permalink
Throw CommandLineParserInternalException for immutable collections
Browse files Browse the repository at this point in the history
  • Loading branch information
magicDGS committed Nov 30, 2017
1 parent 61857ed commit f42fa6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,17 @@ private void setArgument(ArgumentDefinition argumentDefinition, List<String> val
if (argumentDefinition.isCollection) {
@SuppressWarnings("rawtypes")
final Collection c = (Collection) argumentDefinition.getFieldValue();
if (value == null) {
//user specified this arg=null which is interpreted as empty list
c.clear();
} else {
c.add(value);
try {
if (value == null) {
//user specified this arg=null which is interpreted as empty list
c.clear();
} else {
c.add(value);
}
} catch (UnsupportedOperationException e) {
throw new CommandLineException.CommandLineParserInternalException(String.format("Collection arguments seems immutable: \"%s\". Initialized collection should support the clear and add methods, but %s does not.",
argumentDefinition.getLongName(),
c.getClass().getName()));
}
argumentDefinition.hasBeenSet = true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ public void testCollectionThatCannotBeAutoInitialized() {
new CommandLineArgumentParser(o);
}

class ImmutableCollectionArguments {
@Argument
public List<String> LIST = Collections.emptyList();
}

@Test(expectedExceptions = CommandLineException.CommandLineParserInternalException.class)
public void testCollectionThatIsImmmutable() {
final ImmutableCollectionArguments o = new ImmutableCollectionArguments();
new CommandLineArgumentParser(o).parseArguments(System.err, new String[]{"--LIST", "A"});
}

//////////////////////////////////////////////////////////////////
// Helper methods

Expand Down

0 comments on commit f42fa6f

Please sign in to comment.