File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reflection; Method.invoke() and obtaining the stack trace

 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given an instance of java.lang.reflect.Method, I'd like to invoke it (by calling invoke()) and during execution of that method I'd like to obtain the stack trace (instance of StackTraceElement that corresponds to the method call). I can see no other way than to have the method itself obtain that information, which is not an option (I'd rather abolish the requirement). One hacky alternative is to call invoke from a low priority thread and then immediately obtain stack trace information of that thread from the 'main' thread with a higher priority and assume that the scheduler will always execute such that obtaining the stack trace occurs always before the call to method.invoke is complete. Can anyone see any reasonable alternatives?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly you might be able to use JDI or JVMTI to set a breakpoint within the method before calling it. Then when you get the BreakpointEvent, call thread().frames() to get a list of StackFrames. I don't offhand see a way to get a StackTraceElement, but hopefully the StackFrame list has all the info you might need. More importantly, I don't know how easy it is to set a Breakpoint inside an arbitrary Method (if it's possible at all); I've never done it myself. But I'd be interested in hearing whether it can work or not, if you try it. Good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic