Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 7954 invoked from network); 21 Aug 2002 23:51:29 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 21 Aug 2002 23:51:29 -0000 Received: (qmail 14294 invoked by uid 97); 21 Aug 2002 23:52:01 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 14279 invoked by uid 97); 21 Aug 2002 23:52:01 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 14268 invoked by uid 97); 21 Aug 2002 23:52:00 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Date: 21 Aug 2002 23:51:22 -0000 Message-ID: <20020821235122.68963.qmail@icarus.apache.org> From: dlr@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/exception ExceptionUtils.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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 . 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 Throwable objects. @@ -271,5 +278,96 @@ } } return null; + } + + /** + * Returns the number of Throwable 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 Throwable objects in the + * exception chain. + * + * @return The list of Throwable 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 + * Throwable that matches the specified type in the + * exception chain of Throwable objects with an index + * greater than or equal to the specified index, or + * -1 if the type is not found. + * + * @param type Class 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 + * -1 if the type is not found. + * @throws IndexOutOfBoundsException If the fromIndex + * argument is negative or not less than the count of + * Throwables 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: For additional commands, e-mail: