How to get string name of a method in java?

java get method name for logging
how to get the calling class and method name in java
java get calling method name
java get current method object
java get current method annotation
how to call get method in java
getname() method in java thread
how to get methods of a class in java using reflection

How can I find out through reflection what is the string name of the method?

For example given:

class Car{
   public void getFoo(){
   }
}

I want to get the string "getFoo", something like the following:

 Car.getFoo.toString() == "getFoo" // TRUE

Since methods aren't objects themselves, they don't have direct properties (like you would expect with first-class functions in languages like JavaScript).

The closest you can do is call Car.class.getMethods()

Car.class is a Class object which you can use to invoke any of the reflection methods.

However, as far as I know, a method is not able to identify itself.

How to get string name of a method in java?, Get name of current method being executed in Java. Getting name of currently method name at 0th index. String nameofCurrMethod = new Throwable(). And finally, if you want to see the name of the method, which is executing at the moment, you should use this code: Thread.currentThread().getStackTrace()[1].getMethodName(); This will get a stack trace for the current thread and return the name of the method on its top.

You can get the String like this:

Car.class.getDeclaredMethods()[0].getName();

This is for the case of a single method in your class. If you want to iterate through all the declared methods, you'll have to iterate through the array returned by Car.class.getDeclaredMethods():

for (Method method : Car.class.getDeclaredMethods()) {
    String name = method.getName();
}

You should use getDeclaredMethods() if you want to view all of them, getMethods() will return only public methods.

And finally, if you want to see the name of the method, which is executing at the moment, you should use this code:

Thread.currentThread().getStackTrace()[1].getMethodName();

This will get a stack trace for the current thread and return the name of the method on its top.

Get name of current method being executed in Java, Overview. Sometimes we need to know the name of the current Java method being executed. String methodName = new Object() {} .getClass  The toString method returns a String representation of an object in Java. By default, the toString method returns the name of the object’s class plus its hash code. Here, you find out how to use the toString method and how to override it in your own classes to create more useful strings.

So, you want to get the name of the currently executing method? Here's a somewhat ugly way to do that:

Exception e = new Exception();
e.fillInStackTrace();
String methodName = e.getStackTrace()[0].getMethodName();

How to Get a Name of a Method Being Executed?, The name parameter is a String specifying the simple name of the desired method. The parameterTypes parameter is an array of Class objects that identify the  Sometimes we need to know the name of the current Java method being executed. This quick article presents a couple of simple ways of getting hold of the method name in the current execution stack. 2.

Look into this thread:

Getting the name of the currently executing method

It offers some more solutions - for example:

String name = new Object(){}.getClass().getEnclosingMethod().getName();

Java.lang.Class.getMethod() Method, The Java Tutorials have been written for JDK 8. However, a method might have the same name as other methods due to method overloading. draw various types of data (strings, integers, and so on) and that contains a method for drawing​  The java.lang.Class.getName() returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String. Declaration. Parameters. Return Value. This method returns the name of the class or interface represented by this object.

try this,

 import java.lang.reflect.*;
    public class DumpMethods {
        public static void main(String args[]) {
            try {
                Class c = Class.forName(args[0]);
                Method m[] = c.getDeclaredMethods();
                for (int i = 0; i < m.length; i++)
                    System.out.println(m[i].toString());
            } catch (Throwable e) {
                System.err.println(e);
            }
        }
    }

Defining Methods (The Java™ Tutorials > Learning the Java , The String class has a number of methods for examining the contents of strings, finding Use the charAt method to get a character at a particular index. illustrates the use of lastIndexOf() and substring() to isolate different parts of a file name. The java.lang.Class.getMethod () returns a Method object that reflects the specified public member method of the class or interface represented by this Class object. The name parameter is a String specifying the simple name of the desired method. The parameterTypes parameter is an array of Class objects that identify the method's formal

Manipulating Characters in a String (The Java™ Tutorials , To call a method, write the method's name followed by two parentheses You will often see Java programs that have either static or public attributes and methods. call the methods on the myCar object public static void main(String[] args)  By default, javac puts a local variable table in the class for each method to assist debugging. Use the -l option to javap to see the local variable table. – erickson Apr 13 '09 at 19:17. Not by default, it seems. I had to use javac -g:vars to get it.

Java Class Methods, To obtain a Java class reference, use the .java property on a KClass instance. or an extension function, it needs to be qualified, e.g. String::toCharArray . to read its value using get() or retrieve the property name using the name property. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.

Reflection, However we have not created any string object using new keyword above. The compiler does that task for us it creates a string object having the string literal (​that  Every class in java is child of Object class either directly or indirectly. Object class contains toString () method. We can use toString () method to get string representation of an object. Whenever we try to print the Object reference then internally toString () method is invoked. If we did not define toString () method in your class then

Comments
  • Thanks, i'm afraid I don't really want to be tied down to the "one-method-per-class" thing...
  • You can get all of the methods of the class or just the method being executed at the moment. See my edits. :)
  • Thanks. Curious. But I do need a name of a method that is Not currently executing. It seems like java does not provide a way to do that...
  • Hi , by doing Thread.currentThread().getStackTrace()[0].getMethodName(); yo get the first method in the stack , which at that moment is "getStackTrace" ,you should do Thread.currentThread().getStackTrace()[1].getMethodName(); in order to get the one which have the code line, if you want to decouple this method so you dont have to duplicate it in each place i suggest to make it static in a class and call it with [2], i haven't tryed thought i seems logic for it to work.
  • On Android the method name is in the third element: Thread.currentThread().getStackTrace()[2].getMethodName()
  • Actually, this.getClass().getEnclosingMethod() works just fine!
  • momomo, no, it doesn't. class Test { @Test public void testReflection() { assert (this.getClass().getEnclosingMethod() == null); } }
  • This should be the new accepted answer.
  • When implementing "ReflectionUtils.findMethod" be sure to use "getDeclaredMethod("writeReplace")" or "getDeclaredMethods()" not "getMethod("writeReplace")" or "getMethods()"
  • I don't want to use a string. I may refactor my method name in the future.
  • IDEs have the ability to look into strings when refactoring