--- old/src/share/classes/java/lang/Throwable.java 2013-09-19 23:24:06.000000000 -0700 +++ new/src/share/classes/java/lang/Throwable.java 2013-09-19 23:24:06.000000000 -0700 @@ -26,6 +26,10 @@ package java.lang; import java.io.*; import java.util.*; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Collector; /** * The {@code Throwable} class is the superclass of all errors and @@ -816,6 +820,49 @@ return getOurStackTrace().clone(); } + /** + * Performs the given action for each {@code StackFrameInfo} representing + * a stack trace element of this stack trace. + * + * @param action a consumer traversing a stream of StackFrameInfo + * + * @throws SecurityException if a security manager exists and its + * {@code checkPermission} method denies access to traverse stack. + * @throws UnsupportedOperationException if this object is not thrown by + * the running Java virtual machine. + * + * @since 1.8 + */ + public void walkStackTrace(Consumer action) { + if (backtrace == null) + throw new UnsupportedOperationException(); + + Thread.checkStackWalkPermission(); + StackStream.build(getStackTrace()).walk(action); + } + + /** + * Performs the given action for each {@linkplain StackFrameInfo stack frames} + * representing a stack trace element of this stack trace + * that match the given predicate. + * + * @param predicate a predicate to apply to the stack walk + * @param action a consumer traversing a stream of StackFrameInfo + * + * @throws SecurityException if a security manager exists and its + * {@code checkPermission} method denies access to traverse stack. + * @throws UnsupportedOperationException if this object is not thrown by + * the running Java virtual machine. + * @since 1.8 + */ + public void walkStackTrace(Predicate predicate, + Consumer action) { + if (backtrace == null) + throw new UnsupportedOperationException(); + Thread.checkStackWalkPermission(); + StackStream.build(getStackTrace()).walk(predicate, action); + } + private synchronized StackTraceElement[] getOurStackTrace() { // Initialize stack trace field with information from // backtrace if this is the first call to this method