[update] jdk StackWalker使用修正

This commit is contained in:
maxf 2025-08-11 09:29:08 +08:00
parent a4be814442
commit d9a548ba44
4 changed files with 126 additions and 3 deletions

View File

@ -15,6 +15,15 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

View File

@ -1,6 +1,7 @@
package top.yexuejc.demo; package top.yexuejc.demo;
import java.lang.StackWalker; import java.lang.StackWalker;
import java.util.stream.Collectors;
public class StackWalkerDemo { public class StackWalkerDemo {
public static void main(String[] args) { public static void main(String[] args) {
@ -22,11 +23,17 @@ public class StackWalkerDemo {
.walk(stackFrameStream -> .walk(stackFrameStream ->
stackFrameStream stackFrameStream
.filter(frame -> frame.getClassName().contains("StackWalkerDemo")) .filter(frame -> frame.getClassName().contains("StackWalkerDemo"))
.peek(frame -> System.out.println(frame.getClassName() + "." + frame.getMethodName())) .map(frame -> frame.getClassName() + "." + frame.getMethodName())
); .collect(Collectors.toList())
).forEach(System.out::println);
System.out.println("\n=== 调用者信息 ==="); System.out.println("\n=== 调用者信息 ===");
// 获取直接调用者信息 // 获取直接调用者信息
StackWalker.getInstance().walk(stackStream -> stackStream.skip(1).findFirst()).ifPresent(caller -> System.out.println("调用者: " + caller.getClassName() + "." + caller.getMethodName())); StackWalker.getInstance()
.walk(stackStream ->
stackStream.skip(1).findFirst()
).ifPresent(caller ->
System.out.println("调用者: " + caller.getClassName() + "." + caller.getMethodName())
);
} }
} }

View File

@ -0,0 +1,16 @@
package top.yexuejc.demo;
import org.junit.jupiter.api.Test;
/**
* @author maxiaofeng
* @date 2025/8/11 9:20
*/
class StackWalkerDemoTest {
@Test
void methodA() {
StackWalkerDemo.methodA();
}
}

91
demo3/pom.xml Normal file
View File

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>top.yexuejc</groupId>
<artifactId>demo3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo3</name>
<description>demo3</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- 项目热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 表示依赖不会传递 -->
<scope>true</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>