在运行期间可以通过throwable类来获取方法调用堆栈信息,例如哪个方法调用的当然方法,当前运行类的类名,文件名,代码行等等。
Throwable ex = new Throwable();
StackTraceElement[] stackElements = ex.getStackTrace();
if(stackElements != null)
{
for(int i = 0; i < stackElements.length; i++)
{
System.out.println(stackElements[i].getClassName());
System.out.println(stackElements[i].getFileName());
System.out.println(stackElements[i].getLineNumber());
System.out.println(stackElements[i].getMethodName());
System.out.println("-----------------------------------");
}
}
Throwable ex = new Throwable();
StackTraceElement[] stackElements = ex.getStackTrace();
if(stackElements != null)
{
for(int i = 0; i < stackElements.length; i++)
{
System.out.println(stackElements[i].getClassName());
System.out.println(stackElements[i].getFileName());
System.out.println(stackElements[i].getLineNumber());
System.out.println(stackElements[i].getMethodName());
System.out.println("-----------------------------------");
}
}