The main focus of AspectJ 5, which is currently in its second milestone version, is to support the new Java language features introduced in Java 5, including annotations and generics. AspectJ 5 also contains new features that are not joined to Java 5, such as the annotation based development style, improved loading and weaving, and a new aspect instantiation model.
In this installment of the Aop@work series, I outlined the AspectJ 5 language and the AspectJ 5 version containing AspectJ compilers and related tools. Let me first explain how to compile a Java application with the AspectJ 5 compiler (either with a command-line compiler or with the AspectJ development tool (AJDT see Resources), and then I provide some examples of implementing AspectJ applications using the Java 5 feature. I also discussed the implications of Erasure (erasure) for AOP systems, which is the technique used to implement generics in Java 5, and I explained how AspectJ solves the problem. Some of the features described in this article can only be compiled with the upcoming AspectJ 5 M3 version (scheduled for release in July 2005).
You can also download the AJDT or command line AspectJ compiler used in the following example. See Resources for links to technical downloads.
Compiling Java 5 Applications with AspectJ
The AspectJ compiler (AJC) supports compiling Java source code at the compatibility level of version 1.3 (and previous versions), 1.4 and 5.0, and generating bytecode for VMS above 1.1. Like Javac, AJC has some limitations: compiling the source code at version 1.4 compatibility level only supports the target VM for 1.4 and above, and compiling the source code at version 5.0 compatibility level only supports the 5.0 version of the target VM.
The default compatibility level for the AspectJ compiler is to use the source code level of 5.0 and generate bytecode for the 5.0 VM. You can pass the 1.5 compiler option to explicitly set the source compatibility level and target level to Java 5. Suppose you want to use the AspectJ 5 compiler to handle the Java 1.4 language and for the 1.4 VM, then just pass-1.4.
The AspectJ 5 loom also defaults to run in Java 5 compatibility mode. In this pattern, the loom correctly interprets the new features in Java 5, for example, when the compiler considers automatic boxing and unboxing when determining whether args (Integer) matches the int parameter. If you are not compiling from a source file, but using the compiled Java 5 class file (on Inpath), using the AspectJ compiler to weave the aspect (on the Aspectpath), then this is the desired behavior. The pass-1.4 or-1.3 option disables the Java 5 attribute.
The AspectJ Development environment Guide contains more information about the new compiler flags and options. See the Resources section for access to this guide.
Compiling with AJDT and Eclipse
If you are compiling and running a AspectJ program with AJDT, then AspectJ inherits the compiler options specified in Eclipse for Java compilation. In this case, AspectJ can be configured to use the Java 5 schema as a workspace option, or on a per-project basis. Just go to the Java compiler option Configuration page and set the Compiler compliance level property to 5.0. If you are upgrading from JDK 1.4, you may also need to update the JRE system library to the Java 5 JRE in the project's build settings.
Figure 1 shows the Java compiler option configuration page for AJDT and an option configuration setting for the Java 5.0 compatibility level.
Figure 1. Specify the 5.0 compatibility level in Eclipse