Recipe4.4.Capturing a Method When It Is Executing


Recipe 4.4. Capturing a Method When It Is Executing

Problem

You want to capture when methods that match a specific signature are executing.

Solution

Use the execution(Signature) pointcut. The syntax of the execution(Signature) pointcut is:

pointcut <pointcut name>(<any values to be picked up>) :      execution((<optional modifier> <return type> <class>.  <method>(<paramater types>);

Discussion

The execution(Signature) pointcut has two key characteristics:

  1. The context of a triggering join point is within the target class method.

  2. The Signature can include wildcard characters to select a range of join points on different classes and methods.

Example 4-4 shows the execution(Signature) pointcut being used to declare an interest in method execution join points on any method that matches the signature MyClass.foo(int,String).

Example 4-4. Using the execution(Signature) pointcut to catch join points within the execution of a method
public aspect ExecutionRecipe  {    /*        Specifies calling advice whenever a method         matching the following rules enters execution:                Class Name: MyClass        Method Name: foo        Method Return Type: int        Method Parameters: an int followed by a String    */    pointcut executionPointcut( ) : execution(void MyClass.foo(int, String));    // Advice declaration    before( ) : executionPointcut( ) && !within(ExecutionRecipe +)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println("In the advice picked by ExecutionRecipe");       System.out.println(          "Signature: "             + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: "             + thisJoinPoint.getStaticPart( ).getSourceLocation( ));       System.out.println(          "------------------------------------------------------------");    } }

Figure 4-2 shows how the execution(Signature) pointcut is applied to a simple class.

Figure 4-2. How the execution(Signature) pointcut is applied


At first, the execution(Signature) pointcut appears to offer nothing more than the call(Signature) pointcut described in the previous recipe. The important thing to remember with this recipe is where the advice is invoked and what is its context.

In the case of the call(Signature) pointcut, the advice is invoked where the method is invoked. The context of the advice invocation is the calling class. The execution(Signature) pointcut is invoked once the method has been entered and therefore the calling context is the method being executed.

Finally, if you haven't already read Recipe 4.1, then it is worth going back a couple of pages to read about the strange behavior that the call(Signature) and execution(Signature) pointcuts can have when capturing join points on an object's methods that are inherited and/or overridden depending on the object's static and dynamic type.

See Also

The subtle characteristics of call(Signature) and execution(Signature) pointcuts when capturing join points in inherited or overridden methods are explained in more detail in the report available at www.xs.iastate.edu/~leavens/FOAL/papers-2004/barzilay-etal.pdf; Recipe 4.1 shows some of the wildcard variations that can be used in a Signature; how to capture parameters on a join point, in particular on a method call, is shown in Recipe 4.2; the calling context that is available to advice is covered in Chapter 13.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net