Recipe 11.2. Capturing When a Join Point's Target Object Is a Specific TypeProblemYou want to capture all join points encountered when a join point's target object, if any, is of a specific type. SolutionUse the target([Type | Identifier]) pointcut. The syntax of the target([Type or Identifier]) pointcut is: pointcut <pointcut name>(<any values to be picked up>) : target(<type> or <identifier> or *); DiscussionThe target([Type | Identifier]) pointcut has five key characteristics:
Example 11-2 shows two examples of the target([Type | Identifier]) pointcut being used to:
Example 11-2. Using the target([Type | Identifier]) pointcut to capture join points based on the type of a methods targetpublic aspect TargetRecipe { /* Specifies calling advice whenever the target of a methods is of a type that matches the following rules: Identifier/s: MyClass object */ pointcut targetIdentifierMyClassPointcut(MyClass object) : target(object); /* Specifies calling advice whenever the target of a methods is of a type that matches the following rules: Type Pattern: AnotherClass */ pointcut targetTypePatternAnotherClassPointcut( ) : target(AnotherClass); // Advice declaration before(MyClass object) : targetIdentifierMyClassPointcut(object) && !within(TargetRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice picked by targetIdentifierMyClassPointcut"); System.out.println("Join Point Kind: " + thisJoinPoint.getKind( )); System.out.println("Object referenced by Target passed by Identifier: " + object); System.out.println( "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getStaticPart( ) .getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } // Advice declaration before( ) : targetTypePatternAnotherClassPointcut( ) && !within(TargetRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice picked by targetTypePatternAnotherClassPointcut"); System.out.println("Join Point Kind: " + thisJoinPoint.getKind( )); System.out.println( "Object referenced by Target: " + thisJoinPoint.getTarget( )); System.out.println( "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getStaticPart( ) .getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } } Figure 11-2 shows how the target([Type | Identifier]) pointcut is applied. Figure 11-2. How the target(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. |