How to get string name of a method in java?

前端 未结 7 688
梦谈多话
梦谈多话 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:28

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

提交回复
热议问题