[求助] 如何查看一个方法被其他类中的方法调用

amwps 2008-01-01 01:56:53
有一个表态方法
public static Object newInstance(){
//System.out.println(类名:方法名 调用了我)
return object;
}


我想在return之前打印出哪个类的哪一个方法调用了我,在开发工具中如ECLIPSE或JBUILD都可以查出一个方法被哪些地方调用,不知如何在自己的程序中实现呢???
...全文
322 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
amwps 2008-01-01
  • 打赏
  • 举报
回复
谢谢楼上的兄弟们!!!
lpingxh 2008-01-01
  • 打赏
  • 举报
回复
反射机制应该能解决问题
dracularking 2008-01-01
  • 打赏
  • 举报
回复
利用堆栈返回调用此方法的方法名

public static Object newInstance() {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
return stack[2].getMethodName();
}
Theface 2008-01-01
  • 打赏
  • 举报
回复
发帖前搜索一下...

http://topic.csdn.net/u/20071209/18/5ea43e76-f5ca-4d05-ad32-b1513df94d86.html

老紫竹 2008-01-01
  • 打赏
  • 举报
回复
System.out.println("the caller is " + sun.reflect.Reflection.getCallerClass(0));
System.out.println("the caller is " + sun.reflect.Reflection.getCallerClass(1));
System.out.println("the caller is " + sun.reflect.Reflection.getCallerClass(2));
System.out.println("the caller is " + sun.reflect.Reflection.getCallerClass(3));

这里的参数表示调用层数,0表示此reflection方法本体,1表示本方法所在类,2表示本类的调用者。
amwps 2008-01-01
  • 打赏
  • 举报
回复
自己顶下,求高人助我!!

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧