Annotating Aspects
Prev | Chapter 2. Annotations | Next

## Annotating Aspects

AspectJ 5 supports annotations on aspects, and on method, field, constructor, advice, and inter-type declarations within aspects. Method and advice parameters may also be annotated. 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);
    			}
    		}

An annotation on an aspect will be inherited by sub-aspects, iff it has the `@Inherited` meta-annotation.

AspectJ 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. (See JLS 9.6.1.5).

Prev | Home | Next
Chapter 2. Annotations | Up | Join Point Matching based on Annotations