Recipe 10.2. Capturing All Join Points Within a Program's Control Flow, Excluding the Initial Join PointProblemYou want to capture all join points encountered within the program control flow after the initiating join point selected by a separate pointcut. SolutionUse the cflowbelow(Pointcut) pointcut. The syntax of the cflowbelow(Pointcut) pointcut is: pointcut <pointcut name>(<any values to be picked up>) : cflowbelow(<pointcut>); DiscussionExample 10-2 shows the cflowbelow(Pointcut) pointcut being used to capture all of the join points that occur after an initial join point captured by the callInitialPointcut( ) pointcut. Example 10-2. Using the cflowbelow(Pointcut) pointcut to capture join points after an initially captured join pointpublic aspect CFlowBelowRecipe { /* Specifies calling advice whenever a method matching the following rules gets called: Class Name: MyClass Method Name: foo Method Return Type: void Method Parameters: an int followed by a String */ pointcut callInitialPointcut( ) : call( void MyClass.foo(int, String)); /* Specifies calling advice whenever a join point is encountered after but excluding the initial join point that triggers the pointcut that is specified in the parameter: Pointcut Name: callInitialPointcut */ pointcut cflowPointcut( ) : cflowbelow(callInitialPointcut( )); // Advice declaration before( ) : cflowPointcut( ) && !within(CFlowBelowRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice attached to the cflowbelowPointcut point cut"); System.out.println( "Join Point Kind: " + thisJoinPoint.getKind( )); System.out.println( "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getStaticPart( ).getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } } Figure 10-2 shows how the cflowbelow(Pointcut) pointcut can be applied. Figure 10-2. How the cflowbelow(Pointcut) pointcut can be appliedThis recipe differs only slightly from Recipe 10.1; the difference is in the number of join points that are actually captured. Whereas the cflow(Pointcut) pointcut triggers advice on all join points encountered within an initial join point's context including the initial join point, the cflowbelow(Pointcut) pointcut excludes that initial join point. See AlsoRecipe 10.1 describes the cflow(Pointcut) pointcut; Chapter 12 describes techniques for combining pointcut definitions; more information on a join point's context and environment is available in Chapter 13. |