Annotated Typedefs

The theory about how typedefs work is the same as when defining them via XML. This example shows you how to define them using annotations

Let's take a look at SimpleInterceptor.java

import org.jboss.aop.TypeDef;
import org.jboss.aop.pointcut.Typedef;

@InterceptorDef
@Bind (pointcut = "all($typedef{SimpleInterceptor.myTypedef})")
public class SimpleInterceptor implements Interceptor
{
   @TypeDef ("(class(POJO) AND has(* *->method(..))) OR class($instanceof{ExecutionTypedefInterface})")
   public static Typedef myTypedef;

   public String getName() { return "SimpleInterceptor"; }

   public Object invoke(Invocation invocation) throws Throwable
   {
      ...
   }
}

SimpleInterceptor contains a field annotated with @TypeDef, which specifies a type expression for all classes to be used for the typedef. As for the Annotated composition example, the fully qualified name of the field itself is used as the name when we want to reference the typedef. We have used a field type of Typedef for clarity, but the actual type of the field is irrelevant.

SimpleInterceptor and SimpleInterceptor2 both reference the typedef in their @Bind annotation:

   @Bind (pointcut = "all($typedef{SimpleInterceptor.myTypedef})")

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
     [java] <<< Entering SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor
     [java] --- pojo.field1 = 10; ---
     [java] <<< Entering SimpleInterceptor
     [java] <<< Entering SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor
     [java] --- get pojo.field1;---
     [java] <<< Entering SimpleInterceptor
     [java] <<< Entering SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor
     [java] --- pojo.method(); ---
     [java] <<< Entering SimpleInterceptor
     [java] <<< Entering SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor

     [java] =======================

     [java] --- new POJO2(); ---
     [java] <<< Entering SimpleInterceptor
     [java] <<< Entering SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor
     [java] --- pojo2.field1 = 10; ---
     [java] <<< Entering SimpleInterceptor
     [java] <<< Entering SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor
     [java] --- get pojo2.field1; ---
     [java] <<< Entering SimpleInterceptor
     [java] <<< Entering SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor
     [java] --- pojo2.method(); ---
     [java] <<< Entering SimpleInterceptor
     [java] <<< Entering SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor2
     [java] >>> Leaving SimpleInterceptor