To Install AspectJ on Windows
----------------------------------------------------------------------------- to install on Linux, this will work close to as is to install on Apple, you will do something similar but I have no platform to try it on -----------------------------------------------------------------------------
- google AspectJ downloads
-
download aspectj1.9.6 (most recent stable release)
this is a java jar file - double click the jar, it will self-install
- Follow the end suggestions... making chages to environment variables Path and CLASSPATH so ajc can be found
- Fire up a DOS window (command prompt) and try "ajc" to make sure the AspectJ compiler is findable
-
Try this HelloWorld example.
Make two files in a directoryFile "HelloWorld.java"
public class HelloWorld { public static void main (String[] args) { printMsg("Hello, World!!"); } public static void printMsg(String msg) { System.out.println("\n"); System.out.println(" "+msg); System.out.println("\n"); } }
File "HWTracer.aj"
(.aj is used for AspectJ advice, which is not valid Java)
public aspect HWTracer { // pointcuts pointcut theMainMethod() : execution(public static void main(String[])); pointcut theCall() : call (* HelloWorld.printMsg(*)); // advice before(): theMainMethod() { System.out.println("Before the Main"); } // note this is after main is done, after the entire program finishes after(): theMainMethod() { System.out.println("After the Main"); } before(): theCall() { System.out.println("-- before the call --"); } after(): theCall() { System.out.println("-- after the call --"); } }
-
Now compile with javac, run java
javac HelloWorld.java this leaves HelloWorld.class java HelloWorld you will see the unadorned java program run no aspects involved
-
8. Now compile with ajc, run java
ajc HelloWorld.java HWTracer.aj this leaves HelloWorld.class and HWTracer.class java HelloWorld you will see the output from HelloWorld, mixed with output from HWTracer
Other Helpful Items
I find Windows Terminal useful as a comman prompt window... more "shell like" for Unix/Linux users