Maison  >  Article  >  Java  >  Comment imprimer toutes les propriétés de l'API StackFrame en Java 9 ?

Comment imprimer toutes les propriétés de l'API StackFrame en Java 9 ?

王林
王林avant
2023-08-28 08:37:02587parcourir

L'

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

StackWalker API est une nouvelle fonctionnalité de Java 9 qui améliore les performances des éléments prédécesseurstack. Il peut également fournir un moyen de filtrer les éléments de la pile dans des situations exceptionnelles ou de comprendre le application comportement. Dans Java 9, l'accès aux traces de pile est très limité et toutes les informations de la pile sont disponibles en même temps.

Dans l'exemple ci-dessous, nous devons imprimer toutes les propriétés dans le cadre de pile p>

Exemple

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>);
      });
   }
}

Sortie

<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>

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer