Recipe 11.1. Capturing When the this Reference Is a Specific TypeProblemYou want to capture all join points where the this reference at a join point is of a specific type. SolutionUse the this([Type | Identifier]) pointcut. The syntax of the this([Type | Identifier]) pointcut is: pointcut <pointcut name>(<any values to be picked up>) : this(<type> or <identifier> or *); DiscussionThe this([Type | Identifier]) pointcut examines the this reference at the captured join point to decide whether to invoke the associated advice and has five key characteristics:
Example 11-1 shows two examples of the this([Type | Identifier]) pointcut being used to:
Example 11-1. Using the this(Type | Identifier) pointcut to capture join points based on the type of the this referencepublic aspect ThisRecipe { /* Specifies calling advice whenever the executing object is of a type that matches the following rules: Identifier/s: MyClass object */ pointcut thisIdentifierMyClassPointcut(MyClass object) : this(object); /* Specifies calling advice whenever the executing object is of a type that matches the following rules: Type Pattern: AnotherClass */ pointcut thisTypePatternAnotherClassPointcut( ) : this(AnotherClass); // Advice declaration before(MyClass object) : thisIdentifierMyClassPointcut(object) && !within(ThisRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice picked by thisIdentifierMyClassPointcut"); System.out.println("Join Point Kind: " + thisJoinPoint.getKind( )); System.out.println( "this reference as passed by Identifier " + object); System.out.println( "Object referenced by this: " + thisJoinPoint.getThis( )); System.out.println( "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getStaticPart( ).getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } // Advice declaration before( ) : thisTypePatternAnotherClassPointcut( ) && !within(ThisRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice picked by thisTypePatternAnotherClassPointcut"); System.out.println("Join Point Kind: " + thisJoinPoint.getKind( )); System.out.println( "Type of executing object: " + thisJoinPoint.getThis( ).getClass( ).getName( )); System.out.println( "Object referenced by this: " + thisJoinPoint.getThis( )); System.out.println( "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getStaticPart( ).getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } } Figure 11-1 shows how the this([Type | Identifier]) pointcut is applied. Figure 11-1. How the this([Type | Identifier]) pointcut is appliedSee AlsoThe calling context that is available to advice is covered in Chapter 13; the handler(TypePattern) pointcut is described in Recipe 5.1; the preinitializa-tion(Signature) pointcut is described in Recipe 7.4; the staticinitializa-tion(TypePattern) pointcut is described in Recipe 7.5; the within(TypePattern) pointcut is described in Recipe 9.1; the NOT(!) operator is described in Recipe 12.4. |