• Post Reply Bookmark Topic Watch Topic
  • New Topic

need the caller name  RSS feed

 
Tirthankar Dutta Chaudhuri
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
is there any method by which i can know a particular fuction has been called from which class.
For eg

In method called i want to know from where is method called .
Is there any way to do that .
Thanks,
Tirthankar
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try getStackTrace() method in java.lang.Thread class (JDK 5) or getStackTrace() method in java.lang.Throwable ( JDK 1.4+). It might work.

This should return a StackTraceElement object which will have the required information.

By the way, just curious: Why do you need such info?
 
Tirthankar Dutta Chaudhuri
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
thanks for the prompt reply . well i need it for logging .
yes we get it from a stacktrace .
Thanks,
Tirthankar
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16045
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you writing your own logging implementation?

You don't need to do that. Use Log4J - it contains everything you need, and it can log the name of the class and method and even the line number in the source code where you're executing the logging statement.
 
Babji Reddy
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that generating a stack trace to know the caller name, slows down your execution process.
So you should disable if you don't want to log the caller in production like environment.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it can slow things down considerably if you do a lot of logging. But it depends on the code. You may well find that the performance overhead of logging the method names is unnoticeable in your application. The important thing is to remember that you can turn it off, or limit it to certain classes, by configuring the log4j.xml file.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Generating a stack trace" never occurs - a thread always knows its current call stack so 'generating' it has no meaning. Generating some structure that resembles the current call stack (such as a StackTraceElement[]) is asymptotic in cost.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you are using 1.4 or greater in which case call stack information is cheap and available. There are several uses for this but of course you should avoid injecting dependencies on a particular caller in any method. Here's one way you can call a method to find out who's calling.



Note that you don't care who called "whence() but rather who called the method containing whence, hence the index is one not zero. Example:

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!