Recipe 9.3. Capturing All Join Points Within a Particular MethodProblemYou want to capture all the join points within the scope of a particular method or methods. SolutionUse the withincode(Signature) pointcut. The syntax of the withincode(Signature) pointcut is: pointcut <pointcut name>(<any values to be picked up>) : withincode(<modifier> <class>.<method>(<parameter types>)); DiscussionThere are three key characteristics of the withincode(Signature) pointcut:
Example 9-3 shows the withincode(Signature) pointcut capturing all join points within the scope of the * MyClass.foo(int,String) method. Example 9-3. Using the withincode(Signature) pointcut to capture all join points within a method's scopepublic aspect WithinMethodRecipe { /* Specifies calling advice whenever a method matching the following rules gets called: Class Name: MyClass Method Name: foo Method Return Type: * (any return type) Method Parameters: an int followed by a String */ pointcut withinFooIntStringAnyReturnPointcut( ) : withincode(* MyClass.foo(int, String)); // Advice declaration before( ) : withinFooIntStringAnyReturnPointcut( ) && !within(WithinMethodRecipe +) { System.out.println( "-------------- Aspect Advice Logic ---------------"); System.out.println( "In the advice picked by withinFooIntStringAnyReturnPointcut"); System.out.println( "Join Point Kind: " + thisJoinPoint.getStaticPart( ).getKind( )); System.out.println( "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getStaticPart( ).getSourceLocation( )); System.out.println( "--------------------------------------------------"); } } Figure 9-2 shows how the withincode(Signature) pointcut is applied. Figure 9-2. How the withincode(Signature) pointcut is appliedSee AlsoRecipe 4.1 shows some of the wildcard variations that can be used in a Signature; the withincode(Signature) pointcut can be negated to remove a specific method from the scope of the weaving using the NOT (!) operator as described in Recipe 12.4; Chapter 13 describes the different types of advice available in AspectJ. |