Recipe12.1.Capturing When a Runtime Condition Evaluates to True on a Join Point


Recipe 12.1. Capturing When a Runtime Condition Evaluates to True on a Join Point

Problem

You want to trigger advice based on a true result when comparing some runtime values that can be evaluated at a captured join point.

Solution

Use the if(Expression) statement to assess a Boolean expression that contains the runtime variables to be compared. The syntax of the if(Expression) statement is:

pointcut <pointcut name>(<any values to be picked up>) :                          if(<Boolean expression>);

Discussion

The if(Expression) statement has two key characteristics:

  1. The if(Expression) pointcut evaluates variables provided at runtime to come to a true or false result as to whether a join point should trigger the corresponding advice.

  2. The Expression can be made up of various logical elements, including exposed join point context, static variables, and other pointcut declarations.

Example 12-1 shows the if(Expression) statement in use. The after advice will only be executed if the runtime values of the variables that make up the expression result in TRue.

Example 12-1. The if(Expression) statement being used to assess target application variables
public aspect IfRecipe  {    // Define some variables for comparison    private static final long realisticSalary = 30000l;    /*     * Specifies calling advice if this is referencing an object of      * class MyClass and the object has a realistic salary:     */    pointcut ifJoinPointThisHasRealisticSalaryPointcut( ) : if (       thisJoinPoint.getThis( ) instanceof MyClass          && ((MyClass) thisJoinPoint.getThis( )).getSalary( )             < realisticSalary          && ((MyClass) thisJoinPoint.getThis( )).getSalary( ) > 0)       && !withincode(* MyClass.get*( ));    // Advice declaration    //This advice will be executed before the pointcut that picks it    after( ) : ifJoinPointThisHasRealisticSalaryPointcut( )       && !within(IfRecipe +)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println(          "In the advice picked by ifJoinPointThisHasRealisticSalaryPointcut");       System.out.println(          "Join Point Kind: " + thisJoinPoint.getKind( ));       System.out.println(          "Executing object: " + thisJoinPoint.getThis( ));       System.out.println(          "MyClass instance: "             + ((MyClass) thisJoinPoint.getThis( )).getName( )             + " : "             + ((MyClass) thisJoinPoint.getThis( )).getSalary( ));       System.out.println(          "Signature: " + thisJoinPoint.getSignature( ));       System.out.println(          "Source Line: " + thisJoinPoint.getSourceLocation( ));       System.out.println(          "------------------------------------------------------------");    } }

The if(Expression) statement allows you to define conditional logic as to whether a piece of advice should be applied to a particular join point. This conditional logic is executed at runtime and must work on valid types at the point when the join points are reached.

The conditional logic contained in the ifJoinPointThisHasRealisticSalaryPointcut( ) pointcut in Example 12-1 specifies that the corresponding after advice should be triggered when the following occur:

  • The executing object is of type MyClass.

  • The salary attribute of the object is less than the realisticSalary constant.

  • The salary attribute of the object is greater than 0.

  • The current join point is not within the getSalary() method. By using the wildcard, the join point must not be within any method in the MyClass class that begins with get.

Each of the conditions is logically combined using the AND (&&) operator. The first three conditions are fairly easily understood as part of the pointcut's Boolean logic, but the final one is a little more interesting. The !withincode(* MyClass.get*( )) condition has to be included to prevent the call to getSalary( ) from within the advice in turn triggering a recursive call to the advice and resulting in an infinite loop.

See Also

The AND (&&) operator and the OR (||) operator are described in Recipes 12.2 and 12.3, respectively; the NOT(!) operator is described in Recipe 12.4; Chapter 13 describes the different forms of advice that AspectJ supports.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net