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 = StackWalker.getInstance(Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE, StackWalker.Option.SHOW_HIDDEN_FRAMES), 16);
Stream stackStream = StackWalker.getInstance().walk(f -> f);
System.out.println(“— Walk all StackFrames —“);
List stacks = walkAllStackframes();
System.out.println(stacks);
System.out.println(“— Skip some StackFrames —“);
List stacksAfterSkip = walkSomeStackframes(3);
System.out.println(stacksAfterSkip);
System.out.println(“— Limit StackFrames —“);
List stacksByLimit = walkLimitStackframes(3);
System.out.println(stacksByLimit);
}
private List walkAllStackframes() {
return StackWalker.getInstance().walk(s -> s.map(frame -> “n” + frame.getClassName() + “/” + frame.getMethodName()).collect(Collectors.toList()));
}
private List walkSomeStackframes(int numberOfFrames) {
return StackWalker.getInstance().walk(s -> s.map(frame -> “n” + frame.getClassName() + “/” + frame.getMethodName()).skip(numberOfFrames).collect(Collectors.toList()));
}
private List walkLimitStackframes(int numberOfFrames) {
return StackWalker.getInstance().walk(s -> s.map(frame -> “n” + frame.getClassName() + “/” + frame.getMethodName()).limit(numberOfFrames).collect(Collectors.toList()));
}
}
}
— Walk all StackFrames —
[
StackWalkerTest$Walker2/walkAllStackframes,
StackWalkerTest$Walker2/Method2,
StackWalkerTest$Walker2/Method1,
StackWalkerTest$Walker2/walk,
StackWalkerTest$Walker1/walk,
StackWalkerTest/walk,
StackWalkerTest/main
]
— Skip some StackFrames —
[
StackWalkerTest$Walker2/walk,
StackWalkerTest$Walker1/walk,
StackWalkerTest/walk,
StackWalkerTest/main
]
— Limit StackFrames —
[
StackWalkerTest$Walker2/walkLimitStackframes,
StackWalkerTest$Walker2/Method2,
StackWalkerTest$Walker2/Method1
]
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!
添加我为好友,拉您入交流群!
请使用微信扫一扫!