Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get Name of enclosing Method

having the method

public void foo(){
  //..    
}

Is there a way to get the methodName (in this case foo) at runtime?

I know how to get the classname via

this.getClass().getName()

or to get all public methods via Method[] methods = this.getClass().getMethods(); Once I have the method name the parameters would also be important as there could be several methods with same name

like image 210
Martin Dürrmeier Avatar asked Jun 29 '10 15:06

Martin Dürrmeier


People also ask

How do you find the method name in a method?

Method Class | getName() Method in Java Method class is helpful to get the name of methods, as a String. To get name of all methods of a class, get all the methods of that class object. Then call getName() on those method objects. Return Value: It returns the name of the method, as String.

How can I find the method that called the current method Java?

Getting Name of Current Method inside a method in Java getEnclosingMethod() returns a Method object representing the immediately enclosing method of the underlying class. StackTraceElement. getMethodName()-The java. lang.

What is method name in C#?

Method name − Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class. Parameter list − Enclosed between parentheses, the parameters are used to pass and receive data from a method.


5 Answers

I use code like the following:

  /**
   * Proper use of this class is
   *     String testName = (new Util.MethodNameHelper(){}).getName();
   *  or
   *     Method me = (new Util.MethodNameHelper(){}).getMethod();
   * the anonymous class allows easy access to the method name of the enclosing scope.
   */
  public static class MethodNameHelper {
    public String getName() {
      final Method myMethod = this.getClass().getEnclosingMethod();
      if (null == myMethod) {
        // This happens when we are non-anonymously instantiated
        return this.getClass().getSimpleName() + ".unknown()"; // return a less useful string
      }
      final String className = myMethod.getDeclaringClass().getSimpleName();
      return className + "." + myMethod.getName() + "()";
    }

    public Method getMethod() {
      return this.getClass().getEnclosingMethod();
    }
}

Just leave off the className + "." part and see if it meets your needs.

like image 192
Jim Avatar answered Oct 10 '22 21:10

Jim


I'm not sure why you need to do this, but you can always create a new Throwable() and getStackTace(), then query StackTraceElement.getMethodName().

As a bonus, you get the whole stack trace up to the execution point, not just the immediately enclosing method.

Related questions

  • Java: Determining Current Call Stack (For Diagnostic Purposes)
  • Call trace in java
like image 40
polygenelubricants Avatar answered Oct 10 '22 20:10

polygenelubricants


Reflection is one way. Another slow and potentially unreliable way is with a stack trace.

StackTraceElement[] trace = new Exception().getStackTrace();
String name = trace[0].getMethodName();

Same idea but from the thread:

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
String name = trace[0].getMethodName();
like image 24
Jonathon Faust Avatar answered Oct 10 '22 19:10

Jonathon Faust


Use this code:

System.out.println(new Throwable().getStackTrace()[0].getMethodName());
like image 40
RaZieRSarE Avatar answered Oct 10 '22 21:10

RaZieRSarE


You can hard code the name.

// The Annotation Processor will complain if the two method names get out of synch
class MyClass
{
    @HardCodedMethodName ( ) 
     void myMethod ( )
     {
          @ HardCodedMethodName // Untried but it seems you should be able to produce an annotation processor that compile time enforces that myname = the method name.
          String myname = "myMethod" ;
          ...
     }
}
like image 30
emory Avatar answered Oct 10 '22 20:10

emory