Recipe13.3.Executing Advice Before a Join Point


Recipe 13.3. Executing Advice Before a Join Point

Problem

You want advice to execute before the join points that trigger it.

Solution

Use the before( ) type of advice.

Discussion

Example 13-3 shows how to execute advice before a call to the void MyClass.foo(int,String) method.

Example 13-3. Executing advice before a method call
public aspect BeforeAdviceRecipe  {    /*    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(BeforeAdviceRecipe +)    {       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 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