我的建议看起来像这样before(): pcmethodA() {System.out.println("[AspectJ] 进入" + thisJoinPoint);System.out.println("[AspectJ] 签名" + thisJoinPoint.getSignature());System.out.println("[AspectJ] SourceLocation "+ thisJoinPoint.getSourceLocation());但这会返回[AspectJ] 进入执行(void com.example.somePackage.A.methodA(int, int)[AspectJ] 签名 com.example.somePackage.A.methodA(int, int)[AspectJ] SourceLocation A.java:25/** 文件中methodA的行号**/这是我第一次使用 AspectJ ,有什么对象或方法可以检索我找到的连接点的调用方法吗?testMethodA 和 methodB谢谢 解决方案 让我先用几个示例类 + 驱动程序应用重新创建您的情况:package de.scrum_master.app;公共类 Foo {public void methodA(int a, int b) {System.out.println("methodA:" + a + ", " + b);}}package de.scrum_master.app;公共类 DummyTest {公共无效 testSomething() {新 Foo().methodA(33, 44);}}package de.scrum_master.app;公共类应用{公共无效doSomething(){新 Foo().methodA(11, 22);}公共静态无效主(字符串 [] args){新应用程序().doSomething();new DummyTest().testSomething();}}现在在您的方面尝试将 call() 与 thisEnclosureJoinPointStaticPart 结合使用:package de.scrum_master.aspect;导入 de.scrum_master.app.Foo;公共方面 MyAspect {切入点 pcmethodA() : call(* Foo.methodA(..));之前():pcmethodA(){System.out.println("[AspectJ] 正在执行:" + thisJoinPoint);System.out.println("[AspectJ] 调用者:" + thisEnclosureJoinPointStaticPart);}}运行Application时的控制台日志:[AspectJ] 执行:call(void de.scrum_master.app.Foo.methodA(int, int))[AspectJ] 调用者: execution(void de.scrum_master.app.Application.doSomething())方法A:11、22[AspectJ] 执行:call(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