File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i know who called a method?

 
Daniel Chaffee
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Daniel Chaffee
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic