getting call hierarchy of a method using reflectio

2019-06-04 04:45发布

i used java reflections to get methods from a class(loaded those classes).Now i want to get the call hierarchy of those methods.How can i use call hierarchy option in eclipse IDE for that?any examples or links????

4条回答
唯我独甜
2楼-- · 2019-06-04 04:51

Throw an exception in the method which is at the root of the hierarchy. Then you can catch the exception and printStackTrace().

I'm sure there would be cleaner solution than this. But this would work too.

查看更多
戒情不戒烟
3楼-- · 2019-06-04 04:59

The solutions proposed is to use Thread.currentThread().getStackTrace() in order to obtain the current trace of callers.

However, you want to get this information not for the currently executing method, but for any method on any class. This can't be done easily. I think you have to inspect all classes on the classpath with a bytecode utility and match the calling instructions. Something you will spend too much time on, and I'm pretty sure it isn't needed, unless you are developing an IDE.

Anyway, you can get Eclipse or NetBeans sources and see how it is implemented there.

查看更多
爷的心禁止访问
4楼-- · 2019-06-04 05:06
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
查看更多
迷人小祖宗
5楼-- · 2019-06-04 05:08

You can use the StackTrace object.

But that looks very brittle to me as approach. Better avoid this kind of logic... Why not passing the caller as an argument to thé method?

查看更多
登录 后发表回答