![]() |
|
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
"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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
> 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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
>
> > 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 |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
"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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |