Recipe 11.3. Capturing When the Arguments to a Join Point Are a Certain Number, Type, and OrderingProblemYou want to capture all join points encountered when a join point's arguments, if any, are of a specific type, number, and ordering. SolutionUse the args([Types | Identifiers]) pointcut. The syntax for this pointcut is: pointcut <pointcut name>(<any values to be picked up>) : args(<types> or <identifiers> | .. | *, <repeat>); DiscussionThe args([Types | Identifiers]) pointcut has seven key characteristics:
Example 11-3 shows two examples of the args([Types | Identifiers]) pointcut being used to:
Example 11-3. Using the args([Types | Identifiers]) pointcut to capture join points based on the types of a methods argumentspublic aspect ArgsRecipe { /* Specifies calling advice whenever the type of a methods argument is of a type that matches the following rules: Identifier/s: MyClass object */ pointcut argIdentifierMyClassPointcut(MyClass object) : args(object); /* Specifies calling advice whenever the type of a methods argument is of a type that matches the following rules: Type Pattern: AnotherClass */ pointcut argTypePatternAnotherClassPointcut( ) : args(AnotherClass); // Advice declaration before(MyClass object) : argIdentifierMyClassPointcut(object) && !within(ArgsRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice picked by argIdentifierMyClassPointcut"); 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( "Arguments picked up using Identifiers: " + object); System.out.println( "------------------------------------------------------------"); } // Advice declaration before( ) : argTypePatternAnotherClassPointcut( ) && !within(ArgsRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice picked by argTypePatternAnotherClassPointcut"); 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 11-3 shows how the args([Types | Identifiers]) pointcut is applied. Figure 11-3. How the args(Types | Identifiers) pointcut is applied
See AlsoRecipes Recipe 11.1 and Recipe 11.2 describe the this([Type | Identifier]) and target([Type | Identifier]) pointcuts respectively; the get(TypePattern) and set(TypePattern) pointcuts are described in Recipes Recipe 8.1 and Recipe 8.3 respectively; the calling context that is available to advice is covered in Chapter 13; the within(TypePattern) pointcut is described in Recipe 9.1; the NOT(!) operator is described in Recipe 12.4. |