• 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

is there any way.....?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class A{
call(){
}
}

Is there any way I can find the CLASS NAME of caller to above mentioned call() method in class A. Please note that call() has no parameters.

Thanks,
Prashant
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


gets you a stack trace, from where you figure out the call chain.
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the following threads

Find Calling Class' name within Called Class
How do I get the Method and Class caller?

Note 1: Don't miss to read the comments from Stan James in the second thread.
Note 2: I got the above links by searching the forums. Next time, I suggest you to do the same.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for mentioning my note there. I went back and read it and it's not very eloquent. Must have been late at night. That's my excuse and I'm stickin with it.

Early in my career, when we carved COBOL on stone tablets, I was loaned to a project where every caller passed its own name along with other parameters, and many programs used the caller name in the logic. If called by A do this, if called by B do that. These mysterious tests communicated nothing about the reasons for different behavior and it wound up being a nightmare to keep all the tests up to date as you called old programs in new ways.

This question comes up fairly often and it always makes me lose sleep thinking about those old rat's nest programs. You can find out who called you but if you really think you need to ask the gang here for another solution.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might be talking pants here but if you really have a need for it

Reflection.getCallerClass(3).getName()

Issued in the call() method does the trick (in terms of the class that called it) although I can't find much in the way of documentation on it.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no "Reflection" class, nor a "getCallerClass()" method in the entire Java API. Is this from a third-party library? If so, it's probably using the technique Stan describes.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic