Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling class's name while in called method?

Status
Not open for further replies.

ericSedor

Programmer
Jun 18, 2007
3
US
how do I get the class name of the calling class while in the called class's method?

I'm seeking to put this name in a new log message based upon some validation results that will be performed in the called method. I cannot change the interface easily so I am trying to figure out how to determine the calling class's name inside of the called class.

Anyone ever done this? (I've searched around a bit and basically, it appears not do-able but I figured I would check here.) Any advise?
 
Remember that reflect package is not supported by Sun anymore, and its use is discouraged.

I'd try something like this:

Code:
new Exception().getStackTrace()[0].getClassName();

Cheers,
Dian
 
Remember that reflect package is not supported by Sun anymore, and its use is discouraged.

I was not aware of that Dian. The JDK1.6 apidocs don't say anything in the java.lang.reflect package.

Tim
 
thanks for the update, I did not realize that the Sun package was not supported. I'll switch over.
 
I'm sorry, I think I wrote too fast.

I said the package was not supported, but I cannot assure that. What I should have said is that the package is not included in the JDK (it's not in the API) and it may cause some incompatibilities with other JVMs.

Cheers,
Dian
 
Yes, be careful of that. Typically, the sun specific packages are Sun's implementation of parts of the public API. If you were to use one directly you'd become unstuck if, for instance, you tried to run your code on IBM's JDK for AIX which would come with IBM's own implementation packages. Stick to the public API classes.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top