commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/exception ExceptionUtils.java
Date Wed, 21 Aug 2002 23:51:22 GMT
dlr         2002/08/21 16:51:22

  Modified:    lang/src/java/org/apache/commons/lang/exception
                        ExceptionUtils.java
  Log:
  Added the getThrowableCount(Throwable), getThrowables(Throwable),
  indexOfThrowable(Throwable, Class), and indexOfThrowable(Throwable,
  Class, int) methods factored out of NestableDelegate, as suggested by
  Stephen Colebourne <scolebourne@btopenworld.com>.  Added a TODO for
  the remaining methods he suggested moving.
  
  Revision  Changes    Path
  1.10      +98 -0     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.9
  retrieving revision 1.10
  diff -u -u -r1.9 -r1.10
  --- ExceptionUtils.java	21 Aug 2002 07:13:50 -0000	1.9
  +++ ExceptionUtils.java	21 Aug 2002 23:51:22 -0000	1.10
  @@ -58,6 +58,13 @@
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
   import java.sql.SQLException;
  +import java.util.ArrayList;
  +
  +/*
  +TODO: Refactor code from NestableDelegate to ExceptionUtils.
  +
  +printStackTrace(Throwable, PrintWriter)
  +*/
   
   /**
    * Utility routines for manipulating <code>Throwable</code> objects.
  @@ -271,5 +278,96 @@
               }
           }
           return null;
  +    }
  +
  +    /**
  +     * Returns the number of <code>Throwable</code> objects in the
  +     * exception chain.
  +     *
  +     * @return The throwable count.
  +     */
  +    public static int getThrowableCount(Throwable t)
  +    {
  +        // Count the number of throwables
  +        int count = 1;
  +        t = ExceptionUtils.getCause(t);
  +        while (t != null)
  +        {
  +            count++;
  +            t = ExceptionUtils.getCause(t);
  +        }
  +        return count;
  +    }
  +
  +    /**
  +     * Returns the list of <code>Throwable</code> objects in the
  +     * exception chain.
  +     *
  +     * @return The list of <code>Throwable</code> objects.
  +     */
  +    public static Throwable[] getThrowables(Throwable t)
  +    {
  +        ArrayList list = new ArrayList();
  +        if (t != null)
  +        {
  +            list.add(t);
  +            t = ExceptionUtils.getCause(t);
  +            while (t != null)
  +            {
  +                list.add(t);
  +                t = ExceptionUtils.getCause(t);
  +            }
  +        }
  +        return (Throwable []) list.toArray(new Throwable[list.size()]);
  +    }
  +
  +    /**
  +     * Delegates to {@link #indexOfThrowable(Throwable, Class, int)},
  +     * starting the search at the beginning of the exception chain.
  +     *
  +     * @see #indexOfThrowable(Throwable, Class, int)
  +     */
  +    public static int indexOfThrowable(Throwable t, Class type)
  +    {
  +        return indexOfThrowable(t, type, 0);
  +    }
  +
  +    /**
  +     * Returns the (zero based) index, of the first
  +     * <code>Throwable</code> that matches the specified type in the
  +     * exception chain of <code>Throwable</code> objects with an index
  +     * greater than or equal to the specified index, or
  +     * <code>-1</code> if the type is not found.
  +     *
  +     * @param type <code>Class</code> to look for.
  +     * @param fromIndex The (zero based) index of the starting
  +     * position in the chain to be searched.
  +     * @return index The first occurrence of the type in the chain, or
  +     * <code>-1</code> if the type is not found.
  +     * @throws IndexOutOfBoundsException If the <code>fromIndex</code>
  +     * argument is negative or not less than the count of
  +     * <code>Throwable</code>s in the chain.
  +     */
  +    public static int indexOfThrowable(Throwable t, Class type, int fromIndex)
  +    {
  +        if (fromIndex < 0)
  +        {
  +            throw new IndexOutOfBoundsException
  +                ("Throwable index out of range: " + fromIndex);
  +        }
  +        Throwable[] throwables = ExceptionUtils.getThrowables(t);
  +        if (fromIndex >= throwables.length)
  +        {
  +            throw new IndexOutOfBoundsException
  +                ("Throwable index out of range: " + fromIndex);
  +        }
  +        for (int i = fromIndex; i < throwables.length; i++)
  +        {
  +            if (throwables[i].getClass().equals(type))
  +            {
  +                return i;
  +            }
  +        }
  +        return -1;
       }
   }
  
  
  

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


Mime
View raw message