Showing posts with label pointcuts. Show all posts
Showing posts with label pointcuts. Show all posts

Sunday, June 1, 2014

Aspect-oriented Programming (AOP) - Static Pointcuts - Exercises



Try writing the following pointcuts statically capturing some joinpoints:

(According to the posts AOP - Pointcuts 1AOP - Pointcuts 2)
  1. The pointcut that captures any call to any int method containing the word "Method" in its name. For example:
class C{
  public static void main(){
      D d = new D();
1.    d.thisMethodIsCool();
  }
}

class D{
  public int thisMethodIsCool(){
2.    System.out.println("within the cool method");
  }
}

The pointcut should match the call in line 1 of class C.



pointcut callMethod(): call(int *.*Method*(..))


  1. The same as before but now capture the execution of the method, i.e., when the eecution flow is within line 2 of class D.

pointcut executeMethod(): execution(int *.*Method*(..))



  1. This pointcut should match whenever a String field named "name" is set

pointcut setName(): set(String *.name)

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

Wednesday, May 28, 2014

Aspect-oriented Programming (AOP) - Pointcuts - 1

So far we have talked very little about pointcuts, but there are several syntax keywords that allow capturing different joinpoints.

Here is the whole list: Pointcuts, but I will point out some that are very useful (and used):

call(MethodPattern): whenever a method is called. MethodPattern is an expression (containing wildcards) that may capture different sets. For instance public int C.m(String) is a method pattern capturing the public method m of class C that receives one argument of type String. We can also define the following * *.*(..) which represents, any method of any class of any type with any set of arguments. Something in the middle could be * Cl*.me*(String, ..) which captures methods (of any type) that begin with me, of classes that start with Cl and receive at least one String argument (as the first  argument).

execution (MethodPattern), is similar but represents when we are in the class of the method pattern. To observe the difference between call and execution let's consider the following example:

1. class MainClass {
2.   public static void main(){
3.      C o1 = new C();
4.      o1.m();
5.   }
6. }

1. class C {
2.   public void m(){
3.   }
4. }

the call pointcut matches the location at MainClass, line 4; while the execution pointcut matches the execution within C that is, between lines 2 and 3 of class C.