Removing setCause method. JDK 1.4 provided an initCause method and this method was to support both pre and post JDK 1.4 use cases. I don't see much of a reason to keep supporting Exceptions that have setCause as well as the inherited initCause. LANG-491
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@895126 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
00a4628d4e
commit
e6e7f16e57
|
@ -150,64 +150,6 @@ public class ExceptionUtils {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Sets the cause of a <code>Throwable</code> using introspection, allowing
|
||||
* source code compatibility between pre-1.4 and post-1.4 Java releases.</p>
|
||||
*
|
||||
* <p>The typical use of this method is inside a constructor as in
|
||||
* the following example:</p>
|
||||
*
|
||||
* <pre>
|
||||
* import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
*
|
||||
* public class MyException extends Exception {
|
||||
*
|
||||
* public MyException(String msg) {
|
||||
* super(msg);
|
||||
* }
|
||||
*
|
||||
* public MyException(String msg, Throwable cause) {
|
||||
* super(msg);
|
||||
* ExceptionUtils.setCause(this, cause);
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @param target the target <code>Throwable</code>
|
||||
* @param cause the <code>Throwable</code> to set in the target
|
||||
* @return a <code>true</code> if the target has been modified
|
||||
* @since 2.2
|
||||
*/
|
||||
public static boolean setCause(Throwable target, Throwable cause) {
|
||||
if (target == null) {
|
||||
throw new NullPointerException("target must not be null.");
|
||||
}
|
||||
Object[] causeArgs = new Object[]{cause};
|
||||
boolean modifiedTarget = false;
|
||||
if (THROWABLE_INITCAUSE_METHOD != null) {
|
||||
try {
|
||||
THROWABLE_INITCAUSE_METHOD.invoke(target, causeArgs);
|
||||
modifiedTarget = true;
|
||||
} catch (IllegalAccessException ignored) {
|
||||
// Exception ignored.
|
||||
} catch (InvocationTargetException ignored) {
|
||||
// Exception ignored.
|
||||
}
|
||||
}
|
||||
try {
|
||||
Method setCauseMethod = target.getClass().getMethod("setCause", new Class[]{Throwable.class});
|
||||
setCauseMethod.invoke(target, causeArgs);
|
||||
modifiedTarget = true;
|
||||
} catch (NoSuchMethodException ignored) {
|
||||
// Exception ignored.
|
||||
} catch (IllegalAccessException ignored) {
|
||||
// Exception ignored.
|
||||
} catch (InvocationTargetException ignored) {
|
||||
// Exception ignored.
|
||||
}
|
||||
return modifiedTarget;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the given list as a <code>String[]</code>.
|
||||
* @param list a list to transform.
|
||||
|
|
|
@ -185,25 +185,6 @@ public class ExceptionUtilsTest extends TestCase {
|
|||
assertSame(((ExceptionWithCause) cyclicCause.getCause()).getCause(), ExceptionUtils.getRootCause(cyclicCause));
|
||||
}
|
||||
|
||||
public void testSetCause() {
|
||||
Exception cause = new ExceptionWithoutCause();
|
||||
assertEquals(true, ExceptionUtils.setCause(new ExceptionWithCause(null), cause));
|
||||
if (SystemUtils.isJavaVersionAtLeast(140)) {
|
||||
assertEquals(true, ExceptionUtils.setCause(new ExceptionWithoutCause(), cause));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests overriding a cause to <code>null</code>.
|
||||
*/
|
||||
public void testSetCauseToNull() {
|
||||
Exception ex = new ExceptionWithCause(new IOException());
|
||||
assertEquals(true, ExceptionUtils.setCause(ex, new IllegalStateException()));
|
||||
assertNotNull(ExceptionUtils.getCause(ex));
|
||||
assertEquals(true, ExceptionUtils.setCause(ex, null));
|
||||
assertNull(ExceptionUtils.getCause(ex));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testGetThrowableCount_Throwable() {
|
||||
assertEquals(0, ExceptionUtils.getThrowableCount(null));
|
||||
|
|
Loading…
Reference in New Issue