7

I know that you can use reflection in Java to get the name of class, methods, fields...etc at run time. I was wondering can a method figure out its own name while its inside it's self? Also, I don't want to pass the name of the method as a String parameter either.

For example

public void HelloMyNameIs() {
  String thisMethodNameIS = //Do something, so the variable equals the method name HelloMyNameIs. 
}

If it that is possible, I was thinking it would probably involve using reflection, but maybe it doesn't.

If anybody know, it would be greatly appreciated.

1

4 Answers 4

9

Use:

public String getCurrentMethodName()
{
     StackTraceElement stackTraceElements[] = (new Throwable()).getStackTrace();
     return stackTraceElements[1].toString();
}

inside the method you want to get the name of.

public void HelloMyNameIs()
{
    String thisMethodNameIS = getCurrentMethodName();
}

(Not reflection, but I don't think it is possible.)

Sign up to request clarification or add additional context in comments.

7 Comments

There are performance implications with this technique, so use with caution. But it's the best solution you're going to get.
@Kirk Woll: Very true. And because of that, it really shouldn't be used in a release build, only for debugging purposes.
Why not use Thread.currentThread().getStackTrace() instead of new Throwable?
But only one of them creates an extra Throwable object
Its possible to avoid this extra object creation, that's all
|
6

This one-liner works using reflection:

public void HelloMyNameIs() {
  String thisMethodNameIS = new Object(){}.getClass().getEnclosingMethod().getName();
}

The downside is that the code can't be moved to a separate method.

Comments

3

Using a Proxy all your methods (that override a method defined in an interface) can know their own names.

import java . lang . reflect . * ;

interface MyInterface
{
      void myfun ( ) ;
}

class MyClass implements MyInterface
{
      public void myfun ( ) { /* implementation */ }
}

class Main
{
      public static void main ( String [ ] args )
      {
            MyInterface m1 = new MyClass ( ) ;
            MyInterface m2 = ( MyInterface ) ( Proxy . newProxyInstance (
                  MyInterface . class() . getClassLoader ( ) ,
                  { MyInterface . class } ,
                  new InvocationHandler ( )
                  {
                        public Object invokeMethod ( Object proxy , Method method , Object [ ] args ) throws Throwable
                        {
                             System . out . println ( "Hello.  I am the method " + method . getName ( ) ) ;
                             method . invoke ( m1 , args ) ;
                        }
                  }
            ) ) ;
            m2 . fun ( ) ;
      }
}

Comments

0

Stack trace from current thread also:

public void aMethod() {  
    System.out.println(Thread.currentThread().getStackTrace()[0].getMethodName()); 
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.