• 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

From where is a method called?

 
Ranch Hand
Posts: 249
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been looking through documentation trying to find this with no luck. Is there a way to find out what class/object/method called a particular method? I have a jsp application (although a jsp app, this I think is a java question) where my sessionlistener's attributeRemoved() method is called. I'd like to know specifically what triggered the call.
Thanks in advance.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your best chance at success will probably lie with a SecurityManager.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get the current thread and dump the stack trace to see where you are:

Of course the "answer" is displayed in standard out. But you could re-assign "System.out" to some file, then do the ".dumpStack()", close the file and then read the results and parse them to figure out where you came from.
Not easy, but doable if you are desparate. I haven't looked at the SecurityManager so I can't speak to that solution.
 
Mike Firkser
Ranch Hand
Posts: 249
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't find anything in SecurityManager I could figure out, and the second solution didn't really do what I needed. However, I found a work around for what I was doing.
Thanks for the help.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create (but not throw) an Exception, then use the Throwable.getStackTrace() method to get an array of StackTraceElement references. Each of these has the data for one level of the calling stack and you can programmatically get at all sorts of information. (SDK 1.4 and later)
Bill
[ March 31, 2004: Message edited by: William Brogden ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a caution - if your design requires you to know how you were called in order to function, I'd stop and take a long look at the design. Depenencies are likely running the wrong way. If you're only curious or want to log some useful stats I'm not so worried.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic