java.langStackTraceElement
类保存了Java中线程中的方法栈信息:
4个属性:
代码语言:javascript复制/**
* 声明的类,是类的全限定名
*/
private String declaringClass;
/**
* 方法名
*/
private String methodName;
/**
* 文件名一般指:XXX.java
*/
private String fileName;
/**
* XX.java类中的行数
*/
private int lineNumber;
一个简单的在SpringBoot 2.0.5-RELEASE 下的示例:
代码语言:javascript复制for ( StackTraceElement ele : Thread.currentThread().getStackTrace()) {
System.out.println("线程栈信息输出:" ele.getClassName() "$" ele.getMethodName() "$" ele.getFileName() "$" ele.getLineNumber());
}
输出结果如下:
代码语言:javascript复制线程栈信息输出:java.lang.Thread$getStackTrace$Thread.java$1559
线程栈信息输出:com.example.myboot.businessTests.RabbitMQTest$test2$RabbitMQTest.java$54
线程栈信息输出:sun.reflect.NativeMethodAccessorImpl$invoke0$NativeMethodAccessorImpl.java$-2
线程栈信息输出:sun.reflect.NativeMethodAccessorImpl$invoke$NativeMethodAccessorImpl.java$62
线程栈信息输出:sun.reflect.DelegatingMethodAccessorImpl$invoke$DelegatingMethodAccessorImpl.java$43
线程栈信息输出:java.lang.reflect.Method$invoke$Method.java$498
线程栈信息输出:org.junit.runners.model.FrameworkMethod$1$runReflectiveCall$FrameworkMethod.java$50
线程栈信息输出:org.junit.internal.runners.model.ReflectiveCallable$run$ReflectiveCallable.java$12
线程栈信息输出:org.junit.runners.model.FrameworkMethod$invokeExplosively$FrameworkMethod.java$47
线程栈信息输出:org.junit.internal.runners.statements.InvokeMethod$evaluate$InvokeMethod.java$17
线程栈信息输出:org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks$evaluate$RunBeforeTestExecutionCallbacks.java$73
线程栈信息输出:org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks$evaluate$RunAfterTestExecutionCallbacks.java$83
线程栈信息输出:org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks$evaluate$RunBeforeTestMethodCallbacks.java$75
线程栈信息输出:org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks$evaluate$RunAfterTestMethodCallbacks.java$86
线程栈信息输出:org.springframework.test.context.junit4.statements.SpringRepeat$evaluate$SpringRepeat.java$84
线程栈信息输出:org.junit.runners.ParentRunner$runLeaf$ParentRunner.java$325
线程栈信息输出:org.springframework.test.context.junit4.SpringJUnit4ClassRunner$runChild$SpringJUnit4ClassRunner.java$251
线程栈信息输出:org.springframework.test.context.junit4.SpringJUnit4ClassRunner$runChild$SpringJUnit4ClassRunner.java$97
线程栈信息输出:org.junit.runners.ParentRunner$3$run$ParentRunner.java$290
线程栈信息输出:org.junit.runners.ParentRunner$1$schedule$ParentRunner.java$71
线程栈信息输出:org.junit.runners.ParentRunner$runChildren$ParentRunner.java$288
线程栈信息输出:org.junit.runners.ParentRunner$access$000$ParentRunner.java$58
线程栈信息输出:org.junit.runners.ParentRunner$2$evaluate$ParentRunner.java$268
线程栈信息输出:org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks$evaluate$RunBeforeTestClassCallbacks.java$61
线程栈信息输出:org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks$evaluate$RunAfterTestClassCallbacks.java$70
线程栈信息输出:org.junit.runners.ParentRunner$run$ParentRunner.java$363
线程栈信息输出:org.springframework.test.context.junit4.SpringJUnit4ClassRunner$run$SpringJUnit4ClassRunner.java$190
线程栈信息输出:org.junit.runner.JUnitCore$run$JUnitCore.java$137
线程栈信息输出:com.intellij.junit4.JUnit4IdeaTestRunner$startRunnerWithArgs$JUnit4IdeaTestRunner.java$68
线程栈信息输出:com.intellij.rt.execution.junit.IdeaTestRunner$Repeater$startRunnerWithArgs$IdeaTestRunner.java$47
线程栈信息输出:com.intellij.rt.execution.junit.JUnitStarter$prepareStreamsAndStart$JUnitStarter.java$242
线程栈信息输出:com.intellij.rt.execution.junit.JUnitStarter$main$JUnitStarter.java$70
从结果中我们可以看出, 栈顶是Thread的getStackTrace方法,这个很好理解。Thread.currentThread().getStackTrace()[1] 是当前执行查看堆栈信息的方法RabbitMQTest的test2。 栈底是 JUnitStarter的main 方法,然后我们看到实际调用了 org.springframework.test.context.junit4.SpringJUnit4ClassRunner的run 方法。