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

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

@@ -638,14 +638,27 @@
      * 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));
+        printStackTrace(new PrintStreamStackTracePrinter(s));
     }
 
-    private void printStackTrace(PrintStreamOrWriter 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,11 +682,11 @@
 
     /**
      * Print our stack trace as an enclosed exception for the specified
      * stack trace.
      */
-    private void printEnclosedStackTrace(PrintStreamOrWriter s,
+    private void printEnclosedStackTrace(StackTracePrinter s,
                                          StackTraceElement[] enclosingTrace,
                                          String caption,
                                          String prefix,
                                          Set<Throwable> dejaVu) {
         assert Thread.holdsLock(s.lock());

@@ -715,29 +728,29 @@
      *
      * @param s {@code PrintWriter} to use for output
      * @since   1.1
      */
     public void printStackTrace(PrintWriter s) {
-        printStackTrace(new WrappedPrintWriter(s));
+        printStackTrace(new PrintWriterStackTracePrinter(s));
     }
 
     /**
-     * Wrapper class for PrintStream and PrintWriter to enable a single
-     * implementation of printStackTrace.
+     * Wrapper class of all targets for printing the stack trace to enable a single
+     * implementation of that functionality.
      */
-    private abstract static class PrintStreamOrWriter {
-        /** Returns the object to be locked when using this StreamOrWriter */
+    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 StreamOrWriter */
+        /** Prints the specified string as a line on this StackTracePrinter */
         abstract void println(Object o);
     }
 
-    private static class WrappedPrintStream extends PrintStreamOrWriter {
+    private static class PrintStreamStackTracePrinter extends StackTracePrinter {
         private final PrintStream printStream;
 
-        WrappedPrintStream(PrintStream printStream) {
+        PrintStreamStackTracePrinter(PrintStream printStream) {
             this.printStream = printStream;
         }
 
         Object lock() {
             return printStream;

@@ -746,14 +759,14 @@
         void println(Object o) {
             printStream.println(o);
         }
     }
 
-    private static class WrappedPrintWriter extends PrintStreamOrWriter {
+    private static class PrintWriterStackTracePrinter extends StackTracePrinter {
         private final PrintWriter printWriter;
 
-        WrappedPrintWriter(PrintWriter printWriter) {
+        PrintWriterStackTracePrinter(PrintWriter printWriter) {
             this.printWriter = printWriter;
         }
 
         Object lock() {
             return printWriter;

@@ -762,10 +775,29 @@
         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.
      *