我的建议看起来像这样 before():pcmethodA(){System.out.println("[AspectJ]输入" + thisJoinPoint);System.out.println("[AspectJ]签名" + thisJoinPoint.getSignature());System.out.println("[AspectJ] SourceLocation" + thisJoinPoint.getSourceLocation()); 但这返回 [AspectJ]输入执行(无效com.example.somePackage.A.methodA(int,int)[AspectJ]签名com.example.somePackage.A.methodA(int,int)[AspectJ] SourceLocation A.java:25/**方法A在文件中的行号**/ 这是我第一次使用AspectJ,是否有任何对象或方法可以检索我找到的连接点的调用方法? testMethodA 和方法B 谢谢解决方案让我首先使用一些示例类和驱动程序应用程序重新创建您的情况: 程序包de.scrum_master.app;公共课Foo {public void methodA(int a,int b){System.out.println("methodA:" + a +," + b);}} 程序包de.scrum_master.app;公共类DummyTest {公共无效testSomething(){新的Foo().methodA(33,44);}} 程序包de.scrum_master.app;公共类申请{公共无效doSomething(){新的Foo().methodA(11,22);}公共静态void main(String [] args){新的Application().doSomething();新的DummyTest().testSomething();}} 现在尝试在您的方面将 call()与 thisEnclosingJoinPointStaticPart 结合使用: package de.scrum_master.aspect;导入de.scrum_master.app.Foo;公开方面MyAspect {切入点pcmethodA():call(* Foo.methodA(..));before():pcmethodA(){System.out.println("[AspectJ]执行中:" + thisJoinPoint);System.out.println("[AspectJ]调用者:" + thisEnclosingJoinPointStaticPart);}} 运行 Application 时的控制台日志: [AspectJ]执行:调用(void de.scrum_master.app.Foo.methodA(int,int))[AspectJ]调用者:execution(void de.scrum_master.app.Application.doSomething())方法A:11、22[AspectJ]执行:呼叫(void de.scrum_master.app.Foo.methodA(int,int))[AspectJ]调用者:execution(void de.scrum_master.app.DummyTest.testSomething())方法A:33、44 看到了吗?如果您只想确定调用方和被调用方,则无需处理堆栈跟踪.I want to retrieve the calling method where a specific method is called. Example : The method I consider :public void methodA(int a, int b){...}is called in a test method and also in the program itself@Testpublic void testMethodA(... some code...objectClassA.methodA(x,y);)}Class B {... public void methodB(){ objectClassA.methodA(x,y); }}What I want to obtain somehow is the inside or at least the signature of testMethodA and methodB To do that I thought AspectJ could help me, so I looked into that and ended up writing this poincut pointcut pcmethodA(): execution(* A.methodA(..) );and my advice looked something like thisbefore(): pcmethodA() { System.out.println("[AspectJ] Entering " + thisJoinPoint); System.out.println("[AspectJ] Signature " + thisJoinPoint.getSignature()); System.out.println("[AspectJ] SourceLocation "+ thisJoinPoint.getSourceLocation());But this returns [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 **/It is my first time using AspectJ , is there any object or way to retreive the calling method of my found joinpoints? testMethodA and methodBThanks 解决方案 Let me recreate your situation first with a few sample classes + driver application:package de.scrum_master.app;public class Foo { public void methodA(int a, int b) { System.out.println("methodA: " + a + ", " + b); }}package de.scrum_master.app;public class DummyTest { public void testSomething() { new Foo().methodA(33, 44); }}package de.scrum_master.app;public class Application { public void doSomething() { new Foo().methodA(11, 22); } public static void main(String[] args) { new Application().doSomething(); new DummyTest().testSomething(); }}Now try call() in combination with thisEnclosingJoinPointStaticPart in your aspect:package de.scrum_master.aspect;import de.scrum_master.app.Foo;public aspect MyAspect { pointcut pcmethodA() : call(* Foo.methodA(..)); before() : pcmethodA() { System.out.println("[AspectJ] Executing: " + thisJoinPoint); System.out.println("[AspectJ] Called by: " + thisEnclosingJoinPointStaticPart); }}The console log when running Application:[AspectJ] Executing: call(void de.scrum_master.app.Foo.methodA(int, int))[AspectJ] Called by: execution(void de.scrum_master.app.Application.doSomething())methodA: 11, 22[AspectJ] Executing: call(void de.scrum_master.app.Foo.methodA(int, int))[AspectJ] Called by: execution(void de.scrum_master.app.DummyTest.testSomething())methodA: 33, 44See? No need to juggle stack traces if you just want to determine caller and callee. 这篇关于AspectJ:查找找到的JoinPoint的源方法代码/名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-16 23:53