通过Thread获取类文件名、类名、当前执行方法,以及行号信息等

package com.extend.test;

public class GetMethodName {
    public static void main(String[] args) {
    String classFile = Thread.currentThread().getStackTrace()[1]
            .getFileName();
    String methodName = Thread.currentThread().getStackTrace()[1]
            .getMethodName();
    int methodNameLine = Thread.currentThread().getStackTrace()[1]
            .getLineNumber();
    String className = Thread.currentThread().getStackTrace()[1]
            .getClassName();
    System.out.println("类文件名是:" + classFile);
    System.out.println("当前方法是:" + methodName);
    System.out.println("当前行号:" + methodNameLine);
    System.out.println("类名是:" + className);
    }
}
运行结果:
                类文件名是:GetMethodName.java
                当前方法是:main
                当前行号:9
                类名是:com.extend.test.GetMethodName

你可能感兴趣的:(通过Thread获取类属性)