![]() |
|
|
|
#1 |
|
Posts: n/a
|
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(); } } |
|
|
|
#2 |
|
Posts: n/a
|
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 ! |
|
|
|
#3 |
|
Posts: n/a
|
Can give me a link to that website . . . Please . . . Thanks . . .
|
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
> Can give me a link to that website . . . Please . . . Thanks . . .
> http://mindprod.com/jgloss/trace.html |
|
|
|
#6 |
|
Posts: n/a
|
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. |
|
|
|
#7 |
|
Posts: n/a
|
yes, it was you, sorry for the name ...
|
|
|
|
#8 |
|
Posts: n/a
|
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. |
|
|
|
#9 |
|
Posts: n/a
|
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. |
|
|
|
#10 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |