首頁  >  文章  >  Java  >  在Java 9中如何列印StackFrame API中的所有屬性?

在Java 9中如何列印StackFrame API中的所有屬性?

王林
王林轉載
2023-08-28 08:37:02589瀏覽

在Java 9中如何打印StackFrame API中的所有属性?

StackWalker APIJava 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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除