## Annotated Interceptor Precedence

#### Background

The theory for precedence is the same as in the XML example, but when using annotations precedence is declared differently. Also, the whole concept of precedence is somewhat more important when applied to an application where the AOP bindings are defined using annotations rather than in XML, since using XML the interceptions occur in the order they have been defined, while using annotations there is no other notion of ordering of interceptions.

### Defining

You define precedence in a separate class annotated with @Precedence

       import org.jboss.aop.Precedence;
       import org.jboss.aop.PrecedenceInterceptor;

       @Precedence
       public class Precedence1
       {

          @PrecedenceInterceptor
          public static SimpleInterceptor precedenceInterceptor1;

          @PrecedenceInterceptor
          public static SimpleInterceptor2 precedenceInterceptor2;
       }

The @PrecedenceInterceptor is used to annotate fields where the type is an interceptor, and the orderings within the class define the relative sort order for the precedence. In other words this class defines that

       SimpleInterceptor
                    comes before
       SimpleInterceptor2

`Precedence2` also defines a set of precedences:

       @Precedence
       public class Precedence2
       {
          @PrecedenceInterceptor
          public static SimpleInterceptor2 simple2;

          @PrecedenceAdvice ("advice")
          public static TestAspect advice;

          @PrecedenceInterceptor
          public static SimpleInterceptor3 simple3;

          @PrecedenceAdvice ("otherAdvice")
          public static TestAspect otherAdvice;
       }

The @PredenceAdvice is used to annotate fields where the type is an aspect. The attribute value in the @PrecedenceAdvice annotation is the name of the advice method in the aspect, so:

       SimpleInterceptor2
                    comes before
       TestAspect.advice
                    comes before
       SimpleInterceptor3
                    comes before
       TestAspect.otherAdvice

The precedence entries are merged into an overall precedence entry, so that:

      SimpleInterceptor
    		comes before
      SimpleInterceptor2
    		comes before
      TestAspect.advice
    		comes before
      SimpleInterceptor3
    		comes before
      TestAspect.otherAdvice

#### Run the example

To compile and run:

      $ ant

It will javac the files and then run the AOPC precompiler to manipulate the bytecode, then finally run the example. The output should read as follows:

    run:
         [java] --- new POJO(); ---
         [java] <<< Entering SimpleInterceptor type: POJO0OptimizedConstructorInvocation
         [java] <<< Entering SimpleInterceptor2 type: POJO0OptimizedConstructorInvocation
         [java] <<< Entering TestAspect.advice type:POJO0OptimizedConstructorInvocation@f6a746
         [java] <<< Entering SimpleInterceptor3 type: POJO0OptimizedConstructorInvocation
         [java] <<< Entering TestAspect.otherAdvice type:POJO0OptimizedConstructorInvocation@f6a746
         [java] empty constructor
         [java] <<< Leaving TestAspect.otherAdvice type:POJO0OptimizedConstructorInvocation@f6a746
         [java] >>> Leaving SimpleInterceptor3
         [java] <<< Leaving TestAspect.advice type:POJO0OptimizedConstructorInvocation@f6a746
         [java] >>> Leaving SimpleInterceptor2
         [java] >>> Leaving SimpleInterceptor