gpt4 book ai didi

java - StackWalker.Option SHOW_HIDDEN_FRAMES 在 Java 9 中如何工作

转载 作者:搜寻专家 更新时间:2023-11-01 02:20:32 27 4
gpt4 key购买 nike

我一直在探索 jdk9 中引入的 StackWalking API。使用或不使用选项 StackWalker.Option.SHOW_HIDDEN_FRAMES,我看不出输出有任何差异。

根据 API documentation :

A Java Virtual Machine implementation may hide implementation specific frames in addition to reflection frames. A StackWalker with this SHOW_HIDDEN_FRAMES option will show all hidden frames (including reflection frames)

谁能帮我理解 JVM 除了反射帧之外还能隐藏哪些帧?这个选项的具体用途是什么?

这是我的演示代码:

class DemoClass {

public static void a() {
b();
}

static void b() {
c();
}

static void c() {
d();
}

static void d() {
StackWalker sw = StackWalker.getInstance(Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE, StackWalker.Option.SHOW_HIDDEN_FRAMES));
sw.forEach(System.out::println);
}
}

public class StackWalkerAPI {

public static void main(String[] args) {
try {
Method methodA = Class.forName("DemoClass").getMethod("a");
methodA.invoke(null, (Object[]) null);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(StackWalkerAPI.class.getName()).log(Level.SEVERE, null, ex);
}

}
}

最佳答案

不确定不能在 Java 9 中工作是什么意思。在这里,使用 this sample code , 详细解释与您类似的代码的输出:

StackWalker.Option.RETAIN_CLASS_REFERENCE displays the Class object in StackFrames walked by this StackWalker.

internal/experiment.StackWalkerDemo.lambda$d$0(StackWalkerDemo.java:29)
java.base/java.lang.Iterable.forEach(Iterable.java:75)
internal/experiment.StackWalkerDemo.d(StackWalkerDemo.java:28)
internal/experiment.StackWalkerDemo.c(StackWalkerDemo.java:20)
internal/experiment.StackWalkerDemo.b(StackWalkerDemo.java:16)
internal/experiment.StackWalkerDemo.a(StackWalkerDemo.java:12)
internal/experiment.StackWalkerAPI.main(StackWalkerAPI.java:15)

StackWalker.Option.SHOW_REFLECT_FRAMES Shows all reflection frames

internal/experiment.StackWalkerDemo.lambda$d$0(StackWalkerDemo.java:29)
java.base/java.lang.Iterable.forEach(Iterable.java:75)
internal/experiment.StackWalkerDemo.d(StackWalkerDemo.java:28)
internal/experiment.StackWalkerDemo.c(StackWalkerDemo.java:20)
internal/experiment.StackWalkerDemo.b(StackWalkerDemo.java:16)
internal/experiment.StackWalkerDemo.a(StackWalkerDemo.java:12)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:564)
internal/experiment.StackWalkerAPI.main(StackWalkerAPI.java:15)

注意这里的声明 java.base/java.lang.reflect.Method.invoke(Method.java:564),正如文档中提到的那样选项显示所有反射帧,包括 Method.invoke(o1,o2)Constructor.newInstance(o)


StackWalker.Option.SHOW_HIDDEN_FRAMES Shows all hidden frames.

internal/experiment.StackWalkerDemo.lambda$d$0(StackWalkerDemo.java:29)
internal/experiment.StackWalkerDemo$$Lambda$23/1358444045.accept(Unknown Source)
java.base/java.lang.Iterable.forEach(Iterable.java:75)
internal/experiment.StackWalkerDemo.d(StackWalkerDemo.java:28)
internal/experiment.StackWalkerDemo.c(StackWalkerDemo.java:20)
internal/experiment.StackWalkerDemo.b(StackWalkerDemo.java:16)
internal/experiment.StackWalkerDemo.a(StackWalkerDemo.java:12)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:564)
internal/experiment.StackWalkerAPI.main(StackWalkerAPI.java:15)

寻找 internal/experiment.StackWalkerDemo$$Lambda$23/1358444045.accept(Unknown Source),这是一个用于对 lambda(实现特定的框架),并且可以使用此选项访问。

关于java - StackWalker.Option SHOW_HIDDEN_FRAMES 在 Java 9 中如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45637205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com