title: Writing interceptors for parameter annotations using Guice
body: <h3>Main References for My Question:</h3>
  
  <ul>
  <li>Writing Method Interceptors for Google Guice: <a href="http://code.google.com/p/google-guice/wiki/AOP" rel="nofollow">http://code.google.com/p/google-guice/wiki/AOP</a></li>
  <li>The JavaDoc for the MethodInterceptor interface: <a href="http://aopalliance.sourceforge.net/doc/org/aopalliance/intercept/MethodInterceptor.html" rel="nofollow">http://aopalliance.sourceforge.net/doc/org/aopalliance/intercept/MethodInterceptor.html</a></li>
  <li>General references about Java annotations: <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html</a> and <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html" rel="nofollow">http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html</a></li>
  </ul>
  
  <hr>
  
  <h3>Now My Question:</h3>
  
  <p>I am writing a Java application that heavily relies on Google Guice for creating objects and handling dependency injection. I am trying to use interceptors to run pre-processing code before certain annotated methods are executed. So far, I have successfully been able to execute interceptors (using the <code>MethodInterceptor</code> interface) on methods that have been annotated, using Guice's instructions. However, I want to now write interceptors that will execure on <em>Parameter Annotations</em>.</p>
  
  <p>Here is an example scenario. First, I create my own annotation. For example::</p>
  
  <pre><code>@BindingAnnotation
  @Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.PARAMETER)
  public @interface MyParameterAnnotation {
  }
  </code></pre>
  
  <p>Next, I write my own interceptor for this annotation:</p>
  
  <pre><code>public class MyParameterAnnotationInterceptor implements MethodInterceptor {
      @Override
      public Object invoke(MethodInvocation invocation) throws Throwable {
         // Do some stuff
         return invocation.proceed();
      }
  }
  </code></pre>
  
  <p>Here's an example of how I intend on using <code>@MyParameterAnnotation</code>:</p>
  
  <pre><code>public class ExampleObject {
      public String foo(@MyParameterAnnotation String param) {
          ...
      }
  }
  </code></pre>
  
  <p>Finally, I need to create a Guice Injector and use it to create an instalce of <code>ExampleObject</code>, or else I cannot use a method interceptor in this project. I configure the Injector so that the <code>MyParameterAnnotationInterceptor</code> is bound to <code>@MyParameterAnnotation</code>, like so:</p>
  
  <pre><code>final MethodInterceptor interceptor = new MyParameterAnnotationInterceptor();
  requestStaticInjection(MyParameterAnnotationInterceptor.class);
  bindInterceptor(Matchers.any(), Matchers.annotatedWith(MyParameterAnnotation.class), interceptor);
  </code></pre>
  
  <p>When I follow the above steps and execute a call to <code>ExampleObject.foo()</code>, unfortunately the interceptor is not executed despite the parameter being marked by <code>@MyParameterAnnotation</code>. Note that these similar steps will work if the annotation was placed at the <em>method level</em> instead.</p>
  
  <p>This leads me to come up with two possible conclusions: either Guice cannot support binding an interceptor to a parameter annotation, or I am doing something completely incorrect (perhaps I should use another AOP Alliance interface for the interceptor, like <a href="http://aopalliance.sourceforge.net/doc/org/aopalliance/intercept/FieldInterceptor.html" rel="nofollow">FieldInterceptor</a>, but I highly doubt it because the <a href="http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/Binder.html#bindInterceptor%28com.google.inject.matcher.Matcher%3C?%20super%20java.lang.Class%3C?%3E%3E,%20com.google.inject.matcher.Matcher%3C?%20super%20java.lang.reflect.Method%3E,%20org.aopalliance.intercept.MethodInterceptor...%29" rel="nofollow">JavaDoc for Guice's AbstractModule</a> suggests that the <code>bindInterceptor()</code> method can only use a <code>MethodInterceptor</code> parameter).</p>
  
  <p>Nonetheless, all help us much appreciated :)</p>