【发布时间】:2020-02-12 14:37:00
【问题描述】:
我想检索调用特定方法的调用方法。
示例:
我考虑的方法:
public void methodA(int a, int b){...}
在测试方法和程序本身中调用
@Test
public void testMethodA(
... some code...
objectClassA.methodA(x,y);
)}
Class B {
...
public void methodB(){
objectClassA.methodA(x,y);
}
}
我想以某种方式获得的是 testMethodA 和 methodB 的内部或至少签名
为此,我认为 AspectJ 可以帮助我,所以我对此进行了研究并最终写了这个 poincut pointcut pcmethodA(): execution(* A.methodA(..) );
我的建议看起来像这样
before(): pcmethodA() {
System.out.println("[AspectJ] Entering " + thisJoinPoint);
System.out.println("[AspectJ] Signature " + thisJoinPoint.getSignature());
System.out.println("[AspectJ] SourceLocation "+ thisJoinPoint.getSourceLocation());
但这会返回
[AspectJ] Entering execution(void com.example.somePackage.A.methodA(int, int)
[AspectJ] Signature com.example.somePackage.A.methodA(int, int)
[AspectJ] SourceLocation A.java:25 /** Line number of the methodA in the file **/
这是我第一次使用 AspectJ ,是否有任何对象或方法来检索我找到的连接点的调用方法? testMethodA 和 methodB
谢谢
【问题讨论】:
-
您没有合适的工具来完成这项工作。这不是 AspectJ 的用途——它不会产生完整的调用堆栈。您必须获取并检查线程堆栈跟踪。
-
确实使用堆栈跟踪可能是更好的解决方案。谢谢
标签: java aop aspectj aspectj-maven-plugin