Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply

Java - Get the Method Name

 
Thread Tools Search this Thread
Old 05-11-2006, 06:41 AM   #1
IveCal
 
Posts: n/a
Default Get the Method Name

Hello . . . Somebody help me . . . How will I get the name of the
present working method . . . A code snippet below is shown with the
part where I want to return the name of the method. please reply . . .
thanks a lot . . .



import java.lang.reflect.*;

public class This
{

public This(){}

public void thisMethod()
{
Class keyClass = this.getClass();
String keyName = keyClass.getName()
System.out.println("Class: "+keyName+", Method: "<<RETURN THE METHOD
NAME>>) );
}


public static void main(String[] args)
{
This t = new This();
t.thisMethod();
}
}

  Reply With Quote
Old 05-11-2006, 07:01 AM   #2
oulan bator
 
Posts: n/a
Default Re: Get the Method Name

Don't know better way than
-throwing an Exception locally,
-catching it in a catch() statement
-getting the StrackTraceElements

and here you are !
Rodney has a snippet in his website !

  Reply With Quote
Old 05-11-2006, 07:37 AM   #3
IveCal
 
Posts: n/a
Default Re: Get the Method Name

Can give me a link to that website . . . Please . . . Thanks . . .

  Reply With Quote
Old 05-11-2006, 07:44 AM   #4
Simon
 
Posts: n/a
Default Re: Get the Method Name

You can also try one of the following:

1) At first sight, this looks like a good solution, but I think it isn't:

Thread.getStackTrace();

This method is not guaranteed to return something usefull, and, actually, it
doesn't. The first two elements of the stack trace it returns (JDK 1.6) are
actually the method getStackTrace() itself and a helper method. You may assume
that the third element (or better the first element that is not an instance
method of Thread) is actually the one you are interested in but that may depend
on the implementation of the standard library.

2) This method is almost the same as oulan suggested, but you needn't catch the
exception:

StackTraceElement stack[] = (new Throwable()).getStackTrace();

Looks like a hack but I have actually stolen it from
java.util.LogRecord.inferCaller().


Here's a sample output:

import java.util.Arrays;
public class ST {

private static void test() {
System.out.println(
Arrays.toString(Thread.currentThread().getStackTra ce()));
}

private static void test2() {
System.out.println(
Arrays.toString(new Throwable().getStackTrace()));
}
public static void main(String[] argv) {
test();
test2();
}
}

prints

[java.lang.Thread.dumpThreads(Native Method),
java.lang.Thread.getStackTrace(Thread.java:1383), ST.test(ST.java:5),
ST.main(ST.java:12)]

[ST.test2(ST.java:9), ST.main(ST.java:13)]


Cheers,
Simon
  Reply With Quote
Old 05-11-2006, 11:11 AM   #5
Thomas Fritsch
 
Posts: n/a
Default Re: Get the Method Name

> Can give me a link to that website . . . Please . . . Thanks . . .
>

http://mindprod.com/jgloss/trace.html
  Reply With Quote
Old 05-11-2006, 05:33 PM   #6
Roedy Green
 
Posts: n/a
Default Re: Get the Method Name

On 11 May 2006 00:01:57 -0700, "oulan bator" <>
wrote, quoted or indirectly quoted someone who said :

>Rodney has a snippet in his website !


I think you mean me, Roedy, and the entry you would want is
http://mindprod.com/jgloss/trace.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
  Reply With Quote
Old 05-11-2006, 06:27 PM   #7
oulan bator
 
Posts: n/a
Default Re: Get the Method Name

yes, it was you, sorry for the name ...

  Reply With Quote
Old 05-12-2006, 05:28 AM   #8
Roedy Green
 
Posts: n/a
Default Re: Get the Method Name

On 11 May 2006 11:27:32 -0700, "oulan bator" <>
wrote, quoted or indirectly quoted someone who said :

>yes, it was you, sorry for the name ...


not to worry. Almost nobody gets my name right verbally. I have
learned to respond to any name beginning with R.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
  Reply With Quote
Old 05-12-2006, 05:28 AM   #9
Roedy Green
 
Posts: n/a
Default Re: Get the Method Name

On 11 May 2006 11:27:32 -0700, "oulan bator" <>
wrote, quoted or indirectly quoted someone who said :

>yes, it was you, sorry for the name ...


Even my mother used to sometimes call me "Roger".
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
  Reply With Quote
Old 05-18-2006, 10:04 PM   #10
dimitar
 
Posts: n/a
Default Re: Get the Method Name

If you use Sun JVM 1.5 can afford to write non-portable code, the
easiest (and the fastest) way is:

sun.reflect.Reflection#getCallerClass(int realFramesToSkip)

To get the name of the current method, realFramesToSkip=1

Dimitar
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cancelling Timer and TimerTask threads wfalby Software 1 04-30-2009 12:04 PM
Help - Method for converting float to double mcvishnuprasad The Lounge 2 07-24-2008 01:00 PM
ASP.NET: Asign Users in Roles(Array.IndexOf(Of String) method) msandlana Software 0 04-25-2008 05:37 AM
GetObject method in C# manuadoor Software 0 05-04-2007 06:44 AM
best method to capture screen shots DP DVD Video 0 09-21-2003 08:57 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47