commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pascalschumac...@apache.org
Subject [1/2] [lang] [LANG-1228] Prefer Throwable.getCause() in ExceptionUtils.getCause() (closes #139)
Date Tue, 24 May 2016 18:16:19 GMT
Repository: commons-lang
Updated Branches:
  refs/heads/master c36de7a60 -> 7f1b88043


[LANG-1228] Prefer Throwable.getCause() in ExceptionUtils.getCause() (closes #139)


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/864721d5
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/864721d5
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/864721d5

Branch: refs/heads/master
Commit: 864721d54b8b372808b098e13120abe5c22fb9b1
Parents: c36de7a
Author: Bradley Hess <bdhess@pobox.com>
Authored: Mon May 9 23:38:13 2016 -0400
Committer: pascalschumacher <pascalschumacher@gmx.net>
Committed: Tue May 24 20:13:39 2016 +0200

----------------------------------------------------------------------
 .../commons/lang3/exception/ExceptionUtils.java | 13 +++++---
 .../lang3/exception/ExceptionUtilsTest.java     | 22 +++----------
 .../lang3/test/NotVisibleExceptionFactory.java  | 34 ++++++++++++++++++++
 3 files changed, 48 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/864721d5/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
index 0a7c2cc..8d2ae78 100644
--- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
+++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
@@ -121,7 +121,7 @@ public class ExceptionUtils {
      */
     @Deprecated
     public static Throwable getCause(final Throwable throwable) {
-        return getCause(throwable, CAUSE_METHOD_NAMES);
+        return getCause(throwable, null);
     }
 
     /**
@@ -144,14 +144,19 @@ public class ExceptionUtils {
         }
 
         if (methodNames == null) {
+            final Throwable cause = throwable.getCause();
+            if (cause != null) {
+                return cause;
+            }
+
             methodNames = CAUSE_METHOD_NAMES;
         }
 
         for (final String methodName : methodNames) {
             if (methodName != null) {
-                final Throwable cause = getCauseUsingMethodName(throwable, methodName);
-                if (cause != null) {
-                    return cause;
+                final Throwable legacyCause = getCauseUsingMethodName(throwable, methodName);
+                if (legacyCause != null) {
+                    return legacyCause;
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/864721d5/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
index 97a2bfc..0b2ce48 100644
--- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
@@ -32,6 +32,7 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
 import java.util.List;
 
+import org.apache.commons.lang3.test.NotVisibleExceptionFactory;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -39,23 +40,6 @@ import org.junit.Test;
 
 /**
  * Tests {@link org.apache.commons.lang3.exception.ExceptionUtils}.
- * 
- * <h3>Notes</h3>
- * <p>
- * Make sure this exception code does not depend on Java 1.4 nested exceptions. SVN revision
38990 does not compile with
- * Java 1.3.1.
- * </p>
- * <ul>
- * <li>Compiled with Sun Java 1.3.1_15</li>
- * <li>Tested with Sun Java 1.3.1_15</li>
- * <li>Tested with Sun Java 1.4.2_12</li>
- * <li>Tested with Sun Java 1.5.0_08</li>
- * <li>All of the above on Windows XP SP2 + patches.</li>
- * </ul>
- * <p>
- * Gary Gregory; August 16, 2006.
- * </p>
- * 
  * @since 1.0
  */
 public class ExceptionUtilsTest {
@@ -65,6 +49,7 @@ public class ExceptionUtilsTest {
     private Throwable withoutCause;
     private Throwable jdkNoCause;
     private ExceptionWithCause cyclicCause;
+    private Throwable notVisibleException;
 
 
     @Before
@@ -77,6 +62,7 @@ public class ExceptionUtilsTest {
         final ExceptionWithCause b = new ExceptionWithCause(a);
         a.setCause(b);
         cyclicCause = new ExceptionWithCause(a);
+        notVisibleException = NotVisibleExceptionFactory.createException(withoutCause);
     }
 
 
@@ -87,6 +73,7 @@ public class ExceptionUtilsTest {
         withCause = null;
         jdkNoCause = null;
         cyclicCause = null;
+        notVisibleException = null;
     }
 
     //-----------------------------------------------------------------------
@@ -134,6 +121,7 @@ public class ExceptionUtilsTest {
         assertSame(cyclicCause.getCause(), ExceptionUtils.getCause(cyclicCause));
         assertSame(((ExceptionWithCause) cyclicCause.getCause()).getCause(), ExceptionUtils.getCause(cyclicCause.getCause()));
         assertSame(cyclicCause.getCause(), ExceptionUtils.getCause(((ExceptionWithCause)
cyclicCause.getCause()).getCause()));
+        assertSame(withoutCause, ExceptionUtils.getCause(notVisibleException));
     }
 
     @SuppressWarnings("deprecation") // Specifically tests the deprecated methods

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/864721d5/src/test/java/org/apache/commons/lang3/test/NotVisibleExceptionFactory.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/test/NotVisibleExceptionFactory.java b/src/test/java/org/apache/commons/lang3/test/NotVisibleExceptionFactory.java
new file mode 100644
index 0000000..70c1289
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/test/NotVisibleExceptionFactory.java
@@ -0,0 +1,34 @@
+package org.apache.commons.lang3.test;
+
+/**
+ * Allows for testing an exception that is not visible to
+ * {@link org.apache.commons.lang3.exception.ExceptionUtils}
+ */
+public class NotVisibleExceptionFactory {
+
+  private NotVisibleExceptionFactory() {}
+
+  /**
+   * Create a new Exception whose getCause method returns the
+   * provided cause.
+   * @param cause the cause of the exception
+   * @return a new {@link Exception}
+   */
+  public static Exception createException(final Throwable cause) {
+    return new NotVisibleException(cause);
+  }
+
+  private static class NotVisibleException extends Exception {
+
+    private final Throwable cause;
+
+    private NotVisibleException(Throwable cause) {
+      this.cause = cause;
+    }
+
+    @Override
+    public Throwable getCause() {
+      return cause;
+    }
+  }
+}


Mime
View raw message