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

Custom IParameterConsumer causes native-image to fail #803

Closed
byteit101 opened this issue Sep 5, 2019 · 11 comments · Fixed by #804
Closed

Custom IParameterConsumer causes native-image to fail #803

byteit101 opened this issue Sep 5, 2019 · 11 comments · Fixed by #804
Labels
theme: codegen An issue or change related to the picocli-codegen module type: bug 🐛
Milestone

Comments

@byteit101
Copy link

byteit101 commented Sep 5, 2019

Using a file with a custom parameter consumer:

import picocli.CommandLine.*;
import picocli.CommandLine.Model.*;
import java.util.Stack;
import java.util.concurrent.Callable;
import picocli.CommandLine;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0", description = "Prints the checksum (MD5 by default) of a file to STDOUT.")
public class Boom implements Callable<Integer> {
	@Parameters(index = "0", description = "Ignored", parameterConsumer = LocalPC.class)
	private String file;
	public static void main(String... args) {
		System.exit(new CommandLine(new Boom()).execute(args));
	}
	@Override public Integer call() throws Exception {return 0;}
}
class LocalPC implements IParameterConsumer {
	@Override public void consumeParameters(Stack<String> args, ArgSpec argSpec, CommandSpec commandSpec) {return;}
}
$ java -cp boom.jar:picocli-codegen-4.0.3.jar picocli.codegen.aot.graalvm.ReflectionConfigGenerator Boom  > test.json
$ native-image -jar boom.jar -H:ReflectionConfigurationFiles=test.json -H:+ReportUnsupportedElementsAtRuntime --no-server
$ ./boom --help                                                                                                                                          ] EP_BAD 4:54 PM
Exception in thread "main" picocli.CommandLine$InitializationException: Could not instantiate class LocalPC: java.lang.NoSuchMethodException: LocalPC.<init>
	at picocli.CommandLine$DefaultFactory.create(CommandLine.java:4749)
	at picocli.CommandLine$DefaultFactory.createParameterConsumer(CommandLine.java:4745)
	at picocli.CommandLine$Model$ArgSpec$Builder.<init>(CommandLine.java:7662)
	at picocli.CommandLine$Model$PositionalParamSpec$Builder.<init>(CommandLine.java:8271)
	at picocli.CommandLine$Model$PositionalParamSpec$Builder.<init>(CommandLine.java:8261)
	at picocli.CommandLine$Model$PositionalParamSpec.builder(CommandLine.java:8220)
	at picocli.CommandLine$Model$CommandReflection.buildArgForMember(CommandLine.java:9773)
	at picocli.CommandLine$Model$CommandReflection.initFromAnnotatedTypedMembers(CommandLine.java:9688)
	at picocli.CommandLine$Model$CommandReflection.initFromAnnotatedFields(CommandLine.java:9641)
	at picocli.CommandLine$Model$CommandReflection.extractCommandSpec(CommandLine.java:9546)
	at picocli.CommandLine$Model$CommandSpec.forAnnotatedObject(CommandLine.java:5118)
	at picocli.CommandLine.<init>(CommandLine.java:223)
	at picocli.CommandLine.<init>(CommandLine.java:196)
	at Boom.main(Boom.java:14)
Caused by: java.lang.NoSuchMethodException: LocalPC.<init>
	at java.lang.Class.getConstructor0(DynamicHub.java:3082)
	at java.lang.Class.getDeclaredConstructor(DynamicHub.java:2178)
	at picocli.CommandLine$DefaultFactory.create(CommandLine.java:4725)
	at picocli.CommandLine$DefaultFactory.create(CommandLine.java:4748)
	... 13 more

The issue seems to be that the LocalPC class is not put into the reflection output by the ReflectionConfigGenerator, and manually putting it in I get it to build and show help successfully

@remkop remkop added type: bug 🐛 theme: codegen An issue or change related to the picocli-codegen module labels Sep 5, 2019
@remkop
Copy link
Owner

remkop commented Sep 6, 2019

Thank you for raising this issue and for the pull request!
I merged the PR but it would be good to add a test for this in picocli-annotation-processing-tests. Do you think you’ll be able to provide this?

@remkop remkop reopened this Sep 6, 2019
byteit101 pushed a commit to byteit101/picocli that referenced this issue Sep 6, 2019
@byteit101
Copy link
Author

I added a commit to my branch that should, in theory, test for this fix, but I can't seem to get it to work, and I'm not sure if it's my weak gradle skills, or what.

@remkop
Copy link
Owner

remkop commented Sep 6, 2019

@byteit101, it could be because I already merged that PR. Maybe create another branch?

@remkop
Copy link
Owner

remkop commented Sep 6, 2019

I might have misread. Did you mean you have trouble getting the test to work? If you can share the code we can look at it together...

@byteit101
Copy link
Author

Yes, the latter. I can't seem to get them to work. Works fine when I do a command line generation, but through gradle test it fails. Code is byteit101@1e7b2f5 and I'm kinda stuck. Looks to me like it should work, but I'm clearly missing something, either in gradle (gradle n00b here), or in your test framework, or something else.

@remkop
Copy link
Owner

remkop commented Sep 7, 2019

Code looks fine to me too. Can you put it in a pull request?

byteit101 pushed a commit to byteit101/picocli that referenced this issue Sep 8, 2019
remkop pushed a commit that referenced this issue Sep 8, 2019
@remkop remkop added this to the 4.0.4 milestone Sep 8, 2019
remkop added a commit that referenced this issue Sep 8, 2019
…ustom `IParameterConsumer` caused native-image to fail. Added tests.
@remkop
Copy link
Owner

remkop commented Sep 8, 2019

I went ahead and released picocli 4.0.4 with your PR and the additional changes to the annotation processor to correctly handle parameterConsumer attribute in the @Option or @Parameters annotations.

Please verify that this solves the issue.
Thanks again for the contribution!

@remkop remkop closed this as completed Sep 8, 2019
@pedrolamarao
Copy link

This happens to my code with picocli 4.6.1 running on OpenJDK 11.

@remkop
Copy link
Owner

remkop commented Jan 9, 2021

Hi @pedrolamarao
Thanks for raising this.
Can you open a new ticket with some code to reproduce the issue? That would help me a lot!

@pedrolamarao
Copy link

I'll see to it next week. We discovered that this happens only if the parameter consumer is an inner non static class. An inner static class works. (If this is a known limitation, we totally missed it from the documentation.)

@remkop
Copy link
Owner

remkop commented Jan 9, 2021

@pedrolamarao Thank you for the clarification.

Yes, that is a know limitation (for more details, see this ticket: #1297),
and perhaps this is an opportunity to improve the documentation! 😄
Any suggestions (or even better, PRs) would be much appreciated.

Thanks again for raising this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: codegen An issue or change related to the picocli-codegen module type: bug 🐛
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants