commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/exception ExceptionUtils.java
Date Sat, 09 Oct 2004 10:04:04 GMT
scolebourne    2004/10/09 03:04:04

  Modified:    lang/src/test/org/apache/commons/lang/exception
                        ExceptionUtilsTestCase.java
               lang/src/java/org/apache/commons/lang/exception
                        ExceptionUtils.java
  Log:
  Add additional method to support subclass searching of exceptions in a chain
  bug 30929
  
  Revision  Changes    Path
  1.13      +54 -0     jakarta-commons/lang/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java
  
  Index: ExceptionUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- ExceptionUtilsTestCase.java	18 Feb 2004 23:22:29 -0000	1.12
  +++ ExceptionUtilsTestCase.java	9 Oct 2004 10:04:03 -0000	1.13
  @@ -179,6 +179,8 @@
           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 @@
           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));
       }
   
       //-----------------------------------------------------------------------
  
  
  
  1.42      +72 -14    jakarta-commons/lang/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
  
  Index: ExceptionUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/exception/ExceptionUtils.java,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -r1.41 -r1.42
  --- ExceptionUtils.java	30 Sep 2004 07:03:25 -0000	1.41
  +++ ExceptionUtils.java	9 Oct 2004 10:04:04 -0000	1.42
  @@ -391,24 +391,28 @@
       //-----------------------------------------------------------------------
       /**
        * <p>Returns the (zero based) index of the first <code>Throwable</code>
  -     * that matches the specified type in the exception chain.</p>
  +     * 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.</p>
        * 
        * <p>A <code>null</code> throwable returns <code>-1</code>.
        * A <code>null</code> type returns <code>-1</code>.
        * No match in the chain returns <code>-1</code>.</p>
        *
        * @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);
       }
   
       /**
        * <p>Returns the (zero based) index of the first <code>Throwable</code>
        * that matches the specified type in the exception chain from
  -     * a specified index.</p>
  +     * a specified index.
  +     * Subclasses of the specified class do not match - see
  +     * {@link #indexOfType(Throwable, Class, int)} for the opposite.</p>
        * 
        * <p>A <code>null</code> throwable returns <code>-1</code>.
        * A <code>null</code> type returns <code>-1</code>.
  @@ -417,13 +421,61 @@
        * A start index greater than the number of throwables returns <code>-1</code>.</p>
        *
        * @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);
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * <p>Returns the (zero based) index of the first <code>Throwable</code>
  +     * 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.</p>
  +     * 
  +     * <p>A <code>null</code> throwable returns <code>-1</code>.
  +     * A <code>null</code> type returns <code>-1</code>.
  +     * No match in the chain returns <code>-1</code>.</p>
  +     *
  +     * @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);
  +    }
  +
  +    /**
  +     * <p>Returns the (zero based) index of the first <code>Throwable</code>
  +     * 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.</p>
  +     * 
  +     * <p>A <code>null</code> throwable returns <code>-1</code>.
  +     * A <code>null</code> type returns <code>-1</code>.
  +     * No match in the chain returns <code>-1</code>.
  +     * A negative start index is treated as zero.
  +     * A start index greater than the number of throwables returns <code>-1</code>.</p>
  +     *
  +     * @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 @@
           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;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message