Recipe 9.1. Capturing All Join Points Within a Particular ClassProblemYou want to capture all the join points within a particular class. SolutionUse 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>); DiscussionThe within(TypePattern) pointcut has three key characteristics:
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 classpublic 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 appliedSee AlsoRecipe 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. |