比如Class A有个methodA(), 我如何知道这个methodA()被调用的关系,比如是被Class B的methodB()中的 Class C 的methodC()调用的。目前我是用这个方法,直接在methodA()里面throw Exception,就可以知道这个运行时的调用关系了,但是有麽可有其他的方法可以知道???
其实你已经摸到边了,确实是异常处理那部分中有,不过不用你那么麻烦
[code="java"]public class Foo {
public void m1() {
m2();
}
public void m2() {
Throwable ex = new Throwable();
StackTraceElement[] stackElements = ex.getStackTrace();
if (stackElements != null) {
for (StackTraceElement ste : stackElements) {
System.out.println("[File:" + ste.getFileName() + "]"
+ ste.getClassName() + "." + ste.getMethodName()
+ "():" + ste.getLineNumber());
}
}
}
public static void main(String[] args) {
new Foo().m1();
}
}[/code]
[quote]methodA()被调用的关系,比如是被Class B的methodB()中的 Class C 的methodC()调用[/quote]
这么悍!没试过!貌似不行! 期待....