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

Allow mixins to get a reference to the command they are mixed into #958

Closed
remkop opened this issue Feb 16, 2020 · 1 comment
Closed

Allow mixins to get a reference to the command they are mixed into #958

remkop opened this issue Feb 16, 2020 · 1 comment

Comments

@remkop
Copy link
Owner

remkop commented Feb 16, 2020

Allow mixins to get a reference to the command they are injected into.

Currently, having a @Spec-annotated field in a Mixin will result in the CommandSpec of the mixin being injected instead of the CommandSpec of the target command (where the mixin is mixed into).

This makes it impossible to climb the command hierarchy in a generic way in a mixin.

A possible solution is something like this:

public @interface Spec {
    enum Target { SELF, MIXEE}

    Target value() default Target.SELF;
}

This would allow for usage like this:

static class VerbosityMixin {
    @Spec CommandSpec mySpec; // spec of the VerbosityMixin itself (not useful)

    @Spec(Spec.Target.MIXEE)
    CommandSpec mixeeSpec; // spec of the command where the @Mixin is used

    /**
     * Delegates the specified verbosity to the top-level (root) command.
     * @param verbosity the new verbosity value
     */
    @Option(names = {"-v", "--verbose"})
    public void setVerbose(boolean[] verbosity) {
        CommandSpec root = mixeeSpec.root(); // #960

        MyApp myApp = root.commandLine().getCommand();
        myApp.verbosity = verbosity;
    }
}

(I created a separate ticket, #960, for getting the root (top-level) command from a command spec.)

Update: a full-blown example is here.

@remkop remkop added this to the 4.3 milestone Feb 16, 2020
remkop added a commit that referenced this issue Feb 16, 2020
@remkop remkop changed the title Allow mixins to get a reference to the command they are injected into Allow mixins to get a reference to the command they are mixed into Feb 17, 2020
remkop added a commit that referenced this issue Feb 17, 2020
remkop added a commit that referenced this issue Feb 18, 2020
* LoggingMixin is now a self-contained, reusable mixin that implements a cross-cutting "log level configuration" concern
* Moved all Log4j2 configuration code into LoggingMixin
* Removed hard-coded dependency on MyApp; instead top-level command must implement LoggingMixin.IOwner
* Simplified bootstrap by setting LoggingMixin::executionStrategy as the execution strategy
* All commands (including the top-level command) now simply use `@Mixin LoggingMixin x` as needed
remkop added a commit that referenced this issue Feb 20, 2020
remkop added a commit that referenced this issue Feb 23, 2020
remkop added a commit that referenced this issue Feb 24, 2020
remkop added a commit that referenced this issue Feb 24, 2020
@remkop
Copy link
Owner Author

remkop commented Feb 25, 2020

This is now implemented and documented in master.
Closing this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant