1

How to solve a “cannot find symbol variable thisJoinPoint” while trying to build an AspectJ project in AndroidStudio using annotation style?

Platform details: AspectJ 1.8.1, AndroidStudio 2.1.3

Code Example:

import org.aspectj.lang.JoinPoint.*;         // Not used!
import org.aspectj.lang.ProceedingJoinPoint; // Not used!
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyAspect {
    @Pointcut( "execution(* *(..))" )
    public void methodExecution() {}

    @After( "methodExecution()" )
    public void accessThisJoinPointData() {
        if ( thisJointPoint != null ) { // HERE the variable isn’t recognized!
          // Do something with thisJoinPoint...
       }
    }
 }

1 Answers1

1

After some tests and research I just found that for annotation style, we need to declare thisJoinPoint as a parameter for the advice. So the problem is solved as follows:

@After( "methodExecution()" )
    public void accessThisJoinPointData( JoinPoint thisJoinPoint ) {
        if ( thisJointPoint != null ) { // Now the variable can be recognized!
          // Do something with thisJoinPoint...
        }
}

Reference:

"If the advice body needs access to thisJoinPoint, thisJoinPointStaticPart, thisEnclosingJoinPointStaticPart then these need to be declared as additional method parameters when using the annotation style."

https://eclipse.org/aspectj/doc/released/adk15notebook/ataspectj-pcadvice.html