Recipe13.8.Controlling Advice Precedence


Recipe 13.8. Controlling Advice Precedence

Problem

You want to control the precedence of multiple advice blocks as they are applied to the same join point.

Solution

If the same types of advice located in different aspects are applied to the same join point, you can use the declare precedence statement. The syntax of the declare precedence statement is:

declare precedence : TypePattern, TypePattern, ..;

If the same types of advice are located in the same aspect, then the location of the advice declaration is used to denote its precedence as it is applied to the shared join point.

Discussion

Example 13-9 shows using the declare precedence statement to specify that AspectA is of a higher precedence than AspectB.

Example 13-9. Using the declare precedence statement to control aspect precedence
public aspect AspectA  {    // Declare precedence rules    declare precedence : AspectA, AspectB;    /*    Specifies calling advice whenever a method    matching the following rules gets called:        Class Name: MyClass    Method Name: foo    Method Return Type: * (any return type)    Method Parameters: an int followed by a String    */    pointcut callPointCut( ) : call(void MyClass.foo(int, String));    // Advice declaration    before( ) : callPointCut( ) && !within(AspectA +)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println("In the advice of AspectA");       System.out.println("Target: " + thisJoinPoint.getTarget( ));       System.out.println("This: " + thisJoinPoint.getThis( ));       System.out.println("Aspect Instance: " + AspectA.aspectOf( ));       System.out.println(          "------------------------------------------------------------");    } }

Aspects can work in relative isolation in some applications, but as your aspect-oriented architecture becomes more complex, having two aspects advising the same join point is reasonable. In these cases, you must control the precedence of the aspects when applying their advice to any shared join points.

TypePatterns are used in the declare precedence statements to specify the different aspects and their explicit orderings. The TypePatterns can be specified using wildcards to indicate the precedence for sets of particular aspects or to entire packages of aspects if required.

The same problem occurs when two blocks of the same type of advice in the same aspect are applied to the same join point. In this case, it does not make sense to use the declare precedence statement because the advice is all in the same aspect. To handle this, AspectJ applies an implicit order of precedence based upon the type and location of the advice within the aspect declaration.

In the case where the same types of advice within the same aspect are advising the same join point the implicit rules of precedence are:

  • The implicit precedence rules for the before( ) and around( ) types of advice are applied in the order they are declared in the aspect. If two blocks of before( ) advice in the same aspect are applied to the same join point, then the first block that is declared will have the highest precedence and the last will have the lowest.

  • The implicit precedence for the after( ), after( ) returning, and around( ) tHRowing types of advice are applied in the reverse order to the before and around( ) types of advice. This means that if two or more blocks of the after( ) types of advice in the same aspect are applied to the same join point, then the block that is declared last will have the highest precedence and the first will have the lowest.

Whether advice is declared in the same or separate aspects, advice precedence means different things depending on the advice type. Figure 13-2 shows the different precedence orderings in relation to a join point and their implications for the different forms of advice.

Figure 13-2. Advice order of precedence in relation to a join point


See Also

The call(Signature) pointcut is covered in Recipe Recipe 4.1; the within(TypePattern) pointcut is described in Recipe 9.1; the NOT(!) operator is described in Recipe 12.4; the before( ) type of advice is described in Recipe 13.3; the around( ) type of advice is covered in Recipe Recipe 13.4; the after( ) type of advice and its variations are described in Recipes Recipe 13.5, Recipe 13.6, and Recipe 13.7, respectively.



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