java直接得到当前运行的类名以及方法名

public class ClassUtils {

    /**
     * 得到当前方法名
     * @return
     */
    public static String getCurrentMethodName() {
        int level = 1;
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        String methodName = stacks[level].getMethodName();
        return methodName;
    }

    /**
     * 得到当前类名
     * @return
     */
    public static String getCurrentClassName() {
        int level = 1;
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        String className = stacks[level].getClassName();
        return className;
    }
}
相关文章
相关标签/搜索