title: Can't Get Guice Method Interception to Work
body: <p>I'm trying to print a "Hello, AOP!" message whenever Guice/AOP Alliance intercepts a method marked with a particular (custom) annotation.  I have followed the official docs (a PDF that can be found <a href="http://google-guice.googlecode.com/files/Guice%201.0%20User%27s%20Guide.pdf" rel="nofollow">here</a> - <em>AOP method interception stuff on pg. 11</em>) and cannot get it to work, only compile.</p>
  
  <p>First, my annotation:</p>
  
  <pre><code>@Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.METHOD})
  @BindingAnnotation
  public @interface Validating {
      // Do nothing; used by Google Guice to intercept certain methods.
  }
  </code></pre>
  
  <p>Then, my <code>Module</code> implementation:</p>
  
  <pre><code>public class ValidatingModule implements com.google.inject.Module {
      public void configure(Binder binder) {
          binder.bindInterceptor(Matchers.any(), 
              Matchers.annotatedWith(Validating.class,
              new ValidatingMethodInterceptor()),
      }
  }
  </code></pre>
  
  <p>Next, my method interceptor:</p>
  
  <pre><code>public class ValidatingMethodInterceptor implements MethodInterceptor {
      public Object invoke(MethodInvocation invocation) throws Throwable {
          System.out.println("Hello, AOP!");
      }
  }
  </code></pre>
  
  <p>Finally, the driver that attempts to make use of all this AOP stuff:</p>
  
  <pre><code>public class AopTest {
      @Validating
      public int doSomething() {
          // do whatever
      }
  
      public static main(String[] args) {
          AopTest test = new AopTest();
  
          Injector injector = Guice.createInjector(new ValidatingModule());
  
          System.out.println("About to use AOP...");
  
          test.doSomething();
      }
  }
  </code></pre>
  
  <p>When I run this little test driver, the only console output I get is <code>About to use AOP...</code>... the <code>Hello, AOP!</code> never gets executed, which means the <code>@Validating doSomething()</code> method is never being intercepted the way the Guice docs show.</p>
  
  <p>The <em>only</em> thing I can think of is the fact that in my <code>Module</code> implementation I am specifying the <code>MethodInterceptor</code> to bind to (as the 3rd argument to the <code>bindInterceptor</code> method) as being a <code>new ValidatingMethodInterceptor()</code>, whereas in that interceptor, I am only defining a required <code>invoke(MethodInvocation</code>) method.</p>
  
  <p>Perhaps I am not wiring these two together correctly? Perhaps Guice doesn't implicitly know that the <code>invoke</code> method should be ran when an intercept occurs?!?!</p>
  
  <p>Then again, not only have I followed the Guice docs, I have also followed several other tutorials to no avail.</p>
  
  <p>Is there something obvious I am missing here? Thanks in advance!</p>
  
  <p><strong>Edit</strong> One other discrepancy between my code and the examples I followed, although small, is the fact that my <code>invoke</code> method (inside the interceptor) is not annotated with <code>@Override</code>. If I try to add this annotation, I get the following compile error:</p>
  
  <blockquote>
    <p>The method invoke(MethodInvocation) of type ValidatingMethodInterceptor must override a superclass method.</p>
  </blockquote>
  
  <p>This error makes sense, because <code>org.aopalliance.intercept.MethodInterceptor</code> is an interface (not a class). Then again, every example using Guice/AOP Alliance uses this <code>@Override</code> annotation on the <code>invoke</code> method, so it obviously works/compiles for some people...weird.</p>