This patch addresses issue #466 to enable get/setStackTrace on java.lang.Throwable.  It does not add any support for GWT filling in stack traces in web mode.

Patch by: sandymac (+tweaks by me)
Review by: ecc


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@453 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/lang/Throwable.java b/user/super/com/google/gwt/emul/java/lang/Throwable.java
index 364c59d..215b1f1 100644
--- a/user/super/com/google/gwt/emul/java/lang/Throwable.java
+++ b/user/super/com/google/gwt/emul/java/lang/Throwable.java
@@ -17,6 +17,8 @@
 
 import com.google.gwt.core.client.GWT;
 
+import java.io.PrintStream;
+
 /**
  * See <a
  * href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Throwable.html">the
@@ -28,6 +30,7 @@
 
   private Throwable cause;
   private String message;
+  private StackTraceElement[] stackTrace = NO_STACK_TRACE;
 
   public Throwable() {
   }
@@ -47,7 +50,7 @@
   }
 
   /**
-   * Stack traces are not currently supported.
+   * Stack traces are not currently populated by GWT. This method does nothing.
    * 
    * @return this
    */
@@ -68,12 +71,14 @@
   }
 
   /**
-   * Stack traces are not currently supported.
+   * Stack traces are not currently populated by GWT. This method will return a
+   * zero-length array unless a stack trace has been explicitly set with
+   * {@link #setStackTrace(StackTraceElement[])}
    * 
-   * @return always a zero-length array
+   * @return the current stack trace
    */
   public StackTraceElement[] getStackTrace() {
-    return NO_STACK_TRACE;
+    return stackTrace;
   }
 
   public Throwable initCause(Throwable cause) {
@@ -88,6 +93,10 @@
   }
 
   public void printStackTrace() {
+    printStackTrace(System.err);
+  }
+
+  public void printStackTrace(PrintStream out) {
     StringBuffer msg = new StringBuffer();
     Throwable currentCause = this;
     while (currentCause != null) {
@@ -101,13 +110,18 @@
       msg.append("\n");
       currentCause = currentCause.getCause();
     }
-    System.err.println(msg);
+    out.println(msg);
   }
 
-  /**
-   * Stack traces are not currently supported.
-   */
   public void setStackTrace(StackTraceElement[] stackTrace) {
+    StackTraceElement[] copy = new StackTraceElement[stackTrace.length];
+    for (int i = 0, c = stackTrace.length; i < c; ++i) {
+      if (stackTrace[i] == null) {
+        throw new NullPointerException();
+      }
+      copy[i] = stackTrace[i];
+    }
+    this.stackTrace = copy;
   }
 
   public String toString() {