-
Notifications
You must be signed in to change notification settings - Fork 441
Description
Problem
When following the instructions at http://picocli.info/autocomplete.html to generate a bash completion script for a non-public @Command
class, picocli.AutoComplete
throws the following exception:
java.lang.IllegalAccessException: Class picocli.AutoComplete$App can not access a member of class bisq.cli.Bisq with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.Class.newInstance(Class.java:436)
at picocli.AutoComplete$App.run(AutoComplete.java:89)
at picocli.CommandLine.execute(CommandLine.java:775)
at picocli.CommandLine.access$700(CommandLine.java:139)
at picocli.CommandLine$RunLast.handle(CommandLine.java:998)
at picocli.CommandLine$RunLast.handle(CommandLine.java:966)
at picocli.CommandLine$AbstractParseResultHandler.handleParseResult(CommandLine.java:831)
at picocli.CommandLine.parseWithHandlers(CommandLine.java:1182)
at picocli.CommandLine.run(CommandLine.java:1544)
at picocli.CommandLine.run(CommandLine.java:1494)
at picocli.AutoComplete.main(AutoComplete.java:51)
This is because (a) bisq.cli.Bisq
is declared (intentionally) with package-private visibility, and (b) because picocli.AutoComplete$App.run
attempts to reflectively instantiate it using Class.newInstance()
, which respects declared visibility modifiers and provides no option to override them.
Solution
Instead of Class.newInstance()
, first get the class's no-arg Constructor
with Class.getConstructor()
, then call setAccessible(true)
on it followed by newInstance
. This will allow users to keep declaring their @Command
classes with whatever visibility they see fit.