Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find Calling Class' name within Called Class

 
Gopal Krishna
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gopal Krishna
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Joe.

-- Srini
 
Gopal Krishna
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,

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

Thanks,

Srini
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go Tiger! (Method added to latest java version)

public StackTraceElement[] getStackTrace()

Since:
1.5
 
Gopal Krishna
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no guarantee that the stacktrace is available at all, though.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic