O'Reilly logo

AspectJ Cookbook by Russ Miles

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.2. Accessing the Join Point Context

Problem

You want to access the join point context from within your advice.

Solution

Use the thisJoinPoint and thisJoinPointStaticPart variable.

Discussion

Classes in Java have a this variable to allow their objects to reference and work with themselves. Aspects are converted by the AspectJ compiler into classes; therefore, the aspects have a this reference.

However, an additional join point context can be exposed to advice from the join points that trigger it. AspectJ provides the thisJoinPoint variable to expose this join point context. In addition to thisJoinPoint, the thisJoinPointStaticPart variable is useful if the context that is being accessed can be assessed statically.

Example 13-2 shows some of the information that is available from the generic thisJoinPoint variable.

Example 13-2. Using the thisJoinPoint variable

public aspect ThisJoinPointRecipe 
{
   /*
   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(ThisJoinPointRecipe +)
   {

      System.out.println(
         "------------------- Aspect Advice Logic --------------------");
      System.out.println(
         "Exercising the static parts of AspectJ 1.1.1 
         thisJoinPoint");
      System.out.println(
         "Source Line: "
            + thisJoinPointStaticPart.getSourceLocation( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required