Java如何获取当前已执行方法的名称?
package org.nhooo.example.lang;
public class GetCurrentMethodName {
public static void main(String[] args) {
//获取当前执行方法的名称
String methodName =
Thread.currentThread().getStackTrace()[1].getMethodName();
System.out.println("methodName = " + methodName);
GetCurrentMethodName obj = new GetCurrentMethodName();
obj.executeAMethod();
}
private void executeAMethod() {
//获取当前执行方法的名称
String methodName =
Thread.currentThread().getStackTrace()[1].getMethodName();
System.out.println("methodName = " + methodName);
}
}该程序将打印以下字符串:
methodName = main methodName = executeAMethod
热门推荐
10 祝老婆生日祝福语简短
11 恭喜朋友买车祝福语简短
12 节日祝福语幽默简短的
13 姐妹结婚快乐祝福语简短
14 对女友的简短祝福语
15 很暧昧的简短祝福语
16 分手新年祝福语大全简短
17 对生活祝福语简短精辟
18 祝球员赛前祝福语简短