diff --git a/src/java/org/apache/commons/lang/exception/ExceptionUtils.java b/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
index 295642843..5cb56adcd 100644
--- a/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
+++ b/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
@@ -42,7 +42,7 @@ import org.apache.commons.lang.SystemUtils;
* @author Gary Gregory
* @author Pete Gieser
* @since 1.0
- * @version $Id: ExceptionUtils.java,v 1.41 2004/09/30 07:03:25 bayard Exp $
+ * @version $Id: ExceptionUtils.java,v 1.42 2004/10/09 10:04:04 scolebourne Exp $
*/
public class ExceptionUtils {
@@ -391,24 +391,28 @@ public class ExceptionUtils {
//-----------------------------------------------------------------------
/**
*
Returns the (zero based) index of the first Throwable
- * that matches the specified type in the exception chain.
+ * that matches the specified class (exactly) in the exception chain.
+ * Subclasses of the specified class do not match - see
+ * {@link #indexOfType(Throwable, Class)} for the opposite.
*
* A null
throwable returns -1
.
* A null
type returns -1
.
* No match in the chain returns -1
.
*
* @param throwable the throwable to inspect, may be null
- * @param type the type to search for
+ * @param clazz the class to search for, subclasses do not match, null returns -1
* @return the index into the throwable chain, -1 if no match or null input
*/
- public static int indexOfThrowable(Throwable throwable, Class type) {
- return indexOfThrowable(throwable, type, 0);
+ public static int indexOfThrowable(Throwable throwable, Class clazz) {
+ return indexOf(throwable, clazz, 0, false);
}
/**
* Returns the (zero based) index of the first Throwable
* that matches the specified type in the exception chain from
- * a specified index.
+ * a specified index.
+ * Subclasses of the specified class do not match - see
+ * {@link #indexOfType(Throwable, Class, int)} for the opposite.
*
* A null
throwable returns -1
.
* A null
type returns -1
.
@@ -417,13 +421,61 @@ public class ExceptionUtils {
* A start index greater than the number of throwables returns -1
.
*
* @param throwable the throwable to inspect, may be null
- * @param type the type to search for
+ * @param clazz the class to search for, subclasses do not match, null returns -1
* @param fromIndex the (zero based) index of the starting position,
* negative treated as zero, larger than chain size returns -1
* @return the index into the throwable chain, -1 if no match or null input
*/
- public static int indexOfThrowable(Throwable throwable, Class type, int fromIndex) {
- if (throwable == null) {
+ public static int indexOfThrowable(Throwable throwable, Class clazz, int fromIndex) {
+ return indexOf(throwable, clazz, fromIndex, false);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns the (zero based) index of the first Throwable
+ * that matches the specified class or subclass in the exception chain.
+ * Subclasses of the specified class do match - see
+ * {@link #indexOfThrowable(Throwable, Class)} for the opposite.
+ *
+ * A null
throwable returns -1
.
+ * A null
type returns -1
.
+ * No match in the chain returns -1
.
+ *
+ * @param throwable the throwable to inspect, may be null
+ * @param type the type to search for, subclasses match, null returns -1
+ * @return the index into the throwable chain, -1 if no match or null input
+ * @since 2.1
+ */
+ public static int indexOfType(Throwable throwable, Class type) {
+ return indexOf(throwable, type, 0, true);
+ }
+
+ /**
+ * Returns the (zero based) index of the first Throwable
+ * that matches the specified type in the exception chain from
+ * a specified index.
+ * Subclasses of the specified class do match - see
+ * {@link #indexOfThrowable(Throwable, Class)} for the opposite.
+ *
+ * A null
throwable returns -1
.
+ * A null
type returns -1
.
+ * No match in the chain returns -1
.
+ * A negative start index is treated as zero.
+ * A start index greater than the number of throwables returns -1
.
+ *
+ * @param throwable the throwable to inspect, may be null
+ * @param type the type to search for, subclasses match, null returns -1
+ * @param fromIndex the (zero based) index of the starting position,
+ * negative treated as zero, larger than chain size returns -1
+ * @return the index into the throwable chain, -1 if no match or null input
+ * @since 2.1
+ */
+ public static int indexOfType(Throwable throwable, Class type, int fromIndex) {
+ return indexOf(throwable, type, fromIndex, true);
+ }
+
+ private static int indexOf(Throwable throwable, Class type, int fromIndex, boolean subclass) {
+ if (throwable == null || type == null) {
return -1;
}
if (fromIndex < 0) {
@@ -433,11 +485,17 @@ public class ExceptionUtils {
if (fromIndex >= throwables.length) {
return -1;
}
- for (int i = fromIndex; i < throwables.length; i++) {
-// TODO: decide on whether to include this
-// if (type.isAssignableFrom(throwables[i].getClass())) {
- if (throwables[i].getClass().equals(type)) {
- return i;
+ if (subclass) {
+ for (int i = fromIndex; i < throwables.length; i++) {
+ if (type.isAssignableFrom(throwables[i].getClass())) {
+ return i;
+ }
+ }
+ } else {
+ for (int i = fromIndex; i < throwables.length; i++) {
+ if (type.equals(throwables[i].getClass())) {
+ return i;
+ }
}
}
return -1;
diff --git a/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java b/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java
index 5f39019ef..97fe9fda5 100644
--- a/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java
+++ b/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java
@@ -179,6 +179,8 @@ public class ExceptionUtilsTestCase extends junit.framework.TestCase {
assertEquals(0, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class));
assertEquals(1, ExceptionUtils.indexOfThrowable(withCause, NestableException.class));
assertEquals(2, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithoutCause.class));
+
+ assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, Exception.class));
}
public void testIndexOf_ThrowableClassInt() {
@@ -204,6 +206,58 @@ public class ExceptionUtilsTestCase extends junit.framework.TestCase {
assertEquals(0, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 0));
assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 1));
assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 9));
+
+ assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, Exception.class, 0));
+ }
+
+ //-----------------------------------------------------------------------
+ public void testIndexOfType_ThrowableClass() {
+ assertEquals(-1, ExceptionUtils.indexOfType(null, null));
+ assertEquals(-1, ExceptionUtils.indexOfType(null, NestableException.class));
+
+ assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, null));
+ assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, ExceptionWithCause.class));
+ assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, NestableException.class));
+ assertEquals(0, ExceptionUtils.indexOfType(withoutCause, ExceptionWithoutCause.class));
+
+ assertEquals(-1, ExceptionUtils.indexOfType(nested, null));
+ assertEquals(-1, ExceptionUtils.indexOfType(nested, ExceptionWithCause.class));
+ assertEquals(0, ExceptionUtils.indexOfType(nested, NestableException.class));
+ assertEquals(1, ExceptionUtils.indexOfType(nested, ExceptionWithoutCause.class));
+
+ assertEquals(-1, ExceptionUtils.indexOfType(withCause, null));
+ assertEquals(0, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class));
+ assertEquals(1, ExceptionUtils.indexOfType(withCause, NestableException.class));
+ assertEquals(2, ExceptionUtils.indexOfType(withCause, ExceptionWithoutCause.class));
+
+ assertEquals(0, ExceptionUtils.indexOfType(withCause, Exception.class));
+ }
+
+ public void testIndexOfType_ThrowableClassInt() {
+ assertEquals(-1, ExceptionUtils.indexOfType(null, null, 0));
+ assertEquals(-1, ExceptionUtils.indexOfType(null, NestableException.class, 0));
+
+ assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, null));
+ assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, ExceptionWithCause.class, 0));
+ assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, NestableException.class, 0));
+ assertEquals(0, ExceptionUtils.indexOfType(withoutCause, ExceptionWithoutCause.class, 0));
+
+ assertEquals(-1, ExceptionUtils.indexOfType(nested, null, 0));
+ assertEquals(-1, ExceptionUtils.indexOfType(nested, ExceptionWithCause.class, 0));
+ assertEquals(0, ExceptionUtils.indexOfType(nested, NestableException.class, 0));
+ assertEquals(1, ExceptionUtils.indexOfType(nested, ExceptionWithoutCause.class, 0));
+
+ assertEquals(-1, ExceptionUtils.indexOfType(withCause, null));
+ assertEquals(0, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class, 0));
+ assertEquals(1, ExceptionUtils.indexOfType(withCause, NestableException.class, 0));
+ assertEquals(2, ExceptionUtils.indexOfType(withCause, ExceptionWithoutCause.class, 0));
+
+ assertEquals(0, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class, -1));
+ assertEquals(0, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class, 0));
+ assertEquals(-1, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class, 1));
+ assertEquals(-1, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class, 9));
+
+ assertEquals(0, ExceptionUtils.indexOfType(withCause, Exception.class, 0));
}
//-----------------------------------------------------------------------