| Author |
How do i know which method has called the current method
|
Kejal Shah
Ranch Hand
Joined: Jun 27, 2003
Posts: 87
|
|
Hi all! How do i know which metthod has called the current method being executed? e.g. Class A{ public void m1(){ m2(); } public void m2(){ //Some code here which will tell me that m2 is called by m1 } } Thanks in advance Kejal
|
Kejal<br />SaneDevil@gmail.com
|
 |
Jeroen Wenting
Ranch Hand
Joined: Oct 12, 2000
Posts: 5093
|
|
You can't know who calls you. You might include an argument to the function call which you mean to be a reference to the calling object but there's no way to make certain that's what you will get.
|
42
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
|
|
Look at the JavaDocs for java.lang.Throwable In Java 1.4 on, we have the getStackTrace method which creates an array of StackTraceElement objects. These provide rather complete information as to how the current Thread got to your method, including class, method and line number. Bill
|
Java Resources at www.wbrogden.com
|
 |
Rovas Kram
Ranch Hand
Joined: Aug 08, 2003
Posts: 135
|
|
As per William: Yes, I am bored.
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
You'll note that it's not real easy or straightforward to find this out. Take this as a lesson that you're not supposed to care or more strongly that the language designers didn't want you to know. If you're doing it just for curiosity the answers above will get you there. If you're thinking about a design that NEEDS TO KNOW you're probably going to a bad place. Let us know why you want this information and maybe we can help find a better solution to the problem.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Jack Kay
Ranch Hand
Joined: Aug 01, 2004
Posts: 62
|
|
This is how I'd do it:: Code: Error Message:: So now we know the error is in Line 6, in Class A! And we can find the method in our code! And all we had to change were the parameters for the method we want to catch. [ Jess added a new line in the code block so it doesn't stretch the page. ] [ August 02, 2004: Message edited by: Jessica Sant ]
|
 |
Kejal Shah
Ranch Hand
Joined: Jun 27, 2003
Posts: 87
|
|
hey guys! thanks a lot! Wud try these out and let u know. Here's precisely wat i want: I've a common method (say m2()) in a class which is called by many other methods from different classes in my application. In the m2() method, i want to know which method has called it. If there's some way of doin it, then gr8 else i'll have to pass an arg to the m2() method which i don't want to. Kejal
|
 |
Vijayendra V Rao
Ranch Hand
Joined: Jul 04, 2004
Posts: 195
|
|
Originally posted by Kejal Shah: In the m2() method, i want to know which method has called it.
What exactly do you mean you want to "know" who is calling this method? You want some sort of a print on the console or some logging to be made or some pop-up...what is it that you exactly mean when you say you want to "know"? Because if you just want some logging or ptinting, its easy to do thats why.
|
Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
|
 |
Mani Venkatesan
Ranch Hand
Joined: Sep 15, 2002
Posts: 64
|
|
I am just wondering here: If I throw and catch an exception in the second (called) method, will the exception stack trace contain the caller method's stack trace element?
|
Mani<br /><a href="http://ideanimal.com" target="_blank" rel="nofollow">blog</a>
|
 |
Mani Venkatesan
Ranch Hand
Joined: Sep 15, 2002
Posts: 64
|
|
Tried it out and glad to say that it works!
|
 |
Kejal Shah
Ranch Hand
Joined: Jun 27, 2003
Posts: 87
|
|
Hey Vije! Tat's exactly wat i want. I want the method name to be logged. Pls lemme know how i can achieve it. Tx in adv Kej
|
 |
Kejal Shah
Ranch Hand
Joined: Jun 27, 2003
Posts: 87
|
|
Thanks a million Mani! this shd have struck me!!! Rock on Kej
|
 |
Vijayendra V Rao
Ranch Hand
Joined: Jul 04, 2004
Posts: 195
|
|
|
Simple, just before you make a call to this method (m2() or whatever), either give a System.out.println() with the ClassName.methodName() syntax or use the LOG() method. There is no need to pass any paremeters since this will change the app design. Just print it out just before you call the method. I don't think its complicated at all.
|
 |
Vijayendra V Rao
Ranch Hand
Joined: Jul 04, 2004
Posts: 195
|
|
What I mean to say is, you can use the following syntax: Just place this line before making a call to m2() and change the class name and method names accordingly from wherever you are calling it. So whenever method m2() gets called, the caller will be logged (printed out to the console).
|
 |
Kejal Shah
Ranch Hand
Joined: Jun 27, 2003
Posts: 87
|
|
Thanks a bunch Vije! Go java! Kej
|
 |
Vijayendra V Rao
Ranch Hand
Joined: Jul 04, 2004
Posts: 195
|
|
Originally posted by Kejal Shah: Thanks a bunch Vije!
I am not sure to what extent my suggestion really helped you but...you are welcome
|
 |
 |
|
|
subject: How do i know which method has called the current method
|
|
|