A good way to visualize metadata is to think of classnames, method names, field names, and constructor signatures as nouns, and annotations/metadata as adjectives. You can declare advice/interceptor bindings like: All @Remotable objects should register with a dispatcher whenever they are constructed. Any method marked @transactional begin/commit/rollback a transaction at the start and end of the method. Or even any field, method, constructor marked @traceable, do tracing. It kinda lets the application developer give hints to the aspect developer. If you think about it another way, combining annotations and AOP allows you to plug in new Java keywords. Kinda like C pre-processor macros on steroids. Macros that are typesafe and checked by the compiler and unlike Major League Baseball players, it will always be legal for you to use these steroids in your applications.
@trace @billable public void someMethod() { System.out.println("someMethod"); }In the above example, we are declaring someMethod() to be traced and billable. Don't forget! At least with JDK 1.5 beta, you must compile your Java source files with 1.5 source to have annotations compile.
$ javac -source 1.5 MyFile.java
<bind pointcut="execution(POJO->@billable(..))"> <interceptor class="BillingInterceptor"/> </bind> <bind pointcut="execution(* POJO->@billable(..))"> <interceptor class="BillingInterceptor"/> </bind>
The first binding above says that for every constructor tagged as @billable apply the BillingInterceptor. The second binding states that for any method tagged as @billable apply the BillingInterceptor. Let's now take a look at applying the tracing advice.
<bind pointcut="all(@trace)"> <interceptor class="TraceInterceptor"/> </bind>
The above states that for any field, constructor, or method tagged as @trace, apply the TraceInterceptor.
$ antThis should be the output:
run: [java] --- new POJO(); --- [java] populateMixinMethods***** [java] billing...org.jboss.aop.joinpoint.ConstructorInvocation@1ee4648 [java] <<< Trace : executing constructor public POJO() [java] empty constructor [java] >>> Leaving Trace [java] --- new POJO(int); --- [java] <<< Trace : executing constructor public POJO(int) [java] int constructor [java] >>> Leaving Trace [java] --- pojo.someMethod(); --- [java] billing...org.jboss.aop.joinpoint.MethodInvocation@1b383e9 [java] <<< Trace : executing method public void POJO.someMethod() [java] someMethod [java] >>> Leaving Trace [java] --- pojo.field = 55; --- [java] <<< Trace : write field name: public int POJO.field [java] >>> Leaving Trace