• 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

Get the currently executing method name w/ getName()

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
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
You can get the stack trace from java.lang.Throwable. The top of the stack is the currently executing method.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Barb Rudnick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Thanks!

 
Barb Rudnick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brilliant!



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

Thanks again.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic