COMP 590-059 (Fall 2021)

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

-----------------------------------------------------------------------------
  1. google AspectJ downloads

  2. download aspectj1.9.6 (most recent stable release)
    this is a java jar file

  3. double click the jar, it will self-install

  4. Follow the end suggestions... making chages to environment variables Path and CLASSPATH so ajc can be found

  5. Fire up a DOS window (command prompt) and try "ajc" to make sure the AspectJ compiler is findable

  6. Try this HelloWorld example.
    Make two files in a directory

    File "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 --");
          }
    
        }
    

  7. 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. 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