StackWalker API在程序执行期间在堆栈跟踪中提供信息流。此API需要虚拟机捕获整个堆栈的快照,并返回元素数组以进行过滤。我们需要使用walk()方法跳过,删除和限制堆栈帧。我们还可以按类过滤堆栈帧以获取第一个匹配的帧,并使用filter()方法过滤所有匹配的帧。
在下面的示例中,我们可以使用StackWalker API过滤堆栈帧。
import java.lang.StackWalker.StackFrame; import java.util.*; import java.util.stream.*; public class StackWalkerFilterTest { public static void main(String args[]) { final List<Class> filterClasses = new ArrayList<>(); filterClasses.add(StackWalkerFilterTest.class); System.out.println("--- filter Frame by Class >> get first matching frame ---"); Optional<StackFrame> frameByClass = findFrameByClass(filterClasses); System.out.println(frameByClass.toString()); System.out.println("--- filter Frame by Class >> get all matching frames ---"); List<StackFrame> framesByClass = findAllFramesByClass(filterClasses); System.out.println(framesByClass); } private static Optional<StackFrame> findFrameByClass(List<Class> filterClasses) { return StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE) .walk(s -> s.filter(f -> filterClasses.contains(f.getDeclaringClass())).findFirst()); } private static List<StackFrame> findAllFramesByClass(List<Class> filterClasses) { return StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).walk( s -> s.filter(f -> filterClasses.contains(f.getDeclaringClass())).collect(Collectors.toList())); } }
输出结果
--- filter Frame by Class >> get first matching frame --- Optional[StackWalkerTest.findFrameByClass(StackWalkerTest.java:20)] --- filter Frame by Class >> get all matching frames --- [StackWalkerTest.findAllFramesByClass(StackWalkerTest.java:23), StackWalkerTest2.main(StackWalkerTest.java:15)]