You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2010/01/02 06:58:58 UTC

svn commit: r895129 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang3/exception/ExceptionUtils.java test/org/apache/commons/lang3/exception/ExceptionUtilsTest.java

Author: bayard
Date: Sat Jan  2 05:58:57 2010
New Revision: 895129

URL: http://svn.apache.org/viewvc?rev=895129&view=rev
Log:
Removed the ability to modify the static store of cause method names. If that feature is wanted, it's easy for the user to pass in their own list, or use the newly added getDefaultCauseMethodNames and modify that before calling. This removes the need for synchronization code. LANG-491

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang3/exception/ExceptionUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang3/exception/ExceptionUtilsTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang3/exception/ExceptionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang3/exception/ExceptionUtils.java?rev=895129&r1=895128&r2=895129&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang3/exception/ExceptionUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang3/exception/ExceptionUtils.java Sat Jan  2 05:58:57 2010
@@ -44,7 +44,6 @@
  * @since 1.0
  * @version $Id$
  */
-//@ThreadSafe
 public class ExceptionUtils {
     
     /**
@@ -55,14 +54,10 @@
      */
     static final String WRAPPED_MARKER = " [wrapped] ";
 
-    // Lock object for CAUSE_METHOD_NAMES
-    private static final Object CAUSE_METHOD_NAMES_LOCK = new Object();
-    
     /**
      * <p>The names of methods commonly used to access a wrapped exception.</p>
      */
-//    @GuardedBy("CAUSE_METHOD_NAMES_LOCK")
-    private static String[] CAUSE_METHOD_NAMES = {
+    private static final String[] CAUSE_METHOD_NAMES = {
         "getCause",
         "getNextException",
         "getTargetException",
@@ -89,44 +84,6 @@
 
     //-----------------------------------------------------------------------
     /**
-     * <p>Adds to the list of method names used in the search for <code>Throwable</code>
-     * objects.</p>
-     * 
-     * @param methodName  the methodName to add to the list, <code>null</code>
-     *  and empty strings are ignored
-     * @since 2.0
-     */
-    public static void addCauseMethodName(String methodName) {
-        if (StringUtils.isNotEmpty(methodName) && !isCauseMethodName(methodName)) {            
-            List<String> list = getCauseMethodNameList();
-            if (list.add(methodName)) {
-                synchronized(CAUSE_METHOD_NAMES_LOCK) {
-                    CAUSE_METHOD_NAMES = toArray(list);
-                }
-            }
-        }
-    }
-
-    /**
-     * <p>Removes from the list of method names used in the search for <code>Throwable</code>
-     * objects.</p>
-     * 
-     * @param methodName  the methodName to remove from the list, <code>null</code>
-     *  and empty strings are ignored
-     * @since 2.1
-     */
-    public static void removeCauseMethodName(String methodName) {
-        if (StringUtils.isNotEmpty(methodName)) {
-            List<String> list = getCauseMethodNameList();
-            if (list.remove(methodName)) {
-                synchronized(CAUSE_METHOD_NAMES_LOCK) {
-                    CAUSE_METHOD_NAMES = toArray(list);
-                }
-            }
-        }
-    }
-
-    /**
      * Returns the given list as a <code>String[]</code>.
      * @param list a list to transform.
      * @return the given list as a <code>String[]</code>.
@@ -136,29 +93,15 @@
     }
 
     /**
-     * Returns {@link #CAUSE_METHOD_NAMES} as a List.
+     * <p>Returns the default names used when searching for the cause of an exception.</p>
      *
-     * @return {@link #CAUSE_METHOD_NAMES} as a List.
-     */
-    private static ArrayList<String> getCauseMethodNameList() {
-        synchronized(CAUSE_METHOD_NAMES_LOCK) {
-            return new ArrayList<String>(Arrays.asList(CAUSE_METHOD_NAMES));
-        }
-    }
-
-    /**
-     * <p>Tests if the list of method names used in the search for <code>Throwable</code>
-     * objects include the given name.</p>
-     * 
-     * @param methodName  the methodName to search in the list.
-     * @return if the list of method names used in the search for <code>Throwable</code>
-     *  objects include the given name.
-     * @since 2.1
+     * <p>This may be modified and used in the overloaded getCause(Throwable, String[]) method.</p>
+     *
+     * @return cloned array of the default method names
+     * @since 3.0
      */
-    public static boolean isCauseMethodName(String methodName) {
-        synchronized(CAUSE_METHOD_NAMES_LOCK) {
-            return ArrayUtils.indexOf(CAUSE_METHOD_NAMES, methodName) >= 0;
-        }
+    public static String[] getDefaultCauseMethodNames() {
+        return ArrayUtils.clone(CAUSE_METHOD_NAMES);
     }
 
     //-----------------------------------------------------------------------
@@ -193,9 +136,7 @@
      * @since 1.0
      */
     public static Throwable getCause(Throwable throwable) {
-        synchronized(CAUSE_METHOD_NAMES_LOCK) {
-            return getCause(throwable, CAUSE_METHOD_NAMES);
-        }
+        return getCause(throwable, CAUSE_METHOD_NAMES);
     }
 
     /**
@@ -222,9 +163,7 @@
         }
 
         if (methodNames == null) {
-            synchronized(CAUSE_METHOD_NAMES_LOCK) {
-                methodNames = CAUSE_METHOD_NAMES;
-            }
+            methodNames = CAUSE_METHOD_NAMES;
         }
 
         for (int i = 0; i < methodNames.length; i++) {

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang3/exception/ExceptionUtilsTest.java?rev=895129&r1=895128&r2=895129&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang3/exception/ExceptionUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang3/exception/ExceptionUtilsTest.java Sat Jan  2 05:58:57 2010
@@ -123,30 +123,6 @@
     }
     
     //-----------------------------------------------------------------------
-    
-    public void testCauseMethodNameOps() {
-        this.testCauseMethodNameOps(null);
-        this.testCauseMethodNameOps("");
-        this.testCauseMethodNameOps(" ");
-        this.testCauseMethodNameOps("\t\r\n\t");
-        this.testCauseMethodNameOps("testMethodName");
-    }
-    
-    void testCauseMethodNameOps(String name) {
-        String methodName = "testMethodName";
-        try {
-            Assert.assertFalse(ExceptionUtils.isCauseMethodName(methodName));
-            ExceptionUtils.addCauseMethodName(methodName);            
-            ExceptionUtils.addCauseMethodName(methodName);            
-            Assert.assertTrue(ExceptionUtils.isCauseMethodName(methodName));
-        } finally {
-            ExceptionUtils.removeCauseMethodName(methodName);
-            Assert.assertFalse(
-                    "The method name " + methodName + " should not be in the array", 
-                    ExceptionUtils.isCauseMethodName(methodName));
-        }
-    }
-    
     public void testGetCause_Throwable() {
         assertSame(null, ExceptionUtils.getCause(null));
         assertSame(null, ExceptionUtils.getCause(withoutCause));