chinshaw / google-gin

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

Allow bindings to be overridden #88

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create FooModule with some bindings, including bind(IFoo.class).to(Foo.class)
2. Create BarModule with some bindings, including bin(IFoo.class).to(Bar.class)
3. Create SomeInjector using @GinModules( { FooModule.class, BarModule.class } )

What is the expected output? What do you see instead?
Expected the binding in BarModule for IFoo.class to override the existing 
binding for IFoo in 
FooModule. Instead, I receive an error about the key being double bound.

What version of the product are you using? On what operating system?
1.0

Please provide any additional information below.
The error is coming from com.google.gwt.inject.rebind.BindingsProcessor, line 
561:
http://www.google.com/codesearch/p?
hl=en#d7yaITFH7NQ/trunk/src/com/google/gwt/inject/rebind/BindingsProcessor.java&
q=Doub
le-bound%20package:http://google-gin%5C.googlecode%5C.com&sa=N&cd=1&ct=rc&l=561

I would like to see modules declared later be allowed to override the existing 
bindings. I believe 
that is the behavior of Guice, so I am unsure why Gin differs in that regard.

Original issue reported on code.google.com by jarrod.c...@gmail.com on 15 Mar 2010 at 1:49

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
The following patch successfully passes all unit tests:

Index: src/com/google/gwt/inject/rebind/BindingsProcessor.java
===================================================================
--- src/com/google/gwt/inject/rebind/BindingsProcessor.java (revision 135)
+++ src/com/google/gwt/inject/rebind/BindingsProcessor.java (working copy)
@@ -566,10 +566,6 @@
   }

   private void addBinding(Key<?> key, Binding binding) {
-    if (bindings.containsKey(key)) {
-      logError("Double-bound: " + key + ". " + bindings.get(key) + ", " + 
binding);
-      return;
-    }

     JClassType classType = keyUtil.getRawClassType(key);
     if (classType != null && !isClassAccessibleFromGinjector(classType)) {

Original comment by jarrod.c...@gmail.com on 15 Mar 2010 at 1:59

GoogleCodeExporter commented 8 years ago
Guice does not allow duplicate bindings. There is no ordering between modules, 
so 
there would be no way to resolve the duplication.

Guice does support Modules.override as a way to override some bindings. That 
would 
be a fine feature request for Gin (and I think it's filed).

Original comment by bstoler+code@google.com on 15 Mar 2010 at 3:32

GoogleCodeExporter commented 8 years ago
And I'd add that for "default bindings" you can annotate your interfaces and 
classes 
with @ImplementedBy or @ProvidedBy, and these are "JIT bindings" so they can be 
overridden by "explicit bindings".

Original comment by t.broyer on 15 Mar 2010 at 9:53

GoogleCodeExporter commented 8 years ago
Well, okay, maybe I didn't read the Guice documentation clearly enough or I 
didn't
understand it well enough.

In either case, unless I'm missing something, this still doesn't provide me a 
way to
bind my singletons eagerly. Meaning, there is no @Singleton(eager = true), or
@ImplementedBy(eager = true).

So if I want my singleton binding to be eager, I have to explicitly bind it in a
module. And that means then that I can't provide default implementations left 
for
other modules to override.

Or maybe I'm just going about it all wrong?

Original comment by jarrod.c...@gmail.com on 20 Mar 2010 at 6:27