How to find call hierarchy of a method

R

Riyaz

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 ?
 
H

Hunter Gratzner

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.
 
P

Patricia Shanahan

Riyaz 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

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
 
T

tzvika.barenholz

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 ?

why not pass the owner as a parameter in the D constructor?
 
M

Mike Schilling

Riyaz said:
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.
 
M

manivannan.palanichamy

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.

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top