Guice Synthetic Warning

We have Guice and its support for AOP. We have two 3D modules that use AOP support: Shiro and Guice JPA module. As a result, Guis complains that "the method can be intercepted twice." My question is: how can I avoid this behavior: I probably don't need to intercept synthetic methods at all.

If our modules were ours, we could just add Matcher, which filters out all synthetic methods (for example, he says here ), but the problem is these are third-party modules.

+4
source share
1 answer

The best way I could find is this: just override 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();
    }
}

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 does not always work. As in my case, the target JpaPersistModule is final, and the only way I could override the method is to copy the inserted implementation.

+8
source

Source: https://habr.com/ru/post/1538134/


All Articles