Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Get the Method Name

Thread view: 
IveCal - 11 May 2006 07:41 GMT
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();
    }
}
oulan bator - 11 May 2006 08:01 GMT
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 !
IveCal - 11 May 2006 08:37 GMT
Can  give me a link to that website . . . Please . . . Thanks . . .
Thomas Fritsch - 11 May 2006 12:11 GMT
> Can  give me a link to that website . . . Please . . . Thanks . . .

http://mindprod.com/jgloss/trace.html
Simon - 11 May 2006 08:44 GMT
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().getStackTrace()));
   }

   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
Roedy Green - 11 May 2006 18:33 GMT
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

oulan bator - 11 May 2006 19:27 GMT
yes, it was you, sorry for the name ...
Roedy Green - 12 May 2006 06:28 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 12 May 2006 06:28 GMT
>yes, it was you, sorry for the name ...

Even my mother used to sometimes call me "Roger".
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

dimitar - 18 May 2006 23:04 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.