google-code-export / google-guice

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

cannot override guice 2.0 provider method #347

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
See the code below.  Eventhough you can successfully override a provider
method with no args, overriding provider methods with more then one arg
will fail.

package net.meat;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import org.testng.annotations.Test;

import java.net.InetSocketAddress;

/**
 *
 * @author Adrian Cole
 */
public class TestImNotCrazy {

   @Inject
   InetSocketAddress me;

   @Test
   void testCannotOverrideProvides() {
      Injector i = Guice.createInjector(new Module2());
      TestImNotCrazy crazy = i.getInstance(TestImNotCrazy.class);

   }

   static class Module1 extends AbstractModule {
      protected void configure() {
         // lala
      }

      @Provides
      @Singleton
      InetSocketAddress provideMeASocket(String meat) {
         return new InetSocketAddress("localhost", 80);
      }
   }

   static class Module2 extends Module1 {
      protected void configure() {
         // lala
      }

      @Provides
      @Singleton
      @Override
      InetSocketAddress provideMeASocket(String meat) {
         return new InetSocketAddress("localhost", 80);
      }
   }
}

Original issue reported on code.google.com by fernc...@gmail.com on 12 Mar 2009 at 11:17

GoogleCodeExporter commented 9 years ago
Do you really need to do this?

I'm not sure that erroring out isn't the correct behavior here.

Original comment by dha...@gmail.com on 13 Mar 2009 at 2:52

GoogleCodeExporter commented 9 years ago
I've gotten around it by using Modules.override...

That said, I would expect a subclass to be able to mask its parent's
behavior.  Maybe I am missing something, but I'd expect @Override to prevent 
parent
behaviour.  What do you think?

-Adrian

Original comment by fernc...@gmail.com on 13 Mar 2009 at 3:12

GoogleCodeExporter commented 9 years ago
Another workaround is to not apply @Provides to the overriding method. We'll 
fix this...

Original comment by limpbizkit on 13 Mar 2009 at 3:28

GoogleCodeExporter commented 9 years ago
the workaround is valid.  thanks!

Original comment by fernc...@gmail.com on 16 Mar 2009 at 3:47

GoogleCodeExporter commented 9 years ago
Issue 447 has been merged into this issue.

Original comment by sberlin on 20 Feb 2011 at 12:53