Intercept on method OR class annotation only once

17 views
Skip to first unread message

Dieter Van de Walle

unread,
Nov 21, 2018, 6:33:16 AM11/21/18
to google-guice

I want to have a Guice interceptor that intercepts calls either to a class that is annotated, or a method that is annotated. I'd like to be able to combine both, ie. override the class annotation with a method annotation with different properties.

I have this working like this:

// Intercept all METHODS annotated with @MyAnnotation
bindInterceptor(
    Matchers.any(),
    Matchers.annotatedWith(company.MyAnnotation),
    new TracingInterceptor());

// Intercept all methods in CLASSES annotated with @MyAnnotation
bindInterceptor(
    Matchers.annotatedWith(company.MyAnnotation),
    Matchers.any(),
    new TracingInterceptor());

However when I annotate a class like this:

@MyAnnotation    
class MyClass {
    @MyAnnotation
    public void myMethod() {}
}

The interceptor gets called twice, which is bad!

Is there any way to avoid triggering the interceptor twice, but having the same behaviour?

Reply all
Reply to author
Forward
0 new messages