Search code examples
javadependency-injectionguice

Guice interceptor invoked twice


If I have an annotation which is

@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD})
public @interface RESTMonitor {
}

And if i used it on both class level and method level in the same class. Then how do I restrict it to execute only once. Now it is getting invoked twice.


Solution

  • So you have two bindInterceptor() calls, and this normally results in two different interceptors being invoked. But if you pass the same interceptor to both calls, they will get de-duplicated:

    RESTMonitorMethodInterceptor interceptor = new RESTMonitorMethodInterceptor();
    bindInterceptor(Matchers.annotatedWith(RESTMonitor.class), Matchers.any(), interceptor);
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(RESTMo‌​nitor.class), interceptor);
    

    (I implemented this feature.)