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

Support binding Module in parent modules for installation in child modules #795

Open
gissuebot opened this issue Jul 7, 2014 · 1 comment
Labels

Comments

@gissuebot
Copy link

From b.k.oxley on February 04, 2014 18:37:42

"1) Binding to core guice framework type is not allowed: Module."

I wrote hm.binkley:service-binder (on Maven Central) with the goal of injecting modules for child injectors.  Essentially it a version of ServiceLoader which can inject loaded instances including non-default constructors.

They key code is:

// service - Class<T> of type to bind ala ServiceLoader.load()
// implemntations - Entries in META-INF/services for the service
// Fails when T == Module
final Multibinder<T> bindings = newSetBinder(binder, service);
for (final Class<? extends T> implementation : implementations)
    bindings.addBinding().to(implementation); // Fails here

For simple modules without dependencies I can use:

@Override
protected void configure() {
    for (Module module : ServiceLoader.load(Module.class))
        install(module);
}

When I use ServiceBinder to get injected modules (modules with non-default constructors):

public static final class ServiceBinderParentModule extends AbstractModule {
    @Override
    protected void configure() {
        ServiceBinder.with(binder()).bind(Module.class); // Fails here
     }
}

public static final class ServiceBinderChildModule extends AbstractModule {
    private final Injector guice;

    @Inject
    public ServiceBinderChildModule(@Nonnull final Injector guice) {
        this.guice = guice;
    }

    @Override
    protected void configure() {
        for (final Module module : guice.getInstance(Key.get(new TypeLiteral<Set<Module>>() {})))
            install(module);
    }
}

Use:

parent = createInjector(new ServiceBinderParentModule()); // Fails here
child = parent.createChildInjector(parent.getInstance(ServiceBinderChildModule.class));

I've pushed a public branch with sources and tests demonstrating the problem (tests marked @Ignore("Guice cannot bind Module")): https://github.com/binkley/service-binder/tree/feature/GuiceModules ENHANCEMENT REQUEST - I'd like to support this idiom.  My goal as to use DI on modules.  The dependencies go into the parent module, the child module shows up with injected modules nicely installed.

Use case is supporting modules as components.  Developers drop a jar into their project; the jar contains modules declared in META-INF/services/com.google.inject.Module; their main() includes the two lines of code directly above: it all "just works".

Original issue: http://code.google.com/p/google-guice/issues/detail?id=795

@gissuebot
Copy link
Author

From b.k.oxley on February 05, 2014 01:29:36

I tried the easy thing of cloning git 4.0-SNAPSHOT, and commenting out Module.class and AbstractModule.class from FORBIDDEN_TYPES in AbstractBindingProcessor.  My tests for Guice all pass with that change.

I don't now the reason for the restriction, unsure what havoc this change might wreak.

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

No branches or pull requests

1 participant