Recipe 4.4. Capturing a Method When It Is ExecutingProblemYou want to capture when methods that match a specific signature are executing. SolutionUse 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>); DiscussionThe execution(Signature) pointcut has two key characteristics:
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 methodpublic 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 appliedAt 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 AlsoThe 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. |