]> source.dussan.org Git - aspectj.git/commitdiff
47952 fix: SoftException.printStackTrace(..) prints wrapped throwable (if not >=...
authorwisberg <wisberg>
Thu, 15 Jan 2004 05:21:33 +0000 (05:21 +0000)
committerwisberg <wisberg>
Thu, 15 Jan 2004 05:21:33 +0000 (05:21 +0000)
tested in 1.1, 1.3, 1.4 (but not in 1.1 release tests)

runtime/src/org/aspectj/lang/SoftException.java
runtime/testsrc/RuntimeModuleTests.java

index 406eb5272f3905f528c9d321d311f74164df2340..b1ac84cb4d3aa3d0e47dabbd7fd68bec87c6d0f7 100644 (file)
@@ -1,6 +1,7 @@
 /* *******************************************************************
  * Copyright (c) 1999-2001 Xerox Corporation, 
- *               2002 Palo Alto Research Center, Incorporated (PARC).
+ *               2002 Palo Alto Research Center, Incorporated (PARC),
+ *               2004 Contributors.
  * All rights reserved. 
  * This program and the accompanying materials are made available 
  * under the terms of the Common Public License v1.0 
@@ -14,6 +15,9 @@
 
 package org.aspectj.lang;
 
+import java.io.PrintStream;
+import java.io.PrintWriter;
+
 /** 
  * Wrapper for checked exceptions matched by a 'declare soft'.
  * You can soften checked exceptions at join points by using
@@ -21,10 +25,29 @@ package org.aspectj.lang;
  * At the join points, any exceptions thrown which match
  * TypePattern will be wrapped in <code>SoftException</code>
  * and rethrown. You can get the original exception using
- * <code>getWrappedThrowable()</code>.
+ * <code>getWrappedThrowable()</code> or
+ * <code>getCause()</code>.
  */
 public class SoftException extends RuntimeException {
-    Throwable inner;
+
+    private static final boolean HAVE_JAVA_14;
+
+    static {
+        boolean java14 = false;
+        try {
+            Class.forName("java.nio.Buffer");
+            java14 = true;
+        } catch (Throwable t) {
+            // still false;
+        }
+        HAVE_JAVA_14 = java14;
+    }
+
+    // shouldn't field be private final, constructor default or private? 
+    // but either would be a binary incompatible change.
+
+    Throwable inner; 
+
     public SoftException(Throwable inner) {
         super();
         this.inner = inner;
@@ -33,4 +56,25 @@ public class SoftException extends RuntimeException {
     public Throwable getWrappedThrowable() { return inner; }
     public Throwable getCause() { return inner; }
     
+    public void printStackTrace() {
+        printStackTrace(System.err);                
+    }
+    
+    public void printStackTrace(PrintStream stream) {
+        super.printStackTrace(stream);
+        final Throwable _inner = this.inner;
+        if (!HAVE_JAVA_14 && (null != _inner)) {
+            stream.print("Caused by: ");
+            _inner.printStackTrace(stream);
+        }
+    }
+    
+    public void printStackTrace(PrintWriter stream) {
+        super.printStackTrace(stream);
+        final Throwable _inner = this.inner;
+        if (!HAVE_JAVA_14 && (null != _inner)) {
+            stream.print("Caused by: ");
+            _inner.printStackTrace(stream);
+        }
+    }
 }
index a426025c5bf4a5ce475c12135622d6dd4c60f390..ff7560c8e6e762d44b5d74e374a01ca764189bcf 100644 (file)
  *     Xerox/PARC     initial implementation 
  * ******************************************************************/
 
-
 // default package
 
-import org.aspectj.lang.NoAspectBoundException;
+import java.io.*;
+
+import org.aspectj.lang.*;
 
 import junit.framework.*;
 
@@ -27,12 +28,52 @@ public class RuntimeModuleTests extends TestCase {
     }
 
     public RuntimeModuleTests(String name) { super(name); }
-
-    public void testNothing() {}
     
     public void testNoAspectBoundException() {
-       RuntimeException fun = new RuntimeException("fun");
-       NoAspectBoundException nab = new NoAspectBoundException("Foo", fun);
-       assertEquals(fun,nab.getCause());
+        RuntimeException fun = new RuntimeException("fun");
+        NoAspectBoundException nab = new NoAspectBoundException("Foo", fun);
+        assertEquals(fun,nab.getCause());
+    }
+
+    public void testSoftExceptionPrintStackTrace() {
+        // let's see
+//        Throwable t = new Error("xyz");       
+//        new SoftException(t).printStackTrace();
+
+        // save to specified PrintStream
+        ByteArrayOutputStream sink = new ByteArrayOutputStream();
+        PrintStream out = new PrintStream(sink);
+        new SoftException(new Error("xyz")).printStackTrace(out);
+        String s = new String(sink.toByteArray());
+        out.flush();
+        checkSoftExceptionString(s);
+
+        // save to specified PrintWriter
+        sink = new ByteArrayOutputStream();
+        PrintWriter pout = new PrintWriter(sink);
+        new SoftException(new Error("xyz")).printStackTrace(pout);
+        pout.flush();
+        s = new String(sink.toByteArray());
+        checkSoftExceptionString(s);
+
+        // check System.err redirect
+        PrintStream systemErr = System.err;
+        try {
+            sink = new ByteArrayOutputStream();
+            out = new PrintStream(sink);
+            System.setErr(out);
+            new SoftException(new Error("xyz")).printStackTrace();
+            out.flush();
+            s = new String(sink.toByteArray());
+            checkSoftExceptionString(s);
+        } finally {
+            System.setErr(systemErr);
+        }
+    }
+    static void checkSoftExceptionString(String s) {        
+        assertTrue(-1 != s.indexOf("SoftException"));
+        assertTrue(-1 != s.indexOf("Caused by: java.lang.Error"));
+        assertTrue(-1 != s.indexOf("xyz"));
+        assertTrue(-1 != s.indexOf("testSoftExceptionPrintStackTrace"));
     }
 }