Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

How to find call hierarchy of a method

Thread view: 
Riyaz - 07 Aug 2007 06:26 GMT
public class D {

    public void myOwner(){
        //i need to find the class who's calling me
    }

    public static void main(String[] args) {
        new A();
        new B();
        new C();
    }
}

class A {
    public A() {
        D d = new D();
        d.myOwner();
    }
}

class B {
    public B() {
        D d = new D();
        d.myOwner();
    }
}

class C {
    public C() {
        D d = new D();
        d.myOwner();
    }
}

In the above, in class D's myOnwer() i need an implementation which
tells who's calling the myOwner() i.e from class A or B or C

I also need the implementation to be compatible with Java 1.3

Any suggestions ?
Hunter Gratzner - 07 Aug 2007 06:47 GMT
> public class D {
>
>         public void myOwner(){
>                 //i need to find the class who's calling me

If you need this you have structured your application  the wrong way.
Patricia Shanahan - 07 Aug 2007 07:11 GMT
...
> In the above, in class D's myOnwer() i need an implementation which
> tells who's calling the myOwner() i.e from class A or B or C
>
> I also need the implementation to be compatible with Java 1.3
>
> Any suggestions ?

There are ways that might work, though the 1.3 requirement makes it
harder. However, it is liable to be very messy, and definitely not
recommended.

Could you describe the design problem you are trying to solve? There is
almost certainly an easier way.

Patricia
tzvika.barenholz@gmail.com - 07 Aug 2007 08:21 GMT
> public class D {
>
[quoted text clipped - 40 lines]
>
> Any suggestions ?

why not pass the owner as a parameter in the D constructor?
Mike Schilling - 07 Aug 2007 09:15 GMT
> In the above, in class D's myOnwer() i need an implementation which
> tells who's calling the myOwner() i.e from class A or B or C
>
> I also need the implementation to be compatible with Java 1.3

   StringOutputWriter sw = new StringOutputWriter();
   (new Exception()).printStackTrace(sw);
   // parse the contents of sw to get the call stack

But I agree with the previous commenters; there's probably a better way to
do whatever it is you're trying to do.
manivannan.palanichamy@gmail.com - 07 Aug 2007 19:06 GMT
On Aug 7, 1:15 pm, "Mike Schilling" <mscottschill...@hotmail.com>
wrote:
> > In the above, in class D's myOnwer() i need an implementation which
> > tells who's calling the myOwner() i.e from class A or B or C
[quoted text clipped - 7 lines]
> But I agree with the previous commenters; there's probably a better way to
> do whatever it is you're trying to do.

You can declare a contructor in D, which takes a caller name as a
parameter.
Like,
public D(String name)
{
this.name = name;
}
Then, you can print this name.You should pass this name arg from A/B/C
while initializing the constructor. If you want to avoid hardcoding of
the classname, then do it like,
String thisClassName = this.getClass().getName();
then pass it to D's constructor like,
D d = new D (thisClassName);

--
Manivannan.Palanichamy (@) Oracle.com
http://mani.gw.googlepages.com/index.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.