Skip to content

Commit

Permalink
[#572] CommandSpec.addMethodSubcommands now throws `picocli.Command…
Browse files Browse the repository at this point in the history
…Line.InitializationException` instead of `java.lang.UnsupportedOperationException` when the user object of the parent command is a `java.lang.reflect.Method`.

Closes #572
  • Loading branch information
remkop committed Dec 20, 2018
1 parent d984dfa commit 00bd0da
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ This ordering may be modified with the `CommandLine::setHelpSectionKeys` setter
- [#530] Usage message customization. Thanks to [stechio](https://github.com/stechio) for raising the request and productive discussions.
- [#570] Command method options and positional parameter Object values are now cleared correctly when reusing CommandLine. Thanks to [Christian Helmer](https://github.com/SysLord) for the pull request.
- [#569] Facilitate customization of the synopsis: split `Help.detailedSynopsis()` into protected methods.
- [#572] `CommandSpec.addMethodSubcommands` now throws `picocli.CommandLine.InitializationException` instead of `java.lang.UnsupportedOperationException` when the user object of the parent command is a `java.lang.reflect.Method`.

## <a name="3.9.0-deprecated"></a> Deprecations
No features were deprecated in this release.

## <a name="3.9.0-breaking-changes"></a> Potential breaking changes
This is release has no breaking changes.
`CommandSpec.addMethodSubcommands` now throws `InitializationException` instead of `java.lang.UnsupportedOperationException` when the user object of the parent command is a `java.lang.reflect.Method`.


# <a name="3.8.2"></a> Picocli 3.8.2
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -3848,7 +3848,7 @@ private void initResourceBundle(ResourceBundle bundle) {
*/
public CommandSpec addMethodSubcommands(IFactory factory) {
if (userObject() instanceof Method) {
throw new UnsupportedOperationException("cannot discover methods of non-class: " + userObject());
throw new InitializationException("Cannot discover subcommand methods of this Command Method: " + userObject());
}
for (Method method : getCommandMethods(userObject().getClass(), null)) {
CommandLine cmd = new CommandLine(method, factory);
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/picocli/CommandLineCommandMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -989,4 +989,25 @@ public void testDuplicateCommandMethodNames() {
assertTrue(ex.getMessage().startsWith("Expected exactly one @Command-annotated method for "));
}
}

@Test
public void testAddMethodSubcommands() {
CommandSpec spec = CommandSpec.wrapWithoutInspection(new StaticMethodCommand(1));
assertEquals(0, spec.subcommands().size());

spec.addMethodSubcommands();
assertEquals(4, spec.subcommands().size());
}

@Test
public void testAddMethodSubcommands_DisallowedIfUserObjectIsMethod() throws Exception{
Method m = MethodApp.class.getDeclaredMethod("run1", int.class);
CommandSpec spec = CommandSpec.wrapWithoutInspection(m);

try {
spec.addMethodSubcommands();
} catch (InitializationException ex) {
assertEquals("Cannot discover subcommand methods of this Command Method: int picocli.CommandLineCommandMethodTest$MethodApp.run1(int)", ex.getMessage());
}
}
}

0 comments on commit 00bd0da

Please sign in to comment.