在尝试使用注释样式在AndroidStudio中构建AspectJ项目时如何解决“找不到符号变量thisJoinPoint”?
平台详情:AspectJ 1.8.1,AndroidStudio 2.1.3
代码示例:
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...
}
}
}
答案 0 :(得分:1)
经过一些测试和研究后,我发现对于注释样式,我们需要声明 thisJoinPoint 作为建议的参数。所以问题解决如下:
@After( "methodExecution()" )
public void accessThisJoinPointData( JoinPoint thisJoinPoint ) {
if ( thisJointPoint != null ) { // Now the variable can be recognized!
// Do something with thisJoinPoint...
}
}
参考:
“如果建议主体需要访问thisJoinPoint,thisJoinPointStaticPart,thisEnclosingJoinPointStaticPart,那么在使用注释样式时,这些需要被声明为附加方法参数。”
https://eclipse.org/aspectj/doc/released/adk15notebook/ataspectj-pcadvice.html