Saturday, May 31, 2014

Aspect-oriented Programming (AOP) - Pointcuts - 2

We have mentioned the call and execution pointcuts in the post Pointcuts - 1 .



There are several other useful pointcuts expressions, we will first describe static ones and then dynamic ones. Static ones are those that they can be analyzed at compilation time, and therefore, part of the weaving (applying the aspect at the correct place) is done statically, thus avoiding usual aspect overhead. Dynamic pointcuts expressions depend on the runtime state of the system. A certain joinpoint may match the pointcut at some point of time but not at another.

Some interesting static pointcut expressions (besides call and execution) are:
get/set (fieldPattern) do detect whenever a value read or written to a field (wildcards are allowed here too); 
initialization -> to capture constructors; 
within / withinCode -> to statically check if the pointcut is within a certain class or method name

Some interesting dynamic pointcut expressions are:
cflow(pointcut): captures any joinpoint within the execution flow of the pointcut. 
For example, if there is a pointcut: cflow (call(* *.foo()) and within foo() there is a call to bar(); then that call is within the execution flow of call(foo) therefore the advice with that pointcut will be activated. However, when bar is called within a stack trace where foo has not occurred, the matching advice will not be activated.
this, target, args: All this allow not only capturing dynamic information but also exposing it to be used within the advice. For instance, let's consider the pointcut
call(* *.foo()) && this(C)
This pointcut expresses any call to C where the call is done by an object of class C.
Moreover if we define the pointcut as follows:
pointcut p(C objC):call(* *.foo()) && this(objC)
we are saying the same as before but also saving the reference to the object calling foo in objC.
Similarly, with target we can save the reference of the targeted object, and with args, the arguments with which the call is being made.


Besides static and dynamic pointcut descriptors, more complex pointcuts can be obtained by boolean composition:
&& -> and among pointcuts
|| -> or among pointcut
! -> not pointcut

No comments:

Post a Comment