title: Guice won't intercept annotated methods
body: <p>I have a situation where Guice is working for some bindings, and not at all for others. Clearly I am using the API incorrectly.</p>
  <p>In part, it's probably because I'm trying to get too &quot;fancy&quot; with how I'm using Guice. I've created an inheritance tree of <code>Module</code>s and I think I've gotten too clever (or foolish!) for my own good.</p>
  <p>Before you look at the code below, just please understand my <em>intention</em>, which was to provide a reusable <code>Module</code> that I can place in a JAR and share across multiple projects. This abstract, reusable <code>Module</code> would provide so-called &quot;default bindings&quot; that any implementing <code>Module</code> would automatically honor. Things like an AOP <code>MethodInterceptor</code> called <code>Profiler</code>, which looks for methods annotated with <code>@Calibrated</code> and automatically logs how long it took for that method to execute, etc.</p>
  <p>Observe the following:</p>
  <pre><code>@Target({ ElementType.METHOD })
  @RetentionPolicy(RetentionPolicy.RUNTIME)
  @BindingAnnotation
  public @interface Calibrated{}
  
  public class Profiler implement MethodInterceptor {
      @Override
      public Object invoke(MethodInvocation arg0) throws Throwable {
          // Eventually it will calculate and log the amount of time
          // it takes an intercepted method to execute, hence &quot;Profiler&quot;.
          System.out.println(&quot;Intercepted!&quot;);
          return arg0.proceed();
      }
  }
  
  public abstract class BaseModule implements Module {
      private Binder binder;
  
      public BaseModule() {
          super();
      }
  
      public abstract void bindDependencies();
  
      @Override
      public void configure(Binder bind) {
          // Save the binder Guice passes so our subclasses can reuse it.
          setBinder(bind);
  
          // TODO: For now do nothing, down the road add some
          // &quot;default bindings&quot; here.
  
          // Now let subclasses define their own bindings.
          bindDependencies();
      }
  
      // getter and setter for &quot;binder&quot; field
      // ...
  }
  
  public abstract class AbstractAppModule extends BaseModule {
      /* Guice Injector. */
      private Injector injector;
  
      // Some other fields (unimportant for this code snippet)
  
      public AbstractAppModule() {
          super();
      }
  
      // getters and setters for all fields
      // ...
  
      public Object inject(Class&lt;?&gt; classKey) {
          if(injector == null)
              injector = Guice.createInjector(this);
  
          return injector.getInstance(classKey);
      }
  }
  </code></pre>
  <p>So, to use this small library:</p>
  <pre><code>public class DummyObj {
      private int nonsenseInteger = -1;
  
      // getter &amp; setter for nonsenseInteger
  
      @Calibrated
     public void shouldBeIntercepted() {
         System.out.println(&quot;I have been intercepted.&quot;);
     }
  }
  
  public class MyAppModule extends AbstractAppModule {
      private Profiler profiler;
  
      // getter and setter for profiler
  
      @Override
      public void bindDependencies() {
          DummyObj dummy = new DummyObj();
          dummy.setNonsenseInteger(29);
  
          // When asked for a DummyObj.class, return this instance.
          getBinder().bind(DummyObj.class).toInstance(dummy);
  
          // When a method is @Calibrated, intercept it with the given profiler.
          getBinder().bindInterceptor(Matchers.any(),
              Matchers.annotatedWith(Calibrated.class),
              profiler);
      }
  }
  
  public class TestGuice {
      public static void main(String[] args) {
          Profiler profiler = new Profiler();
          MyAppModule mod = new MyAppModule();
          mod.setProfiler(profiler);
  
          // Should return the bounded instance.
          DummyObj dummy = (DummyObj.class)mod.inject(DummyObj.class);
  
          // Should be intercepted...
          dummy.shouldBeIntercepted();
  
          System.out.println(dummy.getNonsenseInteger());
      }
  }
  </code></pre>
  <p>This is a lot of code so I may have introduced a few typos when keying it all in, but I assure you this code compiles and throws no exceptions when ran.</p>
  <p>Here's what happens:</p>
  <ul>
  <li>The <code>@Calibrated shouldBeIntercepted()</code> method is <em>not</em> intercepted; but...</li>
  <li>The console output shows the dummy's nonsense integer as...29!!!!</li>
  </ul>
  <p>So, regardless of how poor a design you may <em>think</em> this is, you can't argue that Guice is indeed working for 1 binding (the instance binding), but not the AOP method interception.</p>
  <p>If the instance binding wasn't working, then I would happily revisit my design. But something else is going on here. I'm wondering if my inheritance tree is throwing the <code>Binder</code> off somehow?</p>
  <p>And I've verified that I am binding the interceptor to the annotation correctly: I created another package where I just implement <code>Module</code> (instead of this inheritance tree) and use the same annotation, the same <code>Profiler</code>, and it works perfectly fine.</p>
  <p>I've used <code>Injector.getAllBindings()</code> to print out the map of all my <code>MyAppModule</code>'s bindings as Strings. Nothing is cropping up as the clear source of this bug.</p>