Skip to content

Reflection

Jonathan edited this page Oct 25, 2020 · 3 revisions

KWCommands provides an Annotation System which enables Commands through Reflection, lets try:

public class KwDocsReflect {

    public static void main(String[] args) {

        KwDocsReflect kwDocsReflect = new KwDocsReflect();
        AIO aio = new AIO(kwDocsReflect)
                .loadObj(kwDocsReflect)
                .registerLoaded();

        HelpInfoHandler helpInfoHandler = new CommonHelpInfoHandler();

        CommandProcessor processor = aio.getProcessor();
        Either<ParseFail, List<CommandResult>> result = processor.parseAndDispatch("random", null, InformationProvidersVoid.INSTANCE);

        result.ifRight(commandResults -> {
            helpInfoHandler.handleResults(commandResults, Printers.INSTANCE.getSysOut());
        });
        result.ifLeft(parseFail -> {
            helpInfoHandler.handleFail(parseFail, Printers.INSTANCE.getSysOut());
        });
    }

    @Cmd
    public int random() {
        return ThreadLocalRandom.current().nextInt();
    }

}

This prints:

-------- Commands --------

--------   Label  --------
 [ ]  = Required argument
 < >  = Optional argument
  !   = Dynamic argument (depends on others arguments)
  ->  = Main command
 -'>  = Sub command
  -   = Description
 - x: = Description of argument x
  *   = Information Requirement
  **  = Requirement
--------   Label  --------

Value result of 'command random': 1197122789

-------- Commands --------

You could also register arguments using @Arg annotation:

public class KwDocsReflect {

    public static void main(String[] args) {

        KwDocsReflect kwDocsReflect = new KwDocsReflect();
        AIO aio = new AIO(kwDocsReflect)
                .loadObj(kwDocsReflect)
                .registerLoaded();

        HelpInfoHandler helpInfoHandler = new CommonHelpInfoHandler();

        CommandProcessor processor = aio.getProcessor();
        Either<ParseFail, List<CommandResult>> result = processor.parseAndDispatch("calculate 10 9", null, InformationProvidersVoid.INSTANCE);

        result.ifRight(commandResults -> {
            helpInfoHandler.handleResults(commandResults, Printers.INSTANCE.getSysOut());
        });
        result.ifLeft(parseFail -> {
            helpInfoHandler.handleFail(parseFail, Printers.INSTANCE.getSysOut());
        });
    }

    @Cmd
    public int calculate(@Arg("a") int a, @Arg("b") int b) {
        return a + b;
    }

}

Result:

-------- Commands --------

--------   Label  --------
 [ ]  = Required argument
 < >  = Optional argument
  !   = Dynamic argument (depends on others arguments)
  ->  = Main command
 -'>  = Sub command
  -   = Description
 - x: = Description of argument x
  *   = Information Requirement
  **  = Requirement
--------   Label  --------

Value result of 'command calculate': 19

-------- Commands --------

Info and Requirement

Reflection API supports both Information and Requirement through @Info annotation and @Require annotation respectively.

@Info could only be used in parameters of functions (just like @Arg, but @Arg could be applied to properties too).

@Require applies to both, function and parameters of function.

Clone this wiki locally