You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by kr...@apache.org on 2011/06/03 11:45:35 UTC

svn commit: r1130964 - /db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java

Author: kristwaa
Date: Fri Jun  3 09:45:34 2011
New Revision: 1130964

URL: http://svn.apache.org/viewvc?rev=1130964&view=rev
Log:
DERBY-5256: Improve error reporting in common.sanity.AssertFailure 

Added more specific error reporting, and fixed code that could result
in an NPE under some circumstances.

Patch file: derby-5256-1a-error_reporting.diff


Modified:
    db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java

Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java?rev=1130964&r1=1130963&r2=1130964&view=diff
==============================================================================
--- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java (original)
+++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/sanity/AssertFailure.java Fri Jun  3 09:45:34 2011
@@ -126,6 +126,20 @@ public class AssertFailure extends Runti
         s.println(threadDump);
     }
 
+    /**
+     * Tells if generating a thread dump is supported in the running JVM.
+     */
+    private boolean supportsThreadDump() {
+        try {
+            // This checks that we are on a jvm >= 1.5 where we
+            // can actually do threaddumps.
+            Thread.class.getMethod("getAllStackTraces", new Class[] {});
+            return true;
+        } catch (NoSuchMethodException nsme) {
+            // Ignore exception
+        }
+        return false;
+    }
 
     /**
      * Dumps stack traces for all the threads if the JVM supports it.
@@ -140,54 +154,57 @@ public class AssertFailure extends Runti
      */
     private String dumpThreads() {
 
+        if (!supportsThreadDump()) {
+            return "(Skipping thread dump because it is not " +
+                    "supported on JVM 1.4)";
+        }
+            
+        // NOTE: No need to flush with the StringWriter/PrintWriter combination.
         StringWriter out = new StringWriter();
         PrintWriter p = new PrintWriter(out, true);
 
-        //Try to get a thread dump and deal with various situations.
+        // Load the class and method we need with reflection.
+        final Method m;
         try {
-            //This checks that we are on a jvm >= 1.5 where we
-            //can actually do threaddumps.
-            Thread.class.getMethod("getAllStackTraces", new Class[] {});
-
-            //Then get the thread dump.
-            Class c = Class.
-            forName("org.apache.derby.shared.common.sanity.ThreadDump");
-            final Method m = c.getMethod("getStackDumpString",new Class[] {});
-
-            String dump;
+            Class c = Class.forName(
+                    "org.apache.derby.shared.common.sanity.ThreadDump");
+            m = c.getMethod("getStackDumpString", new Class[] {});
+        } catch (Exception e) {
+            p.println("Failed to load class/method required to generate " +
+                    "a thread dump:");
+            e.printStackTrace(p);
+            return out.toString();
+        }
 
-            dump = (String) AccessController.doPrivileged
+        //Try to get a thread dump and deal with various situations.
+        try {
+            String dump = (String) AccessController.doPrivileged
             (new PrivilegedExceptionAction(){
                 public Object run() throws
-                IllegalArgumentException,
-                IllegalAccessException,
-                InvocationTargetException{
-                    return m.invoke(null, null);
+                        IllegalArgumentException,
+                        IllegalAccessException,
+                        InvocationTargetException {
+                    return m.invoke(null, (Object[])null);
                 }
             }
             );
 
             //Print the dump to the message string. That went OK.
-            p.print("---------------\nStack traces for all " +
-            "live threads:");
+            p.print("---------------\nStack traces for all live threads:");
             p.println("\n" + dump);
             p.println("---------------");
-        } catch (NoSuchMethodException e) {
-            p.println("(Skipping thread dump because it is not " +
-            "supported on JVM 1.4)");
-
-        } catch (Exception e) {
-            if (e instanceof PrivilegedActionException &&
-                e.getCause() instanceof InvocationTargetException &&
-                e.getCause().getCause() instanceof AccessControlException){
+        } catch (PrivilegedActionException pae) {
+            Throwable cause = pae.getCause();
+            if (cause instanceof InvocationTargetException &&
+                cause.getCause() instanceof AccessControlException) {
 
                 p.println("(Skipping thread dump "
-                        + "because of insufficient permissions:\n"
-                        + e.getCause().getCause() + ")\n");
+                    + "because of insufficient permissions:\n"
+                    + cause.getCause() + ")\n");
             } else {
-                p.println("\nAssertFailure tried to do a thread dump, but "
-                        + "there was an error:");
-                e.getCause().printStackTrace(p);
+                p.println("\nAssertFailure tried to do a thread dump, "
+                    + "but there was an error:");
+                cause.printStackTrace(p);
             }
         }
         return out.toString();