• Post Reply Bookmark Topic Watch Topic
  • New Topic

is there any way.....?  RSS feed

 
Prashu Negu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


gets you a stack trace, from where you figure out the call chain.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Graham VMead
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!