Recipe9.3.Capturing All Join Points Within a Particular Method


Recipe 9.3. Capturing All Join Points Within a Particular Method

Problem

You want to capture all the join points within the scope of a particular method or methods.

Solution

Use 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>));

Discussion

There are three key characteristics of the withincode(Signature) pointcut:

  1. The withincode(Signature) pointcut specifies all join points within the local scope of a particular method.

  2. The withincode(Signature) pointcut is rarely used in isolation. Rather, it is usually combined with other pointcuts to narrow the join points that will trigger the attached advice.

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

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 scope
public 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 applied


See Also

Recipe 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.



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