3 回答
TA贡献1853条经验 获得超6个赞
Thread.currentThread().getStackTrace()
在某些情况下,某些虚拟机可能从堆栈跟踪中省略一个或多个堆栈帧。在极端情况下,如果虚拟机没有有关此线程的堆栈跟踪信息,则允许该方法返回一个零长度数组。
TA贡献1820条经验 获得超9个赞
String name = new Object(){}.getClass().getEnclosingMethod().getName();YourClass$1.class.class
getEncosingMethod()java.lang.reflect.Method
getEnclosingMethod()SecurityException
getEnclosingConstructor()getEnclosingMethod()null.
TA贡献1821条经验 获得超6个赞
/**
* Get the method name for a depth in call stack. <br />
* Utility function
* @param depth depth in the call stack (0 means current method, 1 means call method, ...)
* @return method name
*/public static String getMethodName(final int depth){
final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
//System. out.println(ste[ste.length-depth].getClassName()+"#"+ste[ste.length-depth].getMethodName());
// return ste[ste.length - depth].getMethodName(); //Wrong, fails for depth = 0
return ste[ste.length - 1 - depth].getMethodName(); //Thank you Tom Tresansky}
我使用JRE 6并给出了不正确的方法名。 如果我写 ste[2 + depth].getMethodName().
0是
getStackTrace(),
1是
getMethodName(int depth)和
2正在调用方法。
添加回答
举报
