aspose file tools*
The moose likes Beginning Java and the fly likes How do i know who called a method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do i know who called a method?" Watch "How do i know who called a method?" New topic
Author

How do i know who called a method?

Daniel Chaffee
Greenhorn

Joined: Sep 26, 2001
Posts: 4
In one of my methods i want to see what class and method called a public method. E.g
public class A{
public void methodA(){
B someobj = new B();
someobj.methodB();
}
public class B{
public void methodB(){
//in here i want to know that class A methodA called this
//method. This method may be called from anywere so i dont
//want to pass in data in a method or anything like that.
//I almost want something like printStackTrace()
//but i dont want to deal with exceptions.
}
any ideas?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Why can't you have
public void methodB(Class whoCalledThis){
and just make the users TELL you what class that they are - of course they could lie I suppose but still.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Daniel Chaffee
Greenhorn

Joined: Sep 26, 2001
Posts: 4
I dont want to change my method signatures...that is a requirment that i have...i cant change the method signatures at all. So that means that methodB() must remain as methodB() and the code in that method must somehow determine who called it.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well, since this is an instance method, the class that is using it must have created an instance of B in order to call the method. The holding class just keeps a variable that holds a reference to the instance of B (someobj).
I don't believe that there is any way that an object can track what variables are holding a reference to it. After you created someobj in methodA you could have shared that reference with a zillion other variables and passed it to classes other than class A. Any of those variables holding the reference to the one instance of class B could invoke methodB.
I guess that I am not going to be any help.
Daniel Chaffee
Greenhorn

Joined: Sep 26, 2001
Posts: 4
I appreciate the time and effort though....seriously. Thanks anyway! If i do find an answer though i will post it on this thread for you.
timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
I may be showing my lack of experience here but if I am maybe somebody else will correct me and I will learn. That being said, could you create a new Exception and then instead of throwing it simply call printStackTrace()?
[This message has been edited by timothy zimmerman (edited October 01, 2001).]
 
Don't get me started about those stupid light bulbs.
 
subject: How do i know who called a method?