Recipe 12.3. Combining Pointcuts Using a Logical OR (||)ProblemYou want to combine some pointcut declarations, so advice is executed on a join point as long as one of the conditions within the pointcut declarations evaluates to true. SolutionUse the || operator. The syntax of the || operator is: pointcut <pointcut name>(<any values to be picked up>) : <pointcut declaration> || <pointcut declaration> DiscussionExample 12-4 shows the || operator combining callFooIntStringPointcut() and callBarPointcut( ) into a single compound pointcut called callIntersectionFooOrBar(). Example 12-4. Using the || operator to combine two pointcut declarationspublic aspect LogicalOrRecipe { /* Specifies calling advice whenever a method matching the following rules gets called: Class Name: MyClass Method Name: foo Method Return Type: void Method Parameters: int and a String */ pointcut callFooIntStringPointcut( ) : call( void MyClass.foo(int, String)); /* Specifies calling advice whenever a method matching the following rules gets called: Class Name: MyClass Method Name: bar Method Return Type: void Method Parameters: None */ pointcut callBarPointcut( ) : call(void MyClass.bar( )); /* Specifies calling advice whenever a join points is encountered that would be picked by both pointcuts specified: Pointcut name: callFooIntStringPointcut Pointcut name: callBarPointcut Method Return Type: void Method Parameters: None */ pointcut callIntersectionFooOrBar( ) : callFooIntStringPointcut( ) || callBarPointcut( ); // Advice declaration before( ) : callFooIntStringPointcut( ) && !within(LogicalOrRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice picked by callFooIntStringPointcut"); System.out.println( "Signature: " + thisJoinPoint.getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } // Advice declaration before( ) : callBarPointcut( ) && !within(LogicalOrRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println("In the advice picked by callBarPointcut"); System.out.println( "Signature: " + thisJoinPoint.getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } // Advice declaration before( ) : callIntersectionFooOrBar( ) && !within(LogicalOrRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice picked by callIntersectionFooOrBar"); System.out.println( "Signature: " + thisJoinPoint.getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } } In addition to combining pointcuts using a logical AND, as shown in Recipe 12.2, pointcuts and other logical expressions can be combined using a logical OR. Those familiar with the nature of logical operations will know that the result of a logical OR comparison is seen as positive if any of the constituent parts meet the comparison condition. In the language of AspectJ, if a join point initiates the advice on any of the constituent pointcuts combined using a logical OR into a compound pointcut, then the join point will trigger that advice. The || operator in AspectJ also exhibits short-circuiting behavior, similar to the && operator. However, the short-circuiting rule is the opposite of the && operator. With the || operator, the moment a condition is evaluated as positive, the compound statement can be evaluated as positive and completed. See AlsoRecipe 12.2 covers the AND (&&) operator and its short-circuiting evaluations features; the within(TypePattern) pointcut is described in Recipe 9.1; the NOT(!) operator is described in Recipe 12.4; Chapter 13 describes the different types of advice available in AspectJ. |