springspring-aoppointcut

How does this execution pointcut expression work?


I came across an answer from @kriegaex , which I am unable to comprehend.

The pointcut expression I am trying to understand is the following

@Around("execution(* (@MyAnnotation *).*(..)) || execution(@MyAnnotation * *(..))")

As I understand , this expression will advice any class or method annotated with @MyAnnotation

From the reference documentation , the format of an execution expression is as follows:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
            throws-pattern?)

Based on this format , the following expression

execution(* (@MyAnnotation *).*(..))

can be explained as

ret-type-pattern is * 
declaring-type-pattern is any type with @MyAnnotation
name-pattern is *
param-pattern is ..

to advice any method call in a class annotated with @MyAnnotation. Please correct me if I am wrong .

And for this expression

execution(@MyAnnotation * *(..))

I am unable to understand how modifiers-pattern can be @MyAnnotation ? How does this work ?


Solution

  • That is an interesting question. Someone is reading documentation, wow! :)

    Documentation maintenance is kind of a problem in AspectJ because nowadays development is mainly a one man show performed by Andrew (Andy) Clement. He is mostly busy keeping up with the Java release cycle and the new language features, e.g. there already is a developer version out with Java 14 support (record classes).

    The syntax description on the Spring website has been taken from the AspectJ Programming Guide which it also points to. While the definition is still valid, the programming guide has been written in the pre Java 5 era, i.e. long ago, and IMO not updated since then. You might notice that it does not even mention annotations at all.

    Everything related to annotations is written in the so-called The AspectJ 5 Development Kit Developer's Notebook and there you find a small paragraph mentioning:

    Every join point has a single set of modifiers - these include the standard Java modifiers such as public, private, static, abstract etc., any annotations, and the throws clauses of methods and constructors. These modifiers are the modifiers of the subject of the join point.

    So for AspectJ, method or class annotations are part of their group of modifiers, as you correctly noted already.


    Update: You might want to think about investing in the book "AspectJ in Action" by Ramnivas Laddad. It is also old (2nd edition from 2009), but covers the basics of both AspectJ and Spring AOP in a solid way. Here is a screenshot, I hope Ramnivas and/or the publisher don't sue me for that. It is meant to be an incentive to buy the book:

    Method and constructor signature patterns


    Update 2: It looks as if now the complete book can even be read online for free. The chapter from the screenshot is here, for example.