
StackWalker API 是 Java 9 中的一项新功能,它提高了前身堆栈的性能轨道元素。它还可以提供一种在异常情况下过滤堆栈元素或了解应用程序行为的方法。在 Java 9 中,访问堆栈跟踪的方式非常有限,并且一次提供整个堆栈信息。
在下面的示例中,我们需要打印堆栈帧中的所有属性 p>
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");
<strong>StackWalker </strong>newWalker = StackWalker.getInstance(<strong>Option</strong>.<strong>RETAIN_CLASS_REFERENCE</strong>);
<strong>List<StackWalker.StackFrame></strong> stackFrames = newWalker.walk(frames -> frames.limit(1).collect(<strong>Collectors.toList()</strong>));
stackFrames.forEach(test-> {
System.out.printf("[Bytecode Index] %d%n", test.<strong>getByteCodeIndex()</strong>);
System.out.printf("[Class Name] %s%n", test.<strong>getClassName()</strong>);
System.out.printf("[Declaring Class] %s%n", test.<strong>getDeclaringClass()</strong>);
System.out.printf("[File Name] %s%n", test.<strong>getFileName()</strong>);
System.out.printf("[Method Name] %s%n", test.<strong>getMethodName()</strong>);
System.out.printf("[Is Native] %b%n", test.<strong>isNativeMethod()</strong>);
System.out.printf("[Line Number] %d%n", test.<strong>getLineNumber()</strong>);
});
}
}<strong>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</strong>
以上就是在Java 9中如何打印StackFrame API中的所有属性?的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号