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
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.
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.
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.
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.
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.
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();
Use this code:
System.out.println(new Throwable().getStackTrace()[0].getMethodName());
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" ;
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With