Declare statements
Prev | Chapter 9. An Annotation Based Development Style | Next

## Declare statements

The previous section on inter-type declarations covered the case of declare parents ... implements. The 1.5.0 release of AspectJ 5 does not support annotation style declarations for declare parents ... extends and declare soft (programs with these declarations would not in general be compilable by a regular Java 5 compiler, reducing the priority of their implementation). These may be supported in a future release.

Declare annotation is also not supported in the 1.5.0 release of AspectJ 5.

Declare precedence is supported. For declare precedence, use the `@DeclarePrecedence` annotation as in the following example:

         public aspect SystemArchitecture {
           declare precedence : Security*, TransactionSupport, Persistence;

           // ...
         }

         can be written as:

         @Aspect
         @DeclarePrecedence("Security*,org.xyz.TransactionSupport,org.xyz.Persistence")
         public class SystemArchitecture {

           // ...
         }

We also support annotation style declarations for declare warning and declare error - any corresponding warnings and errors will be emitted at weave time, not when the aspects containing the declarations are compiled. (This is the same behaviour as when using declare warning or error with the code style). Declare warning and error declarations are made by annotating a string constant whose value is the message to be issued.

Note that the String must be a literal and not the result of the invocation of a static method for example.

           declare warning : call(* javax.sql..*(..)) && !within(org.xyz.daos..*)
                           : "Only DAOs should be calling JDBC.";

           declare error : execution(* IFoo+.*(..)) && !within(org.foo..*)
                         : "Only foo types can implement IFoo";

           can be written as...

           @DeclareWarning("call(* javax.sql..*(..)) && !within(org.xyz.daos..*)")
           static final String aMessage = "Only DAOs should be calling JDBC.";

           @DeclareError("execution(* IFoo+.*(..)) && !within(org.foo..*)")
           static final String badIFooImplementors = "Only foo types can implement IFoo";

           // the following is not valid since the message is not a String literal
           @DeclareError("execution(* IFoo+.*(..)) && !within(org.foo..*)")
           static final String badIFooImplementorsCorrupted = getMessage();
           static String getMessage() {
               return "Only foo types can implement IFoo " + System.currentTimeMillis();
           }

Prev | Home | Next
Inter-type Declarations | Up | aspectOf() and hasAspect() methods