Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoComplete fails if using a custom IFactory implementation even if provided with --factory #601

Closed
quinn-harper opened this issue Jan 14, 2019 · 2 comments · Fixed by #602
Labels
theme: auto-completion An issue or change related to auto-completion type: enhancement ✨
Milestone

Comments

@quinn-harper
Copy link
Contributor

Our structure is as follows:

Class (command)

- Main.class (main)
  -- Sub.class (main sub)
    --- Sub$Command1.class (main sub com1)
    --- Sub$Command2.class (main sub com2)

All subcommands are declared in the @Command annotation.

When running the AutoComplete command to generate the file we get a stack trace with error:

Cannot instantiate subcommand package.Sub$Command1: the class has no constructor
Caused by: java.lang.NoSuchMethodException:package.Sub$Command1.<init>()
  • This class has one constructor with parameters annotated with @Inject.
  • We use our own implementation of IFactory we've passed to the command with --factory.
  • The Main.class is the command we're trying to generate the autocompletion script for.

It seems like the problem is that the CommandLine implementation that is created by the AutoComplete command does not use the factory passed to it.

See:

IFactory factory = CommandLine.defaultFactory();
if (factoryClass != null) {
factory = (IFactory) factory.create(Class.forName(factoryClass));
}
Class<?> cls = Class.forName(commandLineFQCN);
Object instance = factory.create(cls);
CommandLine commandLine = new CommandLine(instance);

I've built picocli locally changing this line:

-CommandLine commandLine = new CommandLine(instance);
+CommandLine commandLine = new CommandLine(instance, factory);

This resolved the issue for us and I will create a pull request for it soon. Let me know if I'm missing any side effects this change may produce that require testing.

@remkop
Copy link
Owner

remkop commented Jan 14, 2019

Good catch!

@remkop
Copy link
Owner

remkop commented Jan 14, 2019

The PR has been merged. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: auto-completion An issue or change related to auto-completion type: enhancement ✨
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants