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.
So, what good are JDK 1.5 annotations if you're using a JDK 1.4 compiler???? Well, JBoss AOP has an annotation compiler for JDK 1.4 that can convert typed annotations from doclet tags and embed them in your class files. This bytecode manipulation is compatible with JDK 1.5.
complex.java
public interface complex { char ch(); String string(); float flt(); double dbl(); short shrt(); long lng(); int integer(); boolean bool(); single annotation(); String[] array(); Class clazz(); }
/** * @@trace * @@single ("hello world") * @@complex (ch='a', string="hello world", flt=5.5, dbl=6.6, shrt=5, lng=6, integer=7, bool=true, annotation=@single("hello"), array={"hello", "world"}, clazz=java.lang.String) */ public void someMethod() { System.out.println("someMethod"); }
<annotationc compilerclasspathref="classpath" classpathref="classpath" bytecode="true"> <src path="."/> </annotationc>
For the annotation compiler to run, it has two requires:
The bytecode manipulations done to annotated classes are compatible with JDK 1.5. So, if you rewrite your JDk 1.4 annotations interfaces (changing the keyword interface to @interface), the annotated classes can be used with both JDK 1.4 and 1.5. I was going to ship an implementation of java.lang.annotation.Annotation and force JDK 1.4 annotation interfaces to implement this (and thus be fully binary compatible), but the Java license forbids you to distribute anything under the java.lang package. :( Oh well.
<bind pointcut="all(@trace)"> <interceptor class="TraceInterceptor"/> </bind>
The above states that for any field, constructor, or method tagged as @trace, apply the TraceInterceptor.
complex c = (complex)AnnotationElement.getAnyAnnotation(((MethodInvocation)invocation).getMethod(), complex.class);
$ antThis should be the output:
run: [java] --- new POJO(); --- [java] @single ("hello world") [java] @complex (ch='a', "hello world", flt=5.5, dbl=6.6, ...blah blah blah YOU GET THE IDEA... [java] <<< Trace : executing constructor public POJO() [java] empty constructor [java] >>> Leaving Trace [java] --- pojo.someMethod(); --- [java] @single ("hello world") [java] @complex (ch='a', "hello world", flt=5.5, dbl=6.6, ...blah blah blah YOU GET THE IDEA... [java] <<< Trace : executing method public void POJO.someMethod() [java] someMethod [java] >>> Leaving Trace [java] --- pojo.field = 55; --- [java] @single ("hello world") [java] @complex (ch='a', "hello world", flt=5.5, dbl=6.6, ...blah blah blah YOU GET THE IDEA... [java] <<< Trace : write field name: public int POJO.field [java] >>> Leaving Trace