JAVA中获取当前运行的类名,方法名,行数

时间:2023-03-09 00:13:36
JAVA中获取当前运行的类名,方法名,行数

JAVA中获取当前运行的类名,方法名,行数

public static String getTraceInfo(){
StringBuffer sb = new StringBuffer(); StackTraceElement[] stacks = new Throwable().getStackTrace();
int stacksLen = stacks.length;
sb.append("class: " ).append(stacks[1].getClassName()).append("; method: ").append(stacks[1].getMethodName()).append("; number: ").append(stacks[1].getLineNumber()); return sb.toString();
} String _methodName = new Exception().getStackTrace()[1].getMethodName();// 获得调用者的方法名 String _thisMethodName = new Exception().getStackTrace()[0].getMethodName();// 获得当前的方法名