views:

83

answers:

5

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
+3  A: 

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.

Matt
Thanks, this is what I feared. This sucks.
drozzy
+1  A: 

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);
            }
        }
    }
Han
+2  A: 

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()[0].getMethodName();

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

Malcolm
Thanks, i'm afraid I don't really want to be tied down to the "one-method-per-class" thing...
drozzy
You can get all of the methods of the class or just the method being executed at the moment. See my edits. :)
Malcolm
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...
drozzy
I don't get it, you want to get the name of the method, but how are you gonna specify the method?
Malcolm
+1  A: 

Wait, since you already know the method name, can't you just type it as a string?

Instead of (pseudo) Class.methodName.toString(), just use "methodName".

Otherwise you can use Class#getDeclaredMethods() to get all the methods in a class

Bozho
I don't want to use a string. I may refactor my method name in the future.
drozzy
IDEs have the ability to look into strings when refactoring
Bozho
+1  A: 

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();
Jesper