AspectJ 1.5 supports annotations on aspects, and on method, field, constructor, advice, and inter-type declarations within aspects. Annotations are not permitted on pointcut declarations or on declare statements.
The following example illustrates the use of annotations in aspects:
@AspectAnnotation public abstract aspect ObserverProtocol { @InterfaceAnnotation interface Subject {} @ITDFieldAnnotation private List Subject.observers; @ITDMethodAnnotation public void Subject.addObserver() { ... } @ITDMethodAnnotation public void Subject.removeObserver() { ... } @MethodAnnotation private void notifyObservers(Subject subject) { ... } /** * Delegate to concrete sub-aspect the actual form of * notification for a given type of Subject. */ @MethodAnnotation protected abstract void notifySubject(Subject s); /* no annotations on pointcuts */ abstract pointcut observedEvent(Subject subject); @AdviceAnnotation after(Subject subject) returning : observedEvent(subject) { notifyObservers(subject); } }
AspectJ 1.5 supports a new XLint warning, "the pointcut associated with this advice does not match any join points". The warning is enabled by default and will be emitted by the compiler if the pointcut expression associated with an advice statement can be statically determined to not match any join points. The warning can be suppressed for an individual advice statement by using the @SuppressWarnings("unmatched") annotation.
Discussion point: is it wise to allow annotations on advice? It may have limited usage - eg. an @SuppressWarnings("unchecked") annotation. On the other hand it opens a door to people wanting to match adviceexecution join points based on annotations. Is that a model we should be encouraging or even something we want to support?