Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Determining Calling Class

 
Thread Tools Search this Thread
Old 05-19-2004, 01:40 AM   #1
Default Determining Calling Class


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.





WJ
  Reply With Quote
Old 05-19-2004, 02:36 AM   #2
Ryan Stewart
 
Posts: n/a
Default Re: Determining Calling Class
"WJ" <> wrote in message
news:S1yqc.24702$Md.16133@lakeread05...
> 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.




Ryan Stewart
  Reply With Quote
Old 05-19-2004, 02:37 AM   #3
Lothar Kimmeringer
 
Posts: n/a
Default Re: Determining Calling Class
On Tue, 18 May 2004 17:40:20 -0700, WJ wrote:

> 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:
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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


Lothar Kimmeringer
  Reply With Quote
Old 05-19-2004, 02:41 AM   #4
KC Wong
 
Posts: n/a
Default Re: Determining Calling Class
> 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...




KC Wong
  Reply With Quote
Old 05-19-2004, 02:42 AM   #5
WJ
 
Posts: n/a
Default Re: Determining Calling Class
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.





WJ
  Reply With Quote
Old 05-19-2004, 02:44 AM   #6
Lothar Kimmeringer
 
Posts: n/a
Default Re: Determining Calling Class
On Tue, 18 May 2004 20:36:32 -0500, Ryan Stewart wrote:

> "WJ" <> wrote in message
> news:S1yqc.24702$Md.16133@lakeread05...
>> Is there a way for a class (in the method) to determine the calling class
>> name?
>>
>> In A.myMethod(), it would "know" it was being called from Class B.
>>

> 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:
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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


Lothar Kimmeringer
  Reply With Quote
Old 05-19-2004, 03:46 AM   #7
WJ
 
Posts: n/a
Default Re: Determining Calling Class
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




WJ
  Reply With Quote
Old 05-19-2004, 03:49 AM   #8
WJ
 
Posts: n/a
Default Re: Determining Calling Class
>
>
> 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!





WJ
  Reply With Quote
Old 05-19-2004, 08:52 AM   #9
Chris Uppal
 
Posts: n/a
Default Re: Determining Calling Class
WJ wrote:

> 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




Chris Uppal
  Reply With Quote
Old 05-19-2004, 08:34 PM   #10
Liz
 
Posts: n/a
Default Re: Determining Calling Class

"KC Wong" <> wrote in message
news:...
> > 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...
>


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




Liz
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Eclipse how to add external class with different pkg name jan2321 General Help Related Topics 0 11-06-2008 10:04 AM
Custom Class Loader for Web Application using Tomcat tapas.adhikary Software 0 04-22-2008 09:53 AM
70-536, 3 questions blade MCTS 11 03-23-2008 03:47 PM
GZipStream Class and Compression abbamilkii MCTS 0 01-31-2007 07:57 PM
Storing class with session (ASP.Net) bqmassey Software 0 09-22-2006 05:37 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46