Recipe6.3.Exposing the Original Join Point When Advice Is Being Advised


Recipe 6.3. Exposing the Original Join Point When Advice Is Being Advised

Problem

Where you are advising a piece of advice using the adviceexecution( ) pointcut, you want to access the original join point that triggered the advice block you are, in turn, advising.

Solution

Add the JoinPoint identifier to your pointcut definition. Bind your JoinPoint identifier as the single parameter to an args([Types | Identifiers]) pointcut declaration. Add the JoinPoint identifier to the corresponding advice you wish to expose the original join point to.

Discussion

In Example 6-4, the original join point that triggered the advice being advised is accessed from the AdviceExecution aspect's before( ) advice using the origi-nalJoinPoint identifier.

Example 6-4. Accessing the original triggering join point when advising advice
import org.aspectj.lang.JoinPoint; public aspect AdviceExecutionRecipe  {    /*            Specifies calling advice whenever advice is executed    */    pointcut adviceExecutionPointcut(JoinPoint originalJoinPoint) :        adviceexecution( ) &&        args(originalJoinPoint) &&        !within(AdviceExecutionRecipe);    // Advice declaration    before(JoinPoint originalJoinPoint) : adviceExecutionPointcut                                               (originalJoinPoint)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println("In the advice picked by AdviceExecutionRecipe");       System.out.println(          "Signature: "             + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: "             + thisJoinPoint.getStaticPart( ).getSourceLocation( ));              System.out.println(             "Advised Advice's Join Point Signature: "                + originalJoinPoint.getSignature( ));              System.out.println(          "------------------------------------------------------------");    } }

In AspectJ, there is an implicit JoinPoint object passed to every advice block as it is invoked. The single JoinPoint parameter is normally passed transparently to the advice and becomes the value of the thisJoinPoint reference. To populate the thisJoinPoint reference, every advice block, as it is implemented by AspectJ, must include the single JoinPoint argument as part of its signature.


See Also

The syntax of the adviceexecution( ) pointcut is shown in Recipe 6.1; the within(TypePattern) pointcut is described in Recipe 9.1; the args([Types | Identifiers]) pointcut declaration is explained in Recipe 11.3; combining pointcut logic using a logical AND (&&) is shown in Recipe 12.2; the unary NOT (!) operator is shown in Recipe 12.4; Chapter 13 describes the different types of advice available in AspectJ.



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