Java获取当前类名, 文件名,方法名,行号
2018-02-07 本文已影响0人
不敢预言的预言家
System.out.printf("打印当前 类名 %s\n",
Thread.currentThread().getStackTrace()[1].getClassName());
// 打印当前 类名 tmp.Demo
System.out.printf("打印当前 方法名 %s\n",
Thread.currentThread().getStackTrace()[1].getMethodName());
// 打印当前 方法名 main
System.out.printf("打印当前 文件名 %s\n",
Thread.currentThread().getStackTrace()[1].getFileName());
// 打印当前 文件名 Demo.java
System.out.printf("打印当前 行号 %s\n",
Thread.currentThread().getStackTrace()[1].getLineNumber());
// 打印当前 行号 13
Thread.currentThread().getStackTrace()[0]
0下标位置获取到的信息是java.lang.Thread
的相关信息