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
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@895129 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e87ee0a705
commit
e539bde396
|
@ -44,7 +44,6 @@
|
|||
* @since 1.0
|
||||
* @version $Id$
|
||||
*/
|
||||
//@ThreadSafe
|
||||
public class ExceptionUtils {
|
||||
|
||||
/**
|
||||
|
@ -55,14 +54,10 @@ public class ExceptionUtils {
|
|||
*/
|
||||
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",
|
||||
|
@ -88,44 +83,6 @@ public ExceptionUtils() {
|
|||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* <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.
|
||||
|
@ -136,29 +93,15 @@ private static String[] toArray(List<String> list) {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
* <p>This may be modified and used in the overloaded getCause(Throwable, String[]) method.</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
|
||||
* @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 @@ public static boolean isCauseMethodName(String methodName) {
|
|||
* @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 @@ public static Throwable getCause(Throwable throwable, String[] methodNames) {
|
|||
}
|
||||
|
||||
if (methodNames == null) {
|
||||
synchronized(CAUSE_METHOD_NAMES_LOCK) {
|
||||
methodNames = CAUSE_METHOD_NAMES;
|
||||
}
|
||||
methodNames = CAUSE_METHOD_NAMES;
|
||||
}
|
||||
|
||||
for (int i = 0; i < methodNames.length; i++) {
|
||||
|
|
|
@ -123,30 +123,6 @@ public void testConstructor() {
|
|||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
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));
|
||||
|
|
Loading…
Reference in New Issue