This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Find Calling Class' name within Called Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Find Calling Class Watch "Find Calling Class New topic
Author

Find Calling Class' name within Called Class

Gopal Krishna
Greenhorn

Joined: Aug 31, 2002
Posts: 18
    
    1
Is there a way in java to identify the Calling class' name from within the called class.

For example.

If there are two classes A and B. If a method in A calls a method in B.

Is there a way for the method in B to know which class/method called it ?

Thanks,

Srini
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Thread.getStackTrace() returns the entire stack:


public StackTraceElement[] getStackTrace()

Returns an array of stack trace elements representing the stack dump of this thread. This method will return a zero-length array if this thread has not started or has terminated. If the returned array is of non-zero length then the first element of the array represents the top of the stack, which is the most recent method invocation in the sequence. The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.

and StackTraceElement has a getClassName() method.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Gopal Krishna
Greenhorn

Joined: Aug 31, 2002
Posts: 18
    
    1
Thanks a lot Joe.

-- Srini
Gopal Krishna
Greenhorn

Joined: Aug 31, 2002
Posts: 18
    
    1
Joe,

The solution that you have suggested would not work, since Thread does not have getStackTrace() method .

Thanks,

Srini
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Go Tiger! (Method added to latest java version)

public StackTraceElement[] getStackTrace()

Since:
1.5
Gopal Krishna
Greenhorn

Joined: Aug 31, 2002
Posts: 18
    
    1
Thanks Joe and Carol,

Now i undestand that this solution works but only since 1.5.

But is there a way we can achieve this in prior versions ?

-- Srini
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
getStackTrace() was added to Thread in 1.5, but it was added to Throwable in 1.4. So for 1.4, you can call

If you're using an even older version, the best you can do is something like this:


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
There is no guarantee that the stacktrace is available at all, though.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm curious why you want to do this. It seems to come up fairly often. If it's for profiling or out of curiosity, it might be ok. If you use the knowledge about the calling class for any logic you're wandering into a scary kind of coupling.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I've used it to implement job security. In some of my more complex classes -- ones I'm sure no one else can understand because they have methods with hundreds of lines and cryptic variables like x and cj -- I will occassionally look up the calling class's name. If it's a class written by a coworker that I don't particularly like, I'll pick a random number and return a incorrect result 1 out of 10 times.

This is just often enough to cause them heartache but not so frequent that they realize what's really going on. Most times they quit out of frustration, but even better if they're "let go" for incompetency.

Wait a minute, none of you know my coworkers, do you?

Just to be safe . . . For the humor impared, this is a joke.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Thanks, David. Gave me some good ideas. Now I need a runtime API into the source control system so I can see who last modified the calling class. If it was me, give good results.

I had brief contact with a mainframe COBOL CICS project across the hall many years ago that used the name of the calling program in logic all over the place. I dropped the source listing and ran.
Gopal Krishna
Greenhorn

Joined: Aug 31, 2002
Posts: 18
    
    1
Stan,

This is where i plan to use this. In my Test Enviroment i want to configure my logging so that for certain error levels an email will be sent out with the log message after logging. I identify the develper to whom mail shld be sent by looking at the properties files which have the ClassName mapped to the programmer and programmer mapped to email.

Now in the logger i want to know which class actually made the call, to be able to look at the properties file to identify the programmer and his email.

Thanks,

Srini
 
Don't get me started about those stupid light bulbs.
 
subject: Find Calling Class' name within Called Class
 
Similar Threads
Calling the Methods into another package
all confused about scope....
accessing subclass name in superclass
is there any way.....?
Another Constructor Problem....