pombreda / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 1 forks source link

PrivateModule bindings conflict only if one of them is exposed #789

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Description of the issue:

Take a look at the following code. As it is it throws an error saying A binding 
to java.lang.String annotated with @com.google.inject.name.Named(value=string) 
was already configured at 
TestPrivateModules$PM1.configure(TestPrivateModules.jav
a:17)

If I don't expose the binding everything works fine. I'd expect it to work with 
only one instance of the binding exposed.

import com.google.inject.Guice;
import com.google.inject.Key;
import com.google.inject.PrivateModule;
import com.google.inject.name.Names;

public class TestPrivateModules {
  public static class PM1 extends PrivateModule {
    @Override
    public void configure() {
      bindConstant().annotatedWith(Names.named("string")).to("foo");
      // If I comment out this expose() everything runs fine
      expose(Key.get(String.class, Names.named("string")));
    }
  }

  public static class PM2 extends PrivateModule {
    @Override
    public void configure() {
      bindConstant().annotatedWith(Names.named("string")).to("bar");
    }
  }

  public static void main(String[] args) {
    Guice.createInjector(new PM1(), new PM2());
    System.out.println("ok");
  }
}

Original issue reported on code.google.com by muntea...@gmail.com on 4 Dec 2013 at 8:18

GoogleCodeExporter commented 9 years ago
This is WAI.  You have two bindings for 
  @Named("string") String
in PM1 it's bound to "foo" and in PM2 it's bound to "bar".

If you don't expose the binding, it is OK because everything is private and 
nothing conflicts.  Once you expose one of them, it prevents the other from 
being private because "exposing" means that the parents & other children can 
see & use it, and PM2 now would have two bindings for @Named("string") String, 
which isn't OK.

Original comment by sberlin on 4 Dec 2013 at 9:33