StackWalker API 자바 9의 새로운 기능입니다. 전임자 의 성능을 향상시킵니다. 스택 트랙 요소. 또한 예외의 경우 스택 요소를 필터링하는 방법을 제공할 수 있습니다. 또는 응용 프로그램 이해 행동 . Java 9에서는 스택 추적에 액세스하는 방법이 매우 제한되어 있으며 전체 스택 정보를 한 번에 제공합니다.
아래 예에서는 스택 프레임의 모든 속성을 인쇄해야 합니다.
예
import java.lang.StackWalker.StackFrame;
import java.util.*;
import java.util.stream.*;
import java.lang.StackWalker.Option;
public class AllAttributesTest {
public static void main(String args[]) {
System.out.println("Java 9 Stack Walker API - Print all attributes in stack frame");
StackWalker newWalker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE);
List<StackWalker.StackFrame> stackFrames = newWalker.walk(frames -> frames.limit(1).collect(Collectors.toList()));
stackFrames.forEach(test-> {
System.out.printf("[Bytecode Index] %d%n", test.getByteCodeIndex());
System.out.printf("[Class Name] %s%n", test.getClassName());
System.out.printf("[Declaring Class] %s%n", test.getDeclaringClass());
System.out.printf("[File Name] %s%n", test.getFileName());
System.out.printf("[Method Name] %s%n", test.getMethodName());
System.out.printf("[Is Native] %b%n", test.isNativeMethod());
System.out.printf("[Line Number] %d%n", test.getLineNumber());
});
}
} 출력
Java 9 Stack Walker API - Print all attributes in stack frame [Bytecode Index] 21 [Class Name] AllAttributesTest [Declaring Class] class AllAttributesTest [File Name] AllAttributesTest.java [Method Name] main [Is Native] false [Line Number] 10