Aspectj Cookbook

Recipe 6.1. Capturing When Advice Is Executing

Problem

You want to capture when any piece of advice is executing.

Solution

Use the adviceexecution( ) pointcut. The syntax of the adviceexecution( ) pointcut is:

pointcut <pointcut name>( ) : adviceexecution( );

Discussion

The adviceexecution( ) pointcut captures the join points where any advice is executed within an application. Example 6-1 uses the adviceexecution( ) pointcut to apply advice when other advice begins execution.

Example 6-1. Using the adviceexecution( ) pointcut to apply advice

public aspect AdviceExecutionRecipe { /* Specifies calling advice whenever advice is executed */ pointcut adviceExecutionPointcut( ) : adviceexecution( ); // Advice declaration before( ) : adviceExecutionPointcut( ) && !within(AdviceExecutionRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println("In the advice picked by ExecutionRecipe"); System.out.println( "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getStaticPart( ).getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } }

Figure 6-1 shows how the adviceexecution() pointcut is applied to a simple class.

Figure 6-1. How the adviceexecution( ) pointcut is applied

See Also

Recipe 6.3 protects against code in the aspect being called by using the adviceexecution() pointcut combined with the NOT (!) operator; Chapter 9 discusses scope based pointcuts; 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.

Категории