aspose file tools*
The moose likes Beginning Java and the fly likes Get the currently executing method name w/ getName() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get the currently executing method name w/ getName()" Watch "Get the currently executing method name w/ getName()" New topic
Author

Get the currently executing method name w/ getName()

Barb Rudnick
Greenhorn

Joined: Feb 03, 2005
Posts: 20
I have a need to include a method name in logging files when certain failures occur.

I have searched high & low to find out how to do this in my application and all I've found is fancy-shmancy solutions to problems/needs I don't have.

I use JUNIT to test my code... and in the test methods I use "this.getName()" and it works beautifully.

In the application class/method being tested, "this.getName()" does not work. I can get the class name via "this.getClass().getName()" from any method in the class, but how do I get the method name?

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8708
    
    6

You can get the stack trace from java.lang.Throwable. The top of the stack is the currently executing method.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

In JDK 1.5, you can use Thread.getCurrentThread().getStackTrace() to get an array of StackTraceElement objects representing the current call stack; the one at index 1 will tell you the name of the currently executing method (the zeroth one will be getStackTrace() itself.)

In JDK 1.4, you can create an Exception object and then ask it for its stack trace using getStackTrace(); the zero-th element will be the current method. Don't throw the Exception -- just create it.

In earlier Java versions, you can create an Exception, print the stack trace to a String, and parse it -- not for the faint of heart.


[Jess in Action][AskingGoodQuestions]
Barb Rudnick
Greenhorn

Joined: Feb 03, 2005
Posts: 20
Thanks Joe & Ernest

Stack trace did cross my mind. Had no idea how to make it work, but the "fake" exception sounds doable. Will attempt to make it so.

But tell me, why can't I get the method name using "getName()" as I do in my JUNIT test class?
[ April 26, 2006: Message edited by: Barb Rudnick ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

getName() is a method in junit.framework.TestCase class; you can call it from your test cases because you inherit in from this base class. It's not returning the method name, but a member variable in TestCase that holds the test name; by default, this is the name of the test method, but that's actually not required. In any case, it's set internally by JUnit.
Barb Rudnick
Greenhorn

Joined: Feb 03, 2005
Posts: 20
Great! Thanks!

Barb Rudnick
Greenhorn

Joined: Feb 03, 2005
Posts: 20
Brilliant!



....I guess I'll be writing my own "getName()" next.

Thanks again.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
You can always create a final variable (constant) in a method for logging purpose, as we do in our project:

final String METHOD_NAME = "doSomething()";

:roll:
Barb Rudnick
Greenhorn

Joined: Feb 03, 2005
Posts: 20
rathi, thanks... I'm trying to stay away from constants and hardcoding.
I code back-end API Wrappers that all pretty much look & work the same to get to legacy data. As such I do a lot of "copy/paste/change" ("it is what it is" as my team leader says). I'm trying to make some utility methods so I don't have to change common code too much... again thanks.

But, good news... I have a working version of my method



I invoke the method like this:
....
methodName = getMethodName(getClass().getName().length());

*********

Are there any potential problems with my method?

[ April 26, 2006: Message edited by: Barb Rudnick ]
[ April 26, 2006: Message edited by: Barb Rudnick ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
The javadoc for getStackTrace include the following warning:


Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this throwable is permitted to return a zero-length array from this method. Generally speaking, the array returned by this method will contain one element for every frame that would be printed by printStackTrace.


So this trick for finding the name of the current method is not infallible. But has anyone ever noticed this problem with stack frames? Sometimes in stack traces I see something like "blah blah blah compiled code..." Is that what's being referred to here? And could compiler optimizations like inlining methods also screw up the stack trace?


There is no emoticon for what I am feeling!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I've seen OutOfMemoryError with no stack trace - I think also StackOverflowError, on some systems anyway. Other than that, seems like the stack trace is most always present. It's possible some details get screwed up by compiler optimizations in some cases, but I can't recall ever noticing that effect.

As for "...compiled code..." messages - is it possible you're remembering the output from your IDE rather than a raw stack trace? IDE's will look at the stack trace and try to find source code that goes with it, and if you don't have the source (or the IDE doesn't know where it is), then you get a "compiled code" message. But usually that's still accompanied by a method and line number - it's just that the IDE can't show you the source. Maybe that's not always the case though.

Barb - another issue you might want to beware of is that creating a new Throwable may be a fairly heavy-weight operation (performance-wise) compared to most other things you might be doing. It's pretty minor compared to, say, accessing a database. But it could be huge compared to, say, multiplying two numbers. So in some contexts (like inside a short loop with many repetitions), you might want to avoid using this method. As with most performance issues, you won't really know how big an effect it is unless you measure it. Don't let performance paranoia prevent you from even trying this technique. But be prepared to limit its use if performance turns out to be an issue.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get the currently executing method name w/ getName()
 
Similar Threads
a java challenge
Calling object methods using reference to variables
Thread getName( ) method
DWR or GWT?
how to get the current Object reference name?