Recipe9.1.Capturing All Join Points Within a Particular Class


Recipe 9.1. Capturing All Join Points Within a Particular Class

Problem

You want to capture all the join points within a particular class.

Solution

Use the within(TypePattern) pointcut, using the TypePattern to specify the particular class type pattern. The syntax of the within(TypePattern) pointcut is:

pointcut <pointcut name>(<any values to be picked up>) : within(<class>);

Discussion

The within(TypePattern) pointcut has three key characteristics:

  1. The within(TypePattern) pointcut captures all join points within the scope of the specified class.

  2. The within(TypePattern) 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 TypePattern can include wildcard characters to select a range of join points on different classes.

Example 9-1 shows the within(TypePattern) pointcut being used to capture all the join points within the MyClass class.

Example 9-1. Using the within(TypePattern) pointcut to capture join points within a specific class
public aspect WithinClassRecipe  {    /*        Specifies calling advice on any join point encountered within         the defined scope:            Scope: MyClass    */    pointcut withinMyClass( ) : within(MyClass);    // Advice declaration    before( ) : withinMyClass( ) && !within(WithinClassRecipe +)    {       System.out.println(          "-------------- Aspect Advice Logic ---------------");       System.out.println(          "In the advice picked by " + "withinMyClass( )");       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 9-1 shows how the within(TypePattern) pointcut is applied to a single class.

Figure 9-1. How the within(TypePattern) pointcut is applied


See Also

Recipe 5.1 shows some of the wildcard variations that can be used in a TypePattern; see the recipes in Chapter 12 for examples of the mechanisms by which pointcuts can be combined; the within(TypePattern) pointcut is often negated to remove an aspect from the scope of the weaving using the unary NOT (!) operator as described in Recipe 12.4; Chapter 13 contains recipes that describe 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