Combining Java Annotations and Aspects

Aspect-Oriented Programming

I had been looking for a while for some practical case to get my hands dirty with Aspect-Oriented Programming (AOP) in Java. This article is an account on how I did it.

It is not my goal here to give an overview of AOP or AspectJ (there are plenty good places for that), but to illustrate a way of using aspects for a practical purpose. I will combine the usage of aspects with Java annotations in order to provide some useful parameters to the aspects.

Those among you who do not know about AOP can get some detailed description in the wikipedia entry or in an introductory article by IBM rational. There is also the AspectJ programming guide for a more complete reference of AspectJ in particular, the tool that I used.

For those of you who have already dived into Python, you may find this technique leads to something that looks similar to decorators.

A Practical Use Case for Aspects

I was looking for ways to limit access to some specific functionality in my code depending on some acquired rights (these were in our case were represented by license files, stating which modules were allowed to run at a specific machine… but this is not specially relevant to the story). I wanted to do that in a clean way that did not mess the existing codebase. In fact, I liked the declarative nature of annotations and found it well suited:

private void generateTimeStamp(...) {

Continue reading