A standard API is provided in Java 9, using the java.lang.StackWalker class. This class is designed to be efficient, by allowing delayed access to stack frames. Additionally, there are several options to include implementation and/or reflection frames in the stack trace, which can be useful for debugging purposes. For example, we add the SHOW_REFLECT_FRAMES option when creating a StackWalker instance in order to print the frames of the reflection method.
In the following example, we can display the reflection frame of the StackFrame
Example
import java.lang.StackWalker.Option; import java.lang.StackWalker.StackFrame; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; public class ReflectionFrameTest { public static void main(String args[]) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { <strong>Method </strong>test1Method = Test1.class.<strong>getDeclaredMethod</strong>("test1", (Class<!--?-->[]) null); test1Method.<strong>invoke</strong>(null, (Object[]) null); } } class Test1 { protected static void test1() { Test2.test2(); } } class Test2 { protected static void test2() { <strong>// show reflection methods</strong> <strong>List<StackFrame></strong> stack = <strong>StackWalker.getInstance</strong>(Option.<strong>SHOW_REFLECT_FRAMES</strong>).walk((s) -> s.collect(Collectors.toList())); for(<strong>StackFrame </strong>frame : stack) { System.out.println(frame.<strong>getClassName()</strong> + " " + frame.<strong>getLineNumber()</strong> + " " + frame.<strong>getMethodName()</strong>); } } }
Output
<strong>Test2 22 test2 Test1 16 test1 jdk.internal.reflect.NativeMethodAccessorImpl -2 invoke0 jdk.internal.reflect.NativeMethodAccessorImpl 62 invoke jdk.internal.reflect.DelegatingMethodAccessorImpl 43 invoke java.lang.reflect.Method 564 invoke ReflectionFrameTest 11 main</strong>
The above is the detailed content of How to display reflection frame of StackFrame in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

This article explores methods for sharing data between Cucumber steps, comparing scenario context, global variables, argument passing, and data structures. It emphasizes best practices for maintainability, including concise context use, descriptive

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
