使人疲惫的不是远方的高山,而是鞋子里的一粒沙子。——伏尔泰

转载,原文戳我

码住,这个确实感觉不错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//获取方法名:
public static String getCurrentMethodName() {
int level = 1;
StackTraceElement[] stacks = new Throwable().getStackTrace();
String methodName = stacks[level].getMethodName();
return methodName;
}
//获取类名:
public static String getCurrentClassName() {
int level = 1;
StackTraceElement[] stacks = new Throwable().getStackTrace();
String className = stacks[level].getClassName();
return className;
}

//获取行号:
public static int getLineNumber() {
int level = 1;
StackTraceElement[] stacks = new Throwable().getStackTrace();
int lineNumber = stacks[level].getLineNumber();
return lineNumber;
}