Java에서 현재 스택 상황을 보는 방법

Posted by epicdev Archive : 2011. 11. 16. 20:17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package testpackage;
 
public class ReflectionTest {
     
    public static void main(String[] args) {
        Callee callee = new Callee();
        callee.printB();
    }
}
 
class Callee {
     
    public void printA() {
        StackTraceElement stackTraceElements[] = (new Throwable()).getStackTrace();
        for (int i = 0; i < stackTraceElements.length; i++) {
            System.out.println(stackTraceElements[i]);
        }
        System.out.println("A is called");
    }
     
    public void printB() {
        System.out.println("B is called");
        printA();
    }
}
출력 결과
1
2
3
4
5
B is called
testpackage.Callee.printA(ReflectionTest.java:14)
testpackage.Callee.printB(ReflectionTest.java:23)
testpackage.ReflectionTest.main(ReflectionTest.java:7)
A is called