Using Annotations with declare statements

Declare error and declare warning

Since pointcut expressions in AspectJ 1.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(* (@DataLayer org.xyz..)*.*(..)) &&
  	                within((@UI org.xyz..)*)
  	                : "User interface should not call the data layer directly";
	

declare parents

The general form of a declare parents statement is:

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

Since AspectJ 1.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 @SecuredAnnotation is present, implement the SecuredObject interface.

declare parents : (@Secured *..)* implements SecuredObject;

Every type in an @Secured package implements the SecuredObject interface.

declare precedence

The general form of a declare precedence statement is:

  	declare precedence : TypePatList;
	

AspectJ 1.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).