Create a pointcut based on annotation parameters

98 views Asked by At

I'm trying to create a pointcut based on the parameter of an annotaion

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MyAnnotation {
    Class<? extends ABC> style() default A.class;
}

And the pointcut I'm currently using is:

@Pointcut("execution(@com.something.MyAnnotation * *(..))")
public void dummyMethod() {
}

@Around("method()")
public Object actualFunc(ProceedingJoinPoint joinPoint) throws Throwable {
    //stuff
}

But that unfortunately activates on all values of style.

1

There are 1 answers

0
Andy Clement On

Obviously you could check in the advice if the advised method had the annotation value you are looking for, but that is less than ideal (it is a runtime check). In your case you can just use the syntax:

@Pointcut("execution(@com.something.MyAnnotation(style=B.class) * *(..))")

There is a little bit of info here on annotation value matching: https://eclipse.org/aspectj/doc/released/README-160.html