0
votes

I'd like to write an AspectJ aspect that guards all methods of our Java classes that have a javax validation constraint. I.e. for the following code

@Target({METHOD})
@Retention(RUNTIME)
@Constraint
public @interface NiceMessage {
}

public class Sample {
  void doSomething(@NiceMessage String message) {
  }

  void doSomethingElse(String message) {
  }
}

a 'before' pointcut should be used for the 'doSomething' method but not for the 'doSomethingElse' method. Thus the pointcut has to be applied for all methods that contain at least one parameter with an annotation which itself has the @Constraint annotation.

How can such a pointcut be expressed with AspectJ?

1

1 Answers

0
votes

Here's a pointcut that will match a call to such a method :

@Pointcut("call(* *(.., @com.sample.NiceMessage (*), ..))")
public void pôintcutAnnotatedParam(JoinPoint jp) {
}

Note : If you're absolutely sure that the annotated param is going to be the first (resp. last) parameter, then you can drop the first (resp. last) double dots.