• 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

Getting Name of current Method

 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

My requirement is to get the name of current method.
Is there any method which dose it apart from following option which is very bad way to do it for loggin when therz no exception.

ex

public void updateUser()
{
System.out.pritln(new Exception().getStackTrace()[0].getMethodName());

}
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use
Method m=new Method();
m.getName();

getName method returns the method name.

I hope I am close to what you have expected..

Regards
Rohit
 
Praful Thakare
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Therez no such constructor with zero arguments in class Method


-Praful
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right..I am sorry for giving wrong ans

you can use

Class.getMethods() which will return you the array of Method and then iterate over the array.

Regards
Rohit
[ March 14, 2005: Message edited by: Rohit Bhagwat ]
 
Praful Thakare
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rohit Bhagwat:
Yes you are right..I am sorry for giving wrong ans

you can use

Class.getMethods() which will return you the array of Method and then iterate over the array.

Regards
Rohit

[ March 14, 2005: Message edited by: Rohit Bhagwat ]



Hi Rohit,

Thanx a bunch for your replies,I guess I was not able to frame my question properly. Let me try again.

The method you talking about,will give me names of all methods,but it wont help me as I just need to print the name of current method which is executing the (SOP) statement.

-Praful
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using Java 1.5, you can do it the following way



You can call this method as
 
Praful Thakare
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mani,

Thanx a bunch for your reply

will try this out


-Praful
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I dont think so method names change over night....so no need to be dynamic in that sense.. some situations are better if hard coded
 
Praful Thakare
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I dont think so method names change over night....so no need to be dynamic in that sense.. some situations are better if hard coded



True,they dont,but my requirement is to put usertracking messages where i need to log entery and exit on every method (already defined) so harding coding will take lots of pains as it needs copy paste + changing name of method...

-Praful
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using 1.4.2, does anyone know of a way to do this using that version?

Dave
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure how adding this to each method



is any less trouble than adding



or define a static logging method somewhere



??

Am I missing the point of this somewhere?

[ March 16, 2005: Message edited by: Ben Wood ]
[ January 17, 2006: Message edited by: Jim Yingst ]
 
(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 using a vendor product that uses static constants and local variables like this:

I made an Eclipse keyboard shortcut for the PACKAGE and CLASS constants, one for the METHOD variable and more for entry & exit logging. My project prefix is "mc" so I type mcen+space to get the logEntry line for example. A little ugly but fairly painless.
[ March 16, 2005: Message edited by: Stan James ]
 
Dave Hewy
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben,

you're right that just having a string is probably quicker up front, but down the line as code matures and gets refactored it's one less thing to worry about updating if you have some dynamic way to "look up" the current method.

Dave.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Log4J allows you to specify a logging format, which may include class name, method name, and line number. So your code can be as simple as

logger.info("Began processing");

and your output might be something like

2005-03-16 20:04:44.386Z Info | foo.bar.MyClass myMethod() line 20 | Began processing

This is achieved by log4j by creating a new Throwable, printing the stack trace, and parsing the output. This is more difficult (internally) than it usually needs to be, because it does not assume that JDK 1.4+ is available. Nowadays we could use getStackTrace() to access the info more directly. Be aware that creating an entire new Throwable with complete statck trace info just to get a few short strings of information is rather inefficient, and so if you use a log format that asks for this info you're making the logging less efficient than it might be. However in many cases this is fine (as long as you're not logging inside any tight loops) and the convenience is well worthwhile.

A simple way to achieve something like this without log4j is to put it in a single method:

I recommend log4j though, as a much more flexible logging infrastructure has already been built for you. You can control the format and other things with a configuration file, which makes it easier to increase or decrease the level of logging at runtime, without having to change any code. So when you're testing and debugging you can increase the amount of info that gets logged, perhaps at the expense of performance. In production you cna decrease the level of logging so performance improves. If problems are detedted in the production system, you can then increase the logging again, until the problem has been successfully diagnosed.
[ January 17, 2006: Message edited by: Jim Yingst ]
 
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
Jim, do you have any feeling for the overhead of creating a throwable to get the stack trace. My first reaction is "Yikes!" but maybe it's nothing.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, didn't see this earlier. Yeah, I think it could indeed be "Yikes" in some situations. I just ran a simple test:

This seems to run about 500 times slower if I uncomment the getCallingMethod(). (With suitable adjustments of the number of iteration, to make sure the JVM is warmed up but I don't have to wait for hours.) So yeah, if you want to log the method name inside a tight loop, you can seriously damage your performance. Then again, printing a simple message to the console seems to take about ten times as long as getting the stack trace, on my system. (Writing to a file does take considerably less than either, fortunately.) So it's all relative. Mont jobs I've worked on, it wasn't really a problem to tail -f the logfile of an application as it went, which to me indicates that the time to gather method names would be even less significant. And there can certainly be some benefits to having method name and line number in your logs. But of course if you're logging inside a tight loop, the situation can change drastically, and the performance penalty for logging method names may be horrible. I think that like most performance "problems" - most of the time it's not a problem at all. But you want to be aware of it, so that if and when it is a problem, you can make appropriate adjustments.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic