• 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

How do i know which method has called the current method

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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
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
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per William:




Yes, I am bored.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Venkatesan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried it out and glad to say that it works!

 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a million Mani! this shd have struck me!!!

Rock on
Kej
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a bunch Vije!

Go java!
Kej
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic