Getting the name of the currently executing method

前端 未结 22 2756
闹比i
闹比i 2020-11-22 03:33

Is there a way to get the name of the currently executing method in Java?

22条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 04:38

    Both of these options work for me with Java:

    new Object(){}.getClass().getEnclosingMethod().getName()
    

    Or:

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

提交回复
热议问题