Aspect-Oriented Programming and Lisp

Written by on November 13, 2015 in LISP, Programming with 0 Comments

Aspect-oriented programming (AOP) is a popular approach to handle cross-cutting concerns in an application. Common examples of cross-cutting functionality are Logging, Error Handling and Transaction Management.

AspectJ is the original AOP extension created for Java in 2001. AOP extensions have been developed for some other languages also.

There are many good books and online resources on AOP, so I won’t go into that part.

One of the aspects in AOP is an advice that allows code to be executed before, after or around a join point. Similar feature has been in Lisp for a long time. As part of the standard method combination, Common Lisp supports the definition of the primary method and :before, :after and :around auxiliary methods.

In this post, let us look at a simple class hierarchy involving vehicles. We define a Car class and a Truck class, both inheriting from a Vehicle class. There is a primary print-info method defined on the Vehicle class. This is the method that is normally invoked on any type of vehicle, since there is no specialisation of the method for Car or Truck. We can define such specialisations (for example, on Car) if we want.

Lisp Method Combination

Lisp Method Combination

Notice that in addition to the primary method, we have defined a few auxiliary methods, identified by the keywords :before, :after, and :around. The :before methods will be called before the primary method executes and the :after methods will be called after the primary method is executed. When many methods are applicable, the most applicable method will be invoked first, followed by the next applicable method, and so on.

The :around method is treated differently. It will be called before any other auxiliary method and before the primary method. It can choose to return directly without passing control to the other methods. Or, it can trigger the conventional chain of calls by invoking a special method call-next-method. This will result in calls to the :before methods, followed by the primary method, and then the :after methods.

Checking Method Combination

Checking Method Combination

For the sake of this example, since our focus is on AOP, I have greatly simplified my description of the standard method combination model in Lisp. Interested readers should go through the Common Lisp Reference Manual, or another good resource (see my earlier blog for a good list of reading materials).

The example Lisp code can be downloaded here.

Tags: ,


If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *