• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic