Search code examples
javajpaaopguiceshiro

Guice synthetic methods warning


We are having Guice and its AOP support. We have two 3d party modules which use the AOP support: Shiro and the Guice's JPA module. As a result Guice complains that "The method may be intercepted twice". My question is how can I avoid such behavior: I probably do not need to intercept synthetic methods at all.

If the modules were ours we could just add a Matcher which filters out all synthetic methods (like it says here) but the problem is these are 3d party modules.


Solution

  • The best way I could find is the following: just override the bindInterceptor methods like this.

    Matcher:

    public final class NoSyntheticMethodMatcher extends AbstractMatcher<Method> {
        public static final NoSyntheticMethodMatcher INSTANCE = new NoSyntheticMethodMatcher();
        private NoSyntheticMethodMatcher() {}
    
        @Override
        public boolean matches(Method method) {
            return !method.isSynthetic();
        }
    }
    

    The bindInterceptor method:

    @Override
    protected void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors) {
        super.bindInterceptor(classMatcher, NoSyntheticMethodMatcher.INSTANCE.and(methodMatcher), interceptors);
    }
    

    But the solution doesn't always work. Like in my case, the target JpaPersistModule is final and the only way I could override the method is to copy pasted the implementation.