• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Find Calling Class' name within Called Class

 
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Joe.

-- Srini
 
Gopal Krishna
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe,

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

Thanks,

Srini
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no guarantee that the stacktrace is available at all, though.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic