Articles

An Introduction to Aspect-Oriented Programming and AspectJ

By Erhan Kartaltepe

Object-oriented programming (OOP) guides developers to exploit the expressive power of the object model, using classes as their fundamental building blocks. However, often classes are too specialized for general “drop” reuse and are tangled, as they implement many requirements of a system. Other times, classes are so useful as to be used in many places (that is, scattered in many classes), such as security or session expiration mechanisms. Tangling and scattering can lead to impaired comprehension and an increased complexity in the addition, removal, or modification of requirements. Tangled code makes a class difficult to reason about, and difficult to change as many requirements are realized together. A real-life example of scattering is the org.apache.tomcat project in the figure below. Red shows the relevant lines of code. The XML parsing capability of the project is well-modularized in one file, whereas the project's logger is used in a large number of places.


XML parsing (left) and logging (right) in org.apache.tomcat (1)

(Click to Enlarge)

Because scattering and tangling defy the OOP paradigm, they are symptoms of “crosscutting concerns”, as they cut across many modules in a program. Aspect-oriented programming (AOP) is an attempt to corral these naturally, with aspects being well-modularized crosscutting concerns. Gregor Kiczales and his team at Xerox PARC originated the concept of AOP and its most popular general purpose language, AspectJ (2). Other frameworks such as Spring and Spring.NET (3) also incorporate some of its features to a certain degree. Because AspectJ is the most well-known and is a superset of Java (i.e., all AspectJ programs compile to Java bytecode), the examples in this article use AspectJ.

Terminology

In AOP, points in the code where we would want to run our code (instead of writing it in the class itself) are called join points. A join point can be almost any line of code—a method or constructor call, a field get or set, object initialization, or exception handler execution. AspectJ intercepts program execution flow at these join points and runs the code we want to run instead, called advice, which can happen before, after, or instead of the join point. An aspect describes these join points as point cuts, which can describe one or more join points.

Example

An example Java program using a simple logging mechanism is shown below. In MainClass.java, two methods are being called, and with code from Logger.java being executed when each method (save the main method) is entered and left.


MainClass class (left) and Logger aspect (right)

(Click to Enlarge)

In the AspectJ program below, any reference to logging in MainClass.java is removed. In place of the Logger class, the Logger aspect encapsulates the logging concern. It has one pointcut called SayAnything that describes all join points that are methods being called (using the call keyword) that starts with “say” and has any number of parameters. Two advice are associated with the pointcut. The before advice gets the signature of the join point it is in, and prints “Entering” with the signature’s name. The after advice does the same, printing “Leaving” with the signature's name instead. Thus, whenever any of these join point is reached in the execution of the program, it calls the code in the before advice, executes as normal until the end of the join point, and calls the after advice.


MainClass class (left) and Logger aspect (right)

(Click to Enlarge)

At compile time, the compiler weaves the code from an aspect’s advice into the join points their pointcuts describe, leaving .class files that any Java runtime may execute. Because aspects are a superset of classes, they can contain members and methods, and can even extend other aspects or classes, although classes cannot extend aspects. Also, aspects can be quite powerful, able to read and process code with around advice, affecting the execution of the program at runtime in dynamic and very interesting ways. By the same token, AOP and AspectJ's power lend itself to augment code outside its boundaries, possibly leading to software contract-breaking. Like in any paradigm, a little knowledge is a dangerous thing, but a little more knowledge can do wonders.

Resources

http://www.parc.xerox.com/research/projects/aspectj/downloads/
ECOOP1997-AOP.pdf

The seminal paper on Aspect-Oriented Programming, written by Greg Kiczales.

http://www.aspectj.org
The homepage for the AspectJ Project, the most widely used AOP language

http://www.eclipse.org/aspectj/doc/released/progguide/index.html
The AspectJ Programming Guide—useful for learning AspectJ and the Join Point model.

http://www.springframework.org/docs/reference/aop.html
An AOP reference when using the Spring.NET framework

http://www.eclipse.org/ajdt
AspectJ Development Tools for Eclipse, a set of plugins for Eclipse that provide AOP support

http://www.eclipse.org/aspectj/publications.php
A good list of popular books on AspectJ

Works Cited

1. IrisaTech. Introduction to Aspect-Oriented Software Development. Model-Driven Engineering with Contract, Patterns, and AspectJ. [Online] March 31, 2006. [Cited: October 8, 2007.] http://www.irisa.fr/videos/irisatech/DevelopLogiciels/jezequel/IntroAOSD.pdf.

2. Eclipse.org. The AspectJ Project. The AspectJ Project. [Online] September 25, 2007. [Cited: October 8, 2007.] http://www.eclipse.org/aspectj.

3. Spring.NET. Spring.NET Application Project Framework. Spring.NET Application Project Framework. [Online] August 10, 2007. [Cited: October 8, 2007.] http://www.springframework.net.