Status: New
Owner: ----

New issue 752 by zorze...@gmail.com: Guice.createInjector enforces bindings that become unused by Modules.override
http://code.google.com/p/google-guice/issues/detail?id=752

See test case below. I have a module Foo with an @Provides (buildInt) method, and I Modules.override it with another Bar module that redeclares the same binding. Therefore, Foo.buildInt is a dead method -- it will never be executed. But, when the injector is created, it throws, because that method needs a param that is not bound.

The way I came across this issue is that I precisely wanted to override a binding in my test because that binding needs a class that is not available on these tests.

********************

import com.google.inject.AbstractModule;
import com.google.inject.CreationException;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.util.Modules;

import junit.framework.TestCase;

import org.junit.Assert;

import java.util.regex.Pattern;

public class OverrideBugTestCase extends TestCase {

  private static final class Foo extends AbstractModule {

    @Override
    protected void configure() {}

    @Provides
    public static Integer buildInt(Pattern foo) {
      return 1;
    }
  }

  private static final class Bar extends AbstractModule {

    @Override
    protected void configure() {}

    @Provides
    public static Integer buildInt() {
      return 2;
    }
  }

  public void testOverride() throws Exception {
    Module foo = new Foo();
    Module bar = Modules.override(foo).with(new Bar());

// This line is throwing! Even though Bar overrode Foo's Integer binding, // Guice still tries to fullfil all bindings needed by Foo.buildInt, which
    // include an unbound Pattern.
    Injector barInjector = Guice.createInjector(bar);

    Assert.assertEquals((Integer)2, barInjector.getInstance(Integer.class));

    try {
      Guice.createInjector(foo).getInstance(Integer.class);
      fail();
    } catch (CreationException e) {}
  }
}


--
You received this message because this project is configured to send all issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-guice-dev+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice-dev@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to