• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Getting the current method that this executing.

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have about 40 methods that need to
be overwritten and throwing an UnsupportedOperationException(...).

I want to say the following




The result would print out :
Can't do sampleClass.

Any suggestions would be nice.

Thanks

Zak
 
author and iconoclast
Posts: 24204
44
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
Since JDK 1.4, the Throwable class has the "getStackTrace()" method, which returns an array of StackTraceElement objects, which can tell you the name of the method.

String methodName = new Throwable().getStackTrace[0].getMethodName();

In real code, I wouldn't chain all this together: getStackTrace() can return a 0-length array. You'd have to check for that.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Note that Ernest forgot the () after getStackTrace. To make it safe against getStackTrace() returning an empty array, you'd write it like this:

[ July 25, 2006: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above posts do give good guidance about how to get the current method name. However, I am a bit doubtful why you want to do that, in your particular example.

Do the exception messages really need to contain the method name? The exception stack traces will make the method name clear anyway. Perhaps you are logging exception messages, but not logging stack traces?

If you do need the method name in the message, I would have thought it would be quicker for you, faster to execute, easier to maintain just to "bite the bullet" and type in those 40 method names. If you want to be really flash, a smart editor could probably be programmed to do it automatically (I reckon I could get emacs to do it).

In general, always favour simple obvious code over clever code. As a rule, if someone says your code is "clever", it's an insult, not a compliment!
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic