Declare Annotation

AspectJ 5 supports a new kind of declare statement, declare annotation. This takes different forms according to the recipient of the annotation: declare @type for types, declare @method for methods, declare @constructor for constructors, and declare @field for fields. declare @package may be supported in a future release.

The general form is:

  	declare @<kind> : ElementPattern : Annotation ;

Where annotation is a regular annotation expression as defined in the Java 5 language. If the annotation has the @Target meta-annotation, then the elements matched by ElementPattern must be of the kind specified by the @Target annotation.

ElementPattern is defined as follows:

  	        ElementPattern := TypePattern |
  	                          MethodPattern |
  	                          ConstructorPattern |

The following examples illustrate the use of declare annotation.

declare @type :* : @BusinessDomain ;

All types defined in a package with the prefix have the @BusinessDomain annotation.

declare @method : public * BankAccount+.*(..) : @Secured(role="supervisor")

All public methods in BankAccount and its subtypes have the annotation @Secured(role="supervisor").

declare @constructor : : @Secured(role="supervisor")

All constructors in BankAccount and its subtypes have the annotation @Secured(role="supervisor").

declare @field : * DAO+.* : @Persisted;

All fields defined in DAO or its subtypes have the @Persisted annotation.