src/java.base/share/classes/java/lang/Throwable.java

Print this page
rev 11099 : 5050783: Added getStackTraceString method to Throwable class

*** 638,651 **** * Prints this throwable and its backtrace to the specified print stream. * * @param s {@code PrintStream} to use for output */ public void printStackTrace(PrintStream s) { ! printStackTrace(new WrappedPrintStream(s)); } ! private void printStackTrace(PrintStreamOrWriter s) { // Guard against malicious overrides of Throwable.equals by // using a Set with identity equality semantics. Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<>()); dejaVu.add(this); --- 638,664 ---- * Prints this throwable and its backtrace to the specified print stream. * * @param s {@code PrintStream} to use for output */ public void printStackTrace(PrintStream s) { ! printStackTrace(new PrintStreamStackTracePrinter(s)); } ! /** ! * Returns string representation of this throwable and its backtrace. ! * ! * @return string representation of this {@code Throwable} and its backtrace ! * @since 1.9 ! */ ! public String getStackTraceString() { ! final StringBuilder sb = new StringBuilder(); ! printStackTrace(new StringBuilderStackTracePrinter(sb)); ! ! return sb.toString(); ! } ! ! private void printStackTrace(StackTracePrinter s) { // Guard against malicious overrides of Throwable.equals by // using a Set with identity equality semantics. Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<>()); dejaVu.add(this);
*** 669,679 **** /** * Print our stack trace as an enclosed exception for the specified * stack trace. */ ! private void printEnclosedStackTrace(PrintStreamOrWriter s, StackTraceElement[] enclosingTrace, String caption, String prefix, Set<Throwable> dejaVu) { assert Thread.holdsLock(s.lock()); --- 682,692 ---- /** * Print our stack trace as an enclosed exception for the specified * stack trace. */ ! private void printEnclosedStackTrace(StackTracePrinter s, StackTraceElement[] enclosingTrace, String caption, String prefix, Set<Throwable> dejaVu) { assert Thread.holdsLock(s.lock());
*** 715,743 **** * * @param s {@code PrintWriter} to use for output * @since 1.1 */ public void printStackTrace(PrintWriter s) { ! printStackTrace(new WrappedPrintWriter(s)); } /** ! * Wrapper class for PrintStream and PrintWriter to enable a single ! * implementation of printStackTrace. */ ! private abstract static class PrintStreamOrWriter { ! /** Returns the object to be locked when using this StreamOrWriter */ abstract Object lock(); ! /** Prints the specified string as a line on this StreamOrWriter */ abstract void println(Object o); } ! private static class WrappedPrintStream extends PrintStreamOrWriter { private final PrintStream printStream; ! WrappedPrintStream(PrintStream printStream) { this.printStream = printStream; } Object lock() { return printStream; --- 728,756 ---- * * @param s {@code PrintWriter} to use for output * @since 1.1 */ public void printStackTrace(PrintWriter s) { ! printStackTrace(new PrintWriterStackTracePrinter(s)); } /** ! * Wrapper class of all targets for printing the stack trace to enable a single ! * implementation of that functionality. */ ! private abstract static class StackTracePrinter { ! /** Returns the object to be locked when using this StackTracePrinter */ abstract Object lock(); ! /** Prints the specified string as a line on this StackTracePrinter */ abstract void println(Object o); } ! private static class PrintStreamStackTracePrinter extends StackTracePrinter { private final PrintStream printStream; ! PrintStreamStackTracePrinter(PrintStream printStream) { this.printStream = printStream; } Object lock() { return printStream;
*** 746,759 **** void println(Object o) { printStream.println(o); } } ! private static class WrappedPrintWriter extends PrintStreamOrWriter { private final PrintWriter printWriter; ! WrappedPrintWriter(PrintWriter printWriter) { this.printWriter = printWriter; } Object lock() { return printWriter; --- 759,772 ---- void println(Object o) { printStream.println(o); } } ! private static class PrintWriterStackTracePrinter extends StackTracePrinter { private final PrintWriter printWriter; ! PrintWriterStackTracePrinter(PrintWriter printWriter) { this.printWriter = printWriter; } Object lock() { return printWriter;
*** 762,771 **** --- 775,803 ---- void println(Object o) { printWriter.println(o); } } + private static class StringBuilderStackTracePrinter extends StackTracePrinter { + + private final StringBuilder builder; + + StringBuilderStackTracePrinter(StringBuilder builder) { + this.builder = builder; + } + + @Override + Object lock() { + return builder; + } + + @Override + void println(Object o) { + builder.append(o).append(System.lineSeparator()); + } + } + /** * Fills in the execution stack trace. This method records within this * {@code Throwable} object information about the current state of * the stack frames for the current thread. *