如何仅匹配一次类型或方法注释
How to match on type OR method annotation only once
我想要一个 Guice 拦截器来拦截对带注释的 class 或带注释的方法的调用。我希望能够将两者结合起来,即。使用具有不同属性的方法注释覆盖 class 注释。
我是这样工作的:
// 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());
然而,当我这样注释 class 时:
@MyAnnotation
class MyClass {
@MyAnnotation
public void myMethod() {}
}
拦截器被调用了两次,这很糟糕!
有什么方法可以避免两次触发拦截器,但具有相同的行为?
您可以通过使活页夹互斥来实现此目的,如下所示:
// Intercept all METHODS annotated with @MyAnnotation in classes not annotated with @MyAnnotation
bindInterceptor(
Matchers.not(Matchers.annotatedWith(company.MyAnnotation)),
Matchers.annotatedWith(company.MyAnnotation),
new TracingInterceptor());
// Intercept all methods not annotated with @MyAnnotation in CLASSES annotated with @MyAnnotation
bindInterceptor(
Matchers.annotatedWith(company.MyAnnotation),
Matchers.not(Matchers.annotatedWith(company.MyAnnotation)),
new TracingInterceptor());
// Intercept all METHODS not annotated with @MyAnnotation in CLASSES annotated with @MyAnnotation
bindInterceptor(
Matchers.annotatedWith(company.MyAnnotation),
Matchers.annotatedWith(company.MyAnnotation),
new TracingInterceptor());
我想要一个 Guice 拦截器来拦截对带注释的 class 或带注释的方法的调用。我希望能够将两者结合起来,即。使用具有不同属性的方法注释覆盖 class 注释。
我是这样工作的:
// 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());
然而,当我这样注释 class 时:
@MyAnnotation
class MyClass {
@MyAnnotation
public void myMethod() {}
}
拦截器被调用了两次,这很糟糕!
有什么方法可以避免两次触发拦截器,但具有相同的行为?
您可以通过使活页夹互斥来实现此目的,如下所示:
// Intercept all METHODS annotated with @MyAnnotation in classes not annotated with @MyAnnotation
bindInterceptor(
Matchers.not(Matchers.annotatedWith(company.MyAnnotation)),
Matchers.annotatedWith(company.MyAnnotation),
new TracingInterceptor());
// Intercept all methods not annotated with @MyAnnotation in CLASSES annotated with @MyAnnotation
bindInterceptor(
Matchers.annotatedWith(company.MyAnnotation),
Matchers.not(Matchers.annotatedWith(company.MyAnnotation)),
new TracingInterceptor());
// Intercept all METHODS not annotated with @MyAnnotation in CLASSES annotated with @MyAnnotation
bindInterceptor(
Matchers.annotatedWith(company.MyAnnotation),
Matchers.annotatedWith(company.MyAnnotation),
new TracingInterceptor());