You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2016/05/24 18:16:19 UTC

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

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 <bd...@pobox.com>
Authored: Mon May 9 23:38:13 2016 -0400
Committer: pascalschumacher <pa...@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;
+    }
+  }
+}


[2/2] [lang] LANG-1228: Add changes.xml entry

Posted by pa...@apache.org.
LANG-1228: Add changes.xml entry


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

Branch: refs/heads/master
Commit: 7f1b88043371a63e0faf596c720662f66251ee23
Parents: 864721d
Author: pascalschumacher <pa...@gmx.net>
Authored: Tue May 24 20:16:10 2016 +0200
Committer: pascalschumacher <pa...@gmx.net>
Committed: Tue May 24 20:16:10 2016 +0200

----------------------------------------------------------------------
 src/changes/changes.xml | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/7f1b8804/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 56f00d9..fe3c588 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.5" date="tba" description="tba">
+    <action issue="LANG-1228" type="add" dev="pschumacher" due-to="Brad Hess">Prefer Throwable.getCause() in ExceptionUtils.getCause()</action>
     <action issue="LANG-1233" type="add" dev="pschumacher" due-to="Nick Manley">DiffBuilder add method to allow appending from a DiffResult</action>
     <action issue="LANG-1176" type="update" dev="pschumacher" due-to="Jeffery Yuan">Improve ArrayUtils removeElements time complexity to O(n)</action>
     <action issue="LANG-1234" type="update" dev="pschumacher" due-to="Jonatan J�nsson">getLevenshteinDistance with a threshold: optimize implementation if the strings lengths differ more than the threshold</action>