Recipe13.9.Advising Aspects


Recipe 13.9. Advising Aspects

Problem

You want to apply advice to other aspects.

Solution

Use the pointcuts available within AspectJ to specify the aspects to be advised.

Discussion

AspectJ includes aspects within the scope of advisable things, so the various pointcuts available in AspectJ can be used to specify advising other aspects in addition to the classes within the application. Example 13-10 shows how to declare advice to be applied to advice within another aspect.

Example 13-10. Applying advice to other advice
public aspect AdviseAspectRecipe  {    /*    Specifies calling advice whenever a method    matching the following rules gets called:        Class Name: MyClass    Method Name: foo    Method Return Type: void    Method Parameters: an int followed by a String    */    pointcut callPointCut( ) : call(void MyClass.foo(int, String));    // Advice declaration    before( ) : callPointCut( ) && within(AdvisedAspect +)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println(          "In the advice attached to the call point cut");       System.out.println(          "Signature: "             + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: "             + thisJoinPoint.getStaticPart( ).getSourceLocation( ));       System.out.println(          "------------------------------------------------------------");    } }

See Also

The call(Signature) pointcut is described in Recipe 4.1; the within(TypePattern) pointcut is described in Recipe 9.1; the thisJoinPoint variable and the context it encapsulates is examined in Recipe 13.2.



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