Recipe13.5.Executing Advice Unconditionally After a Join Point


Recipe 13.5. Executing Advice Unconditionally After a Join Point

Problem

You want advice to execute after a specific join point regardless of how the join point returned.

Solution

Use the after( ) type of advice.

Discussion

Example 13-5 shows how to execute advice after the void MyClass.foo(int,String) method regardless of how the method returns.

Example 13-5. Executing advice after a method call
public aspect AfterAdviceRecipe  {    /*    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    after( ) : callPointCut( ) && !within(AfterAdviceRecipe +)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println(          "Source Location: "             + 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; discusses some common pointcut idioms, one of which being the ability to ignore exception handling join points as these do not support after( ) advice.



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