How to get string name of a method in java?

前端 未结 7 703
梦谈多话
梦谈多话 2020-11-29 06:10

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

For example given:

class Car{
   public void getFoo(){
   }
}

7条回答
  •  臣服心动
    2020-11-29 06:24

    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);
                }
            }
        }
    

提交回复
热议问题