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

Ask IFactory for implementation before creating Proxy for interface #404

Closed
remkop opened this issue Jul 5, 2018 · 1 comment
Closed

Comments

@remkop
Copy link
Owner

remkop commented Jul 5, 2018

CommandReflection currently creates a java.lang.reflect.Proxy unconditionally when an interface class is passed in. It should ask the IFactory for an implementation first.

@remkop remkop added this to the 3.2 milestone Jul 5, 2018
@remkop
Copy link
Owner Author

remkop commented Jul 5, 2018

Dependency Injection example.

Guice-based IFactory:

import com.google.inject.*;
import picocli.CommandLine.IFactory;

public class GuiceFactory implements IFactory {
    private final Injector injector = Guice.createInjector(new DemoModule());

    @Override
    public <K> K create(Class<K> aClass) throws Exception {
        return injector.getInstance(aClass);
    }

    static class DemoModule extends AbstractModule {
        @Override
        protected void configure() {
            bind(java.util.List.class).to(java.util.LinkedList.class);
            bind(Runnable.class).to(InjectionDemo.class);
        }
    }
}

Picocli command with injected resource:

import picocli.CommandLine;
import javax.inject.Inject;

@CommandLine.Command(name = "di-demo")
public class InjectionDemo implements Runnable {
    @Inject java.util.List list;

    @CommandLine.Option(names = "-x") int x;

    public static void main(String[] args) {
        CommandLine.run(Runnable.class, new GuiceFactory(), args);
    }

    @Override
    public void run() {
        assert list instanceof java.util.LinkedList;
    }
}

@remkop remkop closed this as completed in 17fe31a Jul 5, 2018
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