Using Annotations with declare statements
Prev | Chapter 2. Annotations | Next

## Using Annotations with declare statements

### Declare error and declare warning

Since pointcut expressions in AspectJ 5 support join point matching based on annotations, this facility can be exploited when writing `declare warning` and `declare error` statements. For example:

      	declare warning : withincode(@PerformanceCritical * *(..)) &&
      	                  call(@ExpensiveOperation * *(..))
      	                : "Expensive operation called from within performance critical section";

      	declare error : call(* org.xyz.model.*.*(..)) &&
      	                !@within(Trusted)
      	                : "Untrusted code should not call the model classes directly";

### declare parents

The general form of a `declare parents` statement is:

      	declare parents : TypePattern extends Type;
      	declare parents : TypePattern implements TypeList;

Since AspectJ 5 supports annotations as part of a type pattern specification, it is now possible to match types based on the presence of annotations with either class-file or runtime retention. For example:

declare parents : (@Secured *) implements SecuredObject;

  All types with the `@Secured` annotation implement the `SecuredObject` inteface.

declare parents : (@Secured BankAccount+) implements SecuredObject;

  The subset of types drawn from the `BankAccount` type and any subtype of `BankAccount`, where the `@Secured` annotation is present, implement the `SecuredObject` interface.

An annotation type may not be used as the target of a declare parents statement. If an annotation type is named explicitly as the target of a declare parents statement, a compilation error will result. If an annotation type is matched by a non-explicit type pattern used in a declare parents statement it will be ignored (and an XLint warning issued).

### declare precedence

The general form of a declare precedence statement is:

      	declare precedence : TypePatList;

AspectJ 5 allows the type patterns in the list to include annotation information as part of the pattern specification. For example:

declare precedence : (@Security *),*;

  All aspects with the `@Security` annotation take precedence over any other aspects in the system. (Or, more informally, all security-related aspects take precedence).

Prev | Home | Next
Join Point Matching based on Annotations | Up | Declare Annotation