2010 13 Aug
Software development process should take into account many considerations. In general, we can divide them into two categories: functional which correspond to the main functionality of the software, and non-functional as security, persistence, maintenance, etc. This paradigm of separation leads to improve the modularity of software.
Aspect Oriented Programming (AOP) is a new programming paradigm dealing with this concept. An aspect is a software entity implementing a specific non-functional part of the application.
It brings together joinpoints, pointcuts and code advice. A joinpoint is a point in the program where aspects can act, e.g., methods, constructors, and classes, etc. pointcut is an element linked to the program execution flow where it is possible to graft an aspect around. From an abstract point of view, a pointcut defines a set of joinpoints, a "cut". The code advice defines the grafted code by the aspect into the original application.
In the following we focus in a particular Aspect Oriented Programming AspectJ, which is an extension of Java. AspectJ has its own compiler that combines components and aspects between them, and convert the source code into another interpretable by a Java virtual machine.
Joinpoint
Joinpoint designate a specific point in the program execution program. This refers to items such as method-call, assignment instruction, etc.
AspectJ allows defining the following joinpoint:
- function-call, constructor
- reading/writing access
- bloc execution as "catch" that treats a Java exception
Pointcuts
Pointcut allows a syntactical definition of a set of joinpoint. The following example shows a simple pointcut. This pointcut(line 1) defines the joinpoints which correspond to the execution of the method with the signature <method signature>. The pointcut (line 2 defines the joinpoints which correspond to the execution of the method MyMethod in the class MyClass and which takes an arbitrary number of arguments, and with a void return value and any right access (public, private,..).
1- pointcut <pointcut name> : execution (<method signature>)
2- pointcut traceprint : execution (void MyClass.MyMethod(..))
A simple example for pointcut.
Code Advice
Code advice defines the set of instructions to be added by the aspect. It is associated to a specific pointcut, and hence, a set of jointpoints. We distinguish three main type of code advice, which differ on how the block of code will be executed:
- before: the code is executed before the joinpoints
- after: the code is executed after the joinpoints
- around: the code is executed before and after the joinpoints
In the case of around code advice, it is necessary to delineate the part of the code to be executed before the joinpoints and the one to be executed after the joinpoints. For this reason, AOP tools provide specific method called proceed ().
The progress of a program with a around code advice can be summarized as follows:
- execution until joinpoint
- just before the joinpoint execution of the before part of the code advice
- if there is a proceed call
- execution of the code corresponding to the jointpoint
- execution of the code after proceed
- Resuming execution of the program.
The following example shows an aspect defining a security rule. If the user is not a admin we do not apply proceed, and hence, we do not execute the method f (), we print "non permission" and we resume execution without execution of the function f (). Otherwise, we call proceed, and hence, we execute normally the method f ().
1 public aspect security {
2 pointcut f(): execution (* f(..));
3 Object around() :: f() {
4 if(authentication()!= admin) {
5 System.out.println(" non permission ");
6 }
7 else {
8 Object x = proceed();
9 System.out.println("the method is well executed");
10 return x;
11 }
12 } }
Aspect for access control of a method.
Article by Mohamad Jaber
PhD Student at Verimag
June 15, 2010



