CPTTM software developer newsletter issue #6

Topics in this issue:

Introduction to Aspect Oriented Programming (AOP)

Suppose that you have a class:

public class Foo {
public void myMethod1() {
System.out.println("hello1");
}
public void myMethod2() {
System.out.println("hello2");
}
}

Suppose that you would like to print a log message whenever any such method is called. A simple way is to add a println to each method:

public class Foo {
public void myMethod1() {
System.out.println("entering...");
System.out.println("hello1");
}
public void myMethod2() {
System.out.println("entering...");
System.out.println("hello2");
}
}

However, this is troublesome if you have lots of methods in Foo. Using AOP, we can state that before any public void method in the Foo class is called, run this println code. More formally, this can written as:

before: call(public void Foo.*()) {
System.out.println("entering...");
}

The call(...) expression is a condition to select the execution points (the current lines as in a debugger) calling a public void method in Foo. In AOP, an execution point is called a "joint point". Here we're not interested in any joint point, but just those joint points that are calling a public void method in Foo. The call(...) expression is called a "pointcut". It is just a condition to select the joint points that we're interested in. The keyword "before" and the println code together is called an "advice". That is, what to do before or after a joint point is executed.

Actually, we can't just write an advice. An advice must belong to an "aspect":

aspect LogFoo {
before: call(public void Foo.*()) {
System.out.println("entering...");
}
}

An aspect is like a class. It can contain one or more advices. To see the effect of the advice, create a Bar class to call a method in Foo:

	public class Bar {
public static void main(String[] args) {
Foo foo = new Foo();
foo.myMethod1();
}
}

Run it and it will print:

	entering...
hello1

Well, actually it won't run yet. We have to write "before()" instead of "before" in the advice:

aspect LogFoo {
before(): call(public void Foo.*()) {
System.out.println("entering...");
}
}

This is because an advice can extract information from the joint point (e.g., what's the object being called?) and store it  into parameters for use by the code in the advice. Here we're not extracting anything, so we don't have any parameter.

You may wonder how can the aspect intercept the call to Foo.myMethod1() in Bar? There are different ways to do it. An AOP implementation may note your LogFoo aspect and modify the Java byte code when compiling Bar.java. Another one may intercept the call at runtime.

To learn more about AOP, see the AspectJ Programming Guide. To actually run the code above, get the AspectJ Development Tools for Eclipse and choose to create an AspectJ project. In addition, Spring also provides AOP support and thus you don't have to use another IDE or plugin. You can take our upcoming Spring course to learn more.

Got it? Now, click here to take a quiz to see if you have really understood.

Our Software Developer Incubation 2005 is completed

SDI (Software Developer Incubation) is a CPTTM training program in which six students and six experienced programmers are recruited to work on a real software development project. Each student works with an experienced programmer on the same computer (pair programming) so that he can learn the skills from the experienced programmer. The SDI in 2005 has completed. The project is a workflow system using Java, OpenWFE, Tapestry and Hibernate and running in Tomcat and with HSQLDB. The tools used include Eclipse, Spindle, JUnit, HtmlUnit, Subclipse and Subversion. For more info, click here.

Feedbacks

Have any questions, ideas or experiences regarding software development? Contact me at 781313 or kent at cpttm dot org dot mo.

Until next time, 

Kent Tong