Home > Java > javaTutorial > How to print different stack frames using StackWalker API in Java?

How to print different stack frames using StackWalker API in Java?

王林
Release: 2023-09-06 20:17:02
forward
965 people have browsed it

如何使用Java中的StackWalker API打印不同的堆栈帧?

Java 9 defines a StackWalkerAPI which provides laziness and frame filtering. The StackWalker object allows us to walk and access the stack and contains a useful method: walk(). This method opens a StackFramestream for the current thread and then applies the function to that StackFramestream. We need to get the StackWalker object and then use the StackWalker.getInstance() method.

In the example below, we can print different content stack frames: All stack frames, Skip some stack frames and using StackWalkerAPI Limit Stack frames. p>

Example

import java.lang.StackWalker.StackFrame;
import java.util.*;
import java.util.stream.*;

public class StackWalkerTest {
   public static void main(String args[]) {
      new StackWalkerTest().walk();
   }
   private void walk() {
      new Walker1().walk();
   }
   private class Walker1 {
      public void walk() {
         new Walker2().walk();
      }
   }
   private class Walker2 {
      public void walk() {
         Method1();
      }
      void Method1() {
         Method2();
      }
      void Method2() {
         StackWalker stackWalker = <strong>StackWalker.getInstance</strong>(Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE, StackWalker.Option.SHOW_HIDDEN_FRAMES), 16);
         <strong>Stream<StackFrame></strong> stackStream = <strong>StackWalker.getInstance().walk</strong>(f -> f);

         System.out.println("--- Walk all StackFrames ---");
         <strong>List<String></strong> stacks = walkAllStackframes();
         System.out.println(stacks);

         System.out.println("--- Skip some StackFrames ---");
         <strong>List<String></strong> stacksAfterSkip = walkSomeStackframes(3);
         System.out.println(stacksAfterSkip);

         System.out.println("--- Limit StackFrames ---");
         <strong>List<String></strong> stacksByLimit = walkLimitStackframes(3);
         System.out.println(stacksByLimit);
      }
      private List<String> walkAllStackframes() {
         return <strong>StackWalker.getInstance().walk</strong>(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).<strong>collect</strong>(Collectors.toList()));
      }
      private List<String> walkSomeStackframes(int numberOfFrames) {
         return <strong>StackWalker.getInstance().walk</strong>(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).<strong>skip</strong>(numberOfFrames).<strong>collect</strong>(Collectors.toList()));
      }
      private List<String> walkLimitStackframes(int numberOfFrames) {
         return <strong>StackWalker.getInstance().walk</strong>(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).<strong>limit</strong>(numberOfFrames).<strong>collect</strong>(Collectors.toList())); 
      }
   }
}
Copy after login

Output

<strong>--- Walk all StackFrames ---
[
StackWalkerTest$Walker2/walkAllStackframes,
StackWalkerTest$Walker2/Method2,
StackWalkerTest$Walker2/Method1,
StackWalkerTest$Walker2/walk,
StackWalkerTest$Walker1/walk,
StackWalkerTest/walk,
StackWalkerTest/main</strong>
<strong>]
--- Skip some StackFrames ---
[
StackWalkerTest$Walker2/walk,
StackWalkerTest$Walker1/walk,
StackWalkerTest/walk,
StackWalkerTest/main</strong>
<strong>]
--- Limit StackFrames ---
[
StackWalkerTest$Walker2/walkLimitStackframes,
StackWalkerTest$Walker2/Method2,
StackWalkerTest$Walker2/Method1</strong>
<strong>]</strong>
Copy after login

The above is the detailed content of How to print different stack frames using StackWalker API in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template