Recipe 6.3. Exposing the Original Join Point When Advice Is Being AdvisedProblemWhere 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. SolutionAdd 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. DiscussionIn 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 adviceimport 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( "------------------------------------------------------------"); } }
See AlsoThe 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. |