Annotations are a great way to tag methods that should be targeted by an aspect. This allows the user of your aspect to apply its functionality wherever the developer wishes; rather than having to hard code a point cut expression. This is easy enough to do:
But, what if you want to target all methods in a class marked with an annotation? Well, that expression is a little more complicated. Let me show you how.
To explain this, let’s take a look at format of execution():
We want to target any class marked with @Annotation. So, we indicated that by placing @Annotation right before the className (in this case * for all classes). When you think of it, this just mirrors Java syntax; the class annotations are listed before the class name. What makes this look mucky is that we need to wrap the class pattern in parenthesis so our implementation knows that we expect the annotation to be attached to the class, not the method.
The above expression only targets classes marked with @Annotation. If you want to also target classes that extend classes marked with @Annotation, we need to add the + modifier to the class pattern (which indicated class X or subclasses of X):
But, because of precedence issues, we need to introduce another set of parenthesis. The final expression looks like this:
