/* My first AspectJ program! AspectJ is the most well-developed "aspect oriented" programming language. It is a radical extension of Java. All downloads and documentation are found at http://eclipse.org/aspectj/ To install on Ubuntu Linux in bash (easiest option): sudo apt install default-jdk (if you don't have java installed already) sudo apt install aspectj export CLASSPATH=./:/usr/local/aspectj1.8/lib/aspectjrt.jar To install Widows/Macs: make sure you have jdk (1.8 or higher - download from java.sun.com) first. Then download the latest "stable release" of the aspectj_.jar file, do Edit your PATH to include ..\jdk..\bin Do java -jar aspectj-1.8.1.jar (or whatever the current version of the .jar that you downloaded)- this will open a graphical installation dialog. Change the version number to whatever you downloaded. At one point during the installation it will ask you for the directory where you installed java (jdk). MAKE SURE IT'S the right one! That is, make sure that it's pointing to the same jdk (not jre or the bin subdirectory) directory that you run java and javac from. Once installed, you need to: 1. Add the aspectj1.8/bin directory to your PATH environment variable. 1b. This is assuming that aspectj1.8 is the installation directory. 2. Add aspectj1.8/lib/aspectjrt.jar to your CLASSPATH (create variable if it doesn't exist). Make sure the CLASSPATH also contains the current (./) directory: export PATH=/usr/local/aspectj1.8/bin:$PATH export CLASSPATH=./:/usr/local/aspectj1.8/lib/aspectjrt.jar On my Mac: export CLASSPATH=./:/Users/cscccl/aspectj1.8/lib/aspectjrt.jar You may have to do it slightly differently ... AspectJ is built as an extension to Java and compiles to JVM code. Use "ajc afirst.java" to compile and "java afirst" to run. ***On Macs, I can't firgure out where all the .jrt files are located so I neede to compile and run by specifying the CLASSPATH: (or use -cp option each time) ***On Windows, do NOT run aspectj inside cygwin: run it inside the windows command prompt. Installation Notes: make sure you know which version of javac and java that you're using (recommend jdk1.8, 64 bit, from oracle (java.sun.com)). */ import org.aspectj.lang.JoinPoint; public class afirst { public int f(int z) { return z*z; } public void g(int x) { System.out.println(f(x+1)); } public static void main(String[] args) { int i = 3; afirst myfirst = new afirst(); myfirst.g(i++); } } /// nothing out of the ordinary so far, but: aspect recorder { pointcut callf() : call(int afirst.f(int)); pointcut callg(int x) : call(void afirst.g(int)) && args(x); // advice: before() : callf() { System.out.println("--- about to call f"); System.out.println(thisJoinPoint); } // a before advice after(int n) returning : callg(n) { System.out.println("--- Just called g with "+n); } // replaces joinpoint with new code - why would you want to do that? int around (int x) : callf() && args(x) { return x+x; } } // recorder aspect // Note that you don't have to create an instance of the aspect to make it // run as part of the program. There are some characteristics of "declarative" // programming here. // The aspect should be placed in a different compilation unit (file) so // that it can be loaded/unloaded simply by including it in the compilation. /* One benefit of AOP is the ability to add to existing code to make it more robust - sometimes we write code without checking for all special cases - like withdrawing a negative amount from a bank account. We can define a pointcut and an advice to prevent that from happening without modifying existing code, which is always an iffy thing. What if you have several classes, several methods, that needs to implement a common functionlity. You can currently modularize that inside another class/method. But you'll still have to pass pointers around to share common structures, and invoke certain methods. All your code have to follow a certain protocol - this makes it difficult to modify. But you can centralize this common "aspect" of your code inside an 'aspect'. How does this compare with functional and/or class-level abstraction? */