Teimuraz
Teimuraz

Reputation: 9325

AspectJ Poincut for method with arguments with specified annotaion

For example I have following methods:

public void method1(@MyAnnotation Object a, Object b..) {
   ...
}

public void method1(Object a, Object b..., @MyAnnotation Object n, ...) {
   ...
}

What is AspectJ pointcut that targets only methods that have parameters annotated with @MyAnnotation?

This annotation can be applied for ANY argument of the method.

Upvotes: 0

Views: 579

Answers (1)

Nándor Előd Fekete
Nándor Előd Fekete

Reputation: 7098

The following pointcut should match the execution of methods in your question. I also provided a second pointcut which is only a slight modification of the first one, but you might find it useful if you need to match specific types annotated with an annotation.

public aspect AnnotatedMethodParameterMatchingAspect  {

    /**
     * Matches the execution of any method having a parameter annotated with the
     * {@link MyAnnotation} annotation.
     */
    pointcut executionOfMethodWithAnnotatedParameter(): 
        execution(* *(.., @MyAnnotation (*), ..));

    /**
     * Matches the execution of any method having a parameter annotated with the
     * {@link MyAnnotation} annotation where the parameter type is a {@link MyType}
     * (or a subtype).
     */
    pointcut executionOfMethodWithAnnotatedTypeRestrictedParameter(): 
        execution(* *(.., @MyAnnotation (MyType+), ..));

}

Upvotes: 3

Related Questions