我正在使用切入点记录我方面的所有异常。基本上我这样做是为了记录捕获异常的方法的名称。要获取我一直在使用thisEnclosingJoinPointStaticPart的方法的名称,它曾经工作得很好,直到我迁移到Java 8和AspectJ 1.8.0。
现在我的旧方面将不再编译,因为我收到以下错误:“本地变量thisEnclosingJoinPointStaticPart可能尚未初始化”。
这是我的方面看起来的样子。
pointcut exceptionLogging(Exception e) : handler(Exception+) && args(e);
before(Exception e) : exceptionLogging(e) {
String method = thisEnclosingJoinPointStaticPart.getSignature().getName();
}
答案 0 :(得分:1)
对我来说也失败了。我认为你正在使用https://bugs.eclipse.org/bugs/show_bug.cgi?id=431976的变体。该错误涵盖thisJoinPoint
案例,但我会添加关于thisEnclosingJoinPointStaticPart
的注释。这是由于Eclipse Java8编译器中的新变量使用流分析,AspectJ现在基于该分析。
答案 1 :(得分:0)
对我而言,它很有效,例如。
public class Application {
public static void main(String[] args) {
try {
throw new Exception("my exception");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public aspect MyAspect {
pointcut exceptionLogging(Exception e) : handler(Exception+) && args(e);
before(Exception e) : exceptionLogging(e) {
System.out.println(thisJoinPointStaticPart + " -> " + e);
System.out.println(thisEnclosingJoinPointStaticPart);
System.out.println(thisEnclosingJoinPointStaticPart.getSignature().getName());
}
}
<强>输出:强>
handler(catch(Exception)) -> java.lang.Exception: my exception
execution(void Application.main(String[]))
main
java.lang.Exception: my exception
at Application.main(Application.java:4)