Determining Calling Class

W

WJ

Is there a way for a class (in the method) to determine the calling class
name?

i.e. Class A has myMethod();

Class B has
A a=new A();
a.myMethod();

In A.myMethod(), it would "know" it was being called from Class B.
 
R

Ryan Stewart

WJ said:
Is there a way for a class (in the method) to determine the calling class
name?

i.e. Class A has myMethod();

Class B has
A a=new A();
a.myMethod();

In A.myMethod(), it would "know" it was being called from Class B.
No way that I'm aware of. Doing that goes against pretty much every OO
principle. If your design requires it, you very likely have a faulty design.
 
L

Lothar Kimmeringer

Is there a way for a class (in the method) to determine the calling class
name?

Since Java 1.4 you can create an Exception and call getStackTrace()
returning an array of StackTraceElement. The rest you should be
able to read in the API.

BTW: Exception-creation is quite time-consuming, especially the
creation of the stacktrace.

Before Java 1.4 there was a way within the SecurityManager to
get the calling-stack, but I would need to get old java-sources
out of my non-online-archive to see how exactly that worked. So
a view into the corresponding API should help, too.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
K

KC Wong

Is there a way for a class (in the method) to determine the calling class
name?

i.e. Class A has myMethod();

Class B has
A a=new A();
a.myMethod();

In A.myMethod(), it would "know" it was being called from Class B.

I think you can parse the stack trace... throw a new exception, catch it,
and call its getStackTrace() method and parse the result.

There might be some better methods that I can't think of...


P.S. Your name reminds me of a member of CivFanatics who is called WillJ...
 
W

WJ

It isn't really a desing issue. I wanted to be able to log various stack
traces. There are a couple of other ways I can go about it, but this would
have been slick. ;-)
 
L

Lothar Kimmeringer

No way that I'm aware of.

See my other post ;-)
Doing that goes against pretty much every OO
principle. If your design requires it, you very likely have a faulty design.

I developed a JSP-like server a couple of years ago (before
there was JSP), where I had to disable the developers of
Java-enabled pages to do something like
<% System.exit(0); %>
(in JSP-syntax, my own HTML-to-Java-Switch looked differently)
leading to the shutdown of the server. The server itself should
still be able to gracefully shut down if needed.

For this you need to determine if there was a "JSP"-page
involved anywhere in the line of calling classes to find
out if a (AFAIR) doExit in Securemanager will lead to an exception
(preventing shutdown) or not.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
W

WJ

That's a slick idea. Except for the object creation. I was going to use
this to log activity to see where the calls are coming from. But creating
an Exception each time is way too expensive, I think.

I may have to bounce to "Plan B" :)

~Cheers!

-William
 
W

WJ

P.S. Your name reminds me of a member of CivFanatics who is called
WillJ...

Never played CivFanatics, but the name is the same (William part, that is).

I played Civ II back in the day. . .talk about wasting a day! :-D
 
C

Chris Uppal

WJ said:
That's a slick idea. Except for the object creation. I was going to use
this to log activity to see where the calls are coming from. But creating
an Exception each time is way too expensive, I think.

Depending on what you are doing, you might be better off using a profiling
tool.

Or -- a bit odd perhaps, but I think it'd work -- only log the caller every
10th time, or something like that...

-- chris
 
L

Liz

KC Wong said:
I think you can parse the stack trace... throw a new exception, catch it,
and call its getStackTrace() method and parse the result.

There might be some better methods that I can't think of...


P.S. Your name reminds me of a member of CivFanatics who is called WillJ...

Does stacktrace give method names if you don't compile with -g?
 
C

Chris Smith

Liz said:
Does stacktrace give method names if you don't compile with -g?

Yes... but it may not give line numbers.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top