Question

Say I have a method like so:

public void method(@CustomAnnotation("value") String argument)

Is there a pointcut expression that could select all methods with arguments annotated with @CustomAnnotation? If so is there a way I could get access go the "value" argument?

Was it helpful?

Solution

On selecting your arguments :

@Before("execution(* *(@CustomAnnotation (*)))")
public void advice() {
System.out.println("hello");
}

ref : http://forum.springsource.org/archive/index.php/t-61308.html

On getting the annotation param :

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Annotation[][] methodAnnotations = method.getParameterAnnotations();

Will get you the annotations which you can iterate and use instanceof to find your bound annotation. I know thats hacky but afaik this is the only way supported currently.

OTHER TIPS

To match 1..N annotated arguments regardless of their position use

Edit 2020-12-3: Simplified version from @sgflt

@Before("execution(* *(.., @CustomAnnotation (*), ..))")
public void advice(int arg1, @CustomAnnotation("val") Object arg2) { ... }

Just to complete the last answer:

@Before(value="execution(* *(@CustomAnnotation (*), ..)) && args(input, ..)")
public void inspect(JoinPoint jp, Object input) {
        LOGGER.info(">>> inspecting "+input+" on "+jp.getTarget()+", "+jp.getSignature());
}

will match a method where (one of) the argument of the method is annotated with @CustomAnnotation, for example:

service.doJob(@CustomAnnotation Pojo arg0, String arg1);

In contrast to

@Before(value="execution(* *(@CustomAnnotation *, ..)) && args(input, ..)")
public void inspect(JoinPoint jp, Object input) {
            LOGGER.info(">>> inspecting "+input+" on "+jp.getTarget()+", "+jp.getSignature());
    }

which will match methods where (one of) the argument's class is annotated with @CustomAnnotation, for example:

service.doJob(AnnotatedPojo arg0, String arg1);

where the pojo is declared as follows:

@CustomAnnotation
public class AnnotatedPojo {
}

All the difference lies in the @CustomAnnotation (*) vs. @CustomAnnotation * in the pointcut declaration.

If you have more than one parameter in method you should use also two dots for mathing any number of parameters (zero or more)

@Before("execution(* *(@CustomAnnotation (*), ..))")
public void advice() {
    System.out.println("hello");
}

From the Spring Docs:

@Before("@annotation(myAnnotation)")
public void audit(Auditable myAnnotation) {
  AuditCode code = auditable.value();
  // ...
}

Which works well for me, without any need to manipulate the method signature.

Note: If you're using a named pointcut, because pointcut names can be overloaded, you must supply matching (parameter name and order) signatures.

@Before("goodAdvise(myAnnotation)")
public void audit(Auditable myAnnotation) {
  String value = auditable.value();
  // ...
}

@Pointcut("@annotation(myAnnotation)")
public void goodAdvise(Auditable myAnnotation) { 
  //empty
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top