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;
    }
}
相關文章
相關標籤/搜索