Tuesday, July 12, 2011

AOP - Notes on Pointcuts

A pointcut has the following syntax: [access specifier] pointcut pointcut-name([args]) : pointcut-definition.

There are five parts of a pointcut as shown above:
1. access specifier
2. pointcut keyword
3. pointcut name
4. pointcut arguments
5. pointcut definition

Pointcut definition consists of pointcut type and pointcut signature, it can be seen as defined-join-point to be captured. To keep things simple, lets list some of the most commonly used pointcut type:

1. execution(MethodSignature) : Method or constructor execution, i.e. execution(public void edu.mat.hello.MessageCommunicator.deliverGlobalMessage(String)) which means capture all join point on the execution of method deliverGlobalMessage in class MessageCommunicator.

2. call(MethodSignature) : Method or constructor call, i.e. call(public void edu.mat.hello.MessageCommunicator.deliverGlobalMessage(String)) which means capture all join points on the calling of method deliverGlobalMessage in class MessageCommunicator.

3. staticinitialization(TypeSignature) : Class initialization, i.e. staticinitialization(edu.mat.hello.MessageCommunicator) which means capture all join points on MessageCommunicator class loading.

4. get(FieldSignature) : Field read access, i.e. get(private String edu.mat.hello.MessageCommunicator.name) which means capture all join points on getter method of private variable name in class MessageCommunicator.

5. set(FieldSignature) : Field write access, i.e. set(private String edu.mat.hello.MessageCommunicator.name) which means capture all join points on setter method of private variable name in class MessageCommunicator.

6. handler(TypeSignature) : Exception handler execution.

7. initialization(ConstructorSignature) : Object initialization, i.e. initialization(public edu.mat.hello.MessageCommunicator.new()) which means capture all join points on object MessageCommunicator creation.

8. within(TypePattern) : Capture join points in a scope, i.e. !within(AccountAspect) && !within(edu.mat.hello.MessageCommunicator) && !within(MessageCommunicatorAspect) which means capture all join points outside AccountAspect aspect and MessageCommunicator class and also outside MessageCommunicatorAspect aspect.

A pointcut could have arguments as shown in the pointcut syntax above. We can capture the values of the arguments using args. For example: private pointcut sayMyName(String person) : call(* edu.mat.hello.MessageCommunicator.sayMyName(String, String)) && args(person, String); which means capture all pointcuts in sayMyName method of class MessageCommunicator and grab first argument (named person) as shown in args(person, String).

We can also capture the caller of being executed join point using target. For example we have the following pointcut private pointcut setNameField() : set(private String edu.mat.hello.MessageCommunicator.name); and private pointcut targetObject(edu.mat.hello.MessageCommunicator object) : target(object) && setNameField();.

In the first pointcut we want to capture all join points on setter method of name variable on class MessageCommunicator. In the second pointcut, we want to capture all join points on previous pointcut (when name variable is set) and grab the MessageCommunicator object which calls that setter method, that is the target.

0 comments:

 

©2009 Stay the Same | by TNB