开发者

Getting stack of invoked methods in java

i need to know wh开发者_C百科ich methods were invoked one by one in code. Simple step-by-step debugging doesn't help (need too much time). How can i do this? It would be really great do this without changes of code and saving result in file.


Since it seems that maybe you don't want to check the stack tree, just the order, you could check out BTrace or Adding logging with Java agent.


Before your program finishes, you could Thread.currentThread().getStackTrace(); and then print each of those elements. A stack trace shows you the order of execution in your thread.


Sounds like a cross-cutting concern, especially when you talk about not changing existing code.

AspectJ is available to do this work for you using pointcuts.

http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html


Do you mean you want to log every method call that occurs in your program? If so, have a look at AspectJ - there's an example here which logs particular methods, but you can easily adapt it to cover all methods.


What about when you catch a Exception you can try to use printStackTrace() function to dump out what has been invoked.

try{
     //Your code
}
catch(Exception e){
    e.printStackTrace();
} 


Run your code in debug mode, you'll be able to set breakpoints to pause the execution, proceed line by line, inspect variables, etc.. Basically all the IDEs have debug mode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜