From 657eac934788180b8fbae3537e349543b127641e Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sat, 9 Oct 2004 10:45:24 +0000 Subject: [PATCH] Change behaviour of indexOf to match subclasses, with appropriate comments bug 30929 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137969 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/lang/exception/Nestable.java | 32 +++++++---- .../lang/exception/NestableDelegate.java | 53 +++++++++++++++---- .../exception/AbstractNestableTestCase.java | 17 ++++-- .../exception/NestableDelegateTestCase.java | 16 ++++-- .../lang/exception/NestableErrorTestCase.java | 14 ++++- .../exception/NestableExceptionTestCase.java | 15 ++++-- .../NestableRuntimeExceptionTestCase.java | 13 ++++- 7 files changed, 125 insertions(+), 35 deletions(-) diff --git a/src/java/org/apache/commons/lang/exception/Nestable.java b/src/java/org/apache/commons/lang/exception/Nestable.java index 2cbaab89c..eef81c78e 100644 --- a/src/java/org/apache/commons/lang/exception/Nestable.java +++ b/src/java/org/apache/commons/lang/exception/Nestable.java @@ -28,7 +28,7 @@ import java.io.PrintWriter; * @author Steven Caswell * @author Pete Gieser * @since 1.0 - * @version $Id: Nestable.java,v 1.11 2004/02/18 22:54:04 ggregory Exp $ + * @version $Id: Nestable.java,v 1.12 2004/10/09 10:45:24 scolebourne Exp $ */ public interface Nestable { @@ -106,10 +106,16 @@ public interface Nestable { /** * Returns the index, numbered from 0, of the first occurrence of the - * specified type in the chain of Throwables, or -1 if the - * specified type is not found in the chain. + * specified type, or a subclass, in the chain of Throwables. + * The method returns -1 if the specified type is not found in the chain. + *

+ * NOTE: From v2.1, we have clarified the Nestable interface + * such that this method matches subclasses. + * If you want to NOT match subclasses, please use + * {@link ExceptionUtils#indexOfThrowable(Throwable, Class)} + * (which is avaiable in all versions of lang). * - * @param type Class to be found + * @param type the type to find, subclasses match, null returns -1 * @return index of the first occurrence of the type in the chain, or -1 if * the type is not found */ @@ -117,11 +123,17 @@ public interface Nestable { /** * Returns the index, numbered from 0, of the first Throwable - * that matches the specified type in the chain of Throwables - * with an index greater than or equal to the specified index, or -1 if - * the type is not found. + * that matches the specified type, or a subclass, in the chain of Throwables + * with an index greater than or equal to the specified index. + * The method returns -1 if the specified type is not found in the chain. + *

+ * NOTE: From v2.1, we have clarified the Nestable interface + * such that this method matches subclasses. + * If you want to NOT match subclasses, please use + * {@link ExceptionUtils#indexOfThrowable(Throwable, Class, int)} + * (which is avaiable in all versions of lang). * - * @param type Class to be found + * @param type the type to find, subclasses match, null returns -1 * @param fromIndex the index, numbered from 0, of the starting position in * the chain to be searched * @return index of the first occurrence of the type in the chain, or -1 if @@ -152,8 +164,8 @@ public interface Nestable { /** * Prints the stack trace for this exception only--root cause not - * included--using the provided writer. Used by {@link - * org.apache.commons.lang.exception.NestableDelegate} to write + * included--using the provided writer. Used by + * {@link org.apache.commons.lang.exception.NestableDelegate} to write * individual stack traces to a buffer. The implementation of * this method should call * super.printStackTrace(out); in most cases. diff --git a/src/java/org/apache/commons/lang/exception/NestableDelegate.java b/src/java/org/apache/commons/lang/exception/NestableDelegate.java index f0b247476..537c073de 100644 --- a/src/java/org/apache/commons/lang/exception/NestableDelegate.java +++ b/src/java/org/apache/commons/lang/exception/NestableDelegate.java @@ -41,7 +41,7 @@ import java.util.List; * @author Sean C. Sullivan * @author Stephen Colebourne * @since 1.0 - * @version $Id: NestableDelegate.java,v 1.25 2004/09/30 07:03:25 bayard Exp $ + * @version $Id: NestableDelegate.java,v 1.26 2004/10/09 10:45:24 scolebourne Exp $ */ public class NestableDelegate implements Serializable { @@ -62,6 +62,9 @@ public class NestableDelegate implements Serializable { /** * Whether to print the stack trace top-down. * This public flag may be set by calling code, typically in initialisation. + * This exists for backwards compatability, setting it to false will return + * the library to v1.0 behaviour (but will affect all users of the library + * in the classloader). * @since 2.0 */ public static boolean topDown = true; @@ -69,9 +72,22 @@ public class NestableDelegate implements Serializable { /** * Whether to trim the repeated stack trace. * This public flag may be set by calling code, typically in initialisation. + * This exists for backwards compatability, setting it to false will return + * the library to v1.0 behaviour (but will affect all users of the library + * in the classloader). * @since 2.0 */ public static boolean trimStackFrames = true; + + /** + * Whether to match subclasses via indexOf. + * This public flag may be set by calling code, typically in initialisation. + * This exists for backwards compatability, setting it to false will return + * the library to v2.0 behaviour (but will affect all users of the library + * in the classloader). + * @since 2.1 + */ + public static boolean matchSubclasses = true; /** * Constructs a new NestableDelegate instance to manage the @@ -212,11 +228,19 @@ public class NestableDelegate implements Serializable { /** * Returns the index, numbered from 0, of the first Throwable - * that matches the specified type in the chain of Throwables - * held in this delegate's Nestable with an index greater than - * or equal to the specified index, or -1 if the type is not found. + * that matches the specified type, or a subclass, in the chain of Throwables + * with an index greater than or equal to the specified index. + * The method returns -1 if the specified type is not found in the chain. + *

+ * NOTE: From v2.1, we have clarified the Nestable interface + * such that this method matches subclasses. + * If you want to NOT match subclasses, please use + * {@link ExceptionUtils#indexOfThrowable(Throwable, Class, int)} + * (which is avaiable in all versions of lang). + * An alternative is to use the public static flag {@link #matchSubclasses} + * on NestableDelegate, however this is not recommended. * - * @param type Class to be found + * @param type the type to find, subclasses match, null returns -1 * @param fromIndex the index, numbered from 0, of the starting position in * the chain to be searched * @return index of the first occurrence of the type in the chain, or -1 if @@ -227,6 +251,9 @@ public class NestableDelegate implements Serializable { * @since 2.0 */ public int indexOfThrowable(Class type, int fromIndex) { + if (type == null) { + return -1; + } if (fromIndex < 0) { throw new IndexOutOfBoundsException("The start index was out of bounds: " + fromIndex); } @@ -235,11 +262,17 @@ public class NestableDelegate implements Serializable { throw new IndexOutOfBoundsException("The start index was out of bounds: " + fromIndex + " >= " + throwables.length); } - 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 (matchSubclasses) { + 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/AbstractNestableTestCase.java b/src/test/org/apache/commons/lang/exception/AbstractNestableTestCase.java index 57e5937e4..1dbd7de71 100644 --- a/src/test/org/apache/commons/lang/exception/AbstractNestableTestCase.java +++ b/src/test/org/apache/commons/lang/exception/AbstractNestableTestCase.java @@ -25,7 +25,7 @@ import junit.framework.TestCase; * interface. * * @author Steven Caswell - * @version $Id: AbstractNestableTestCase.java,v 1.6 2004/02/18 23:02:15 ggregory Exp $ + * @version $Id: AbstractNestableTestCase.java,v 1.7 2004/10/09 10:45:24 scolebourne Exp $ */ public abstract class AbstractNestableTestCase extends TestCase { @@ -360,7 +360,9 @@ public abstract class AbstractNestableTestCase extends TestCase { doNestableExceptionIndexOfThrowable(n, throwables[i], indexes[i], msgs[indexes[i]]); } + doNestableExceptionIndexOfThrowable(n, getBaseThrowableClass(), 0, msgs[0]); doNestableExceptionIndexOfThrowable(n, java.util.Date.class, -1, null); + doNestableExceptionIndexOfThrowable(n, null, -1, null); } private void doNestableExceptionIndexOfThrowable(Nestable n, Class type, int expectedIndex, String expectedMsg) @@ -368,7 +370,7 @@ public abstract class AbstractNestableTestCase extends TestCase Throwable t = null; int index = n.indexOfThrowable(type); - assertEquals("index of throwable " + type.getName(), expectedIndex, index); + assertEquals("index of throwable " + (type == null ? "null" : type.getName()), expectedIndex, index); if(expectedIndex > -1) { t = n.getThrowable(index); @@ -421,6 +423,7 @@ public abstract class AbstractNestableTestCase extends TestCase doNestableExceptionIndexOfThrowableI(n, getTester1Class(), 4, -1, null); doNestableExceptionIndexOfThrowableI(n, getThrowableClass(), 2, 4, msgs[4]); doNestableExceptionIndexOfThrowableI(n, java.util.Date.class, 0, -1, null); + doNestableExceptionIndexOfThrowableI(n, null, 0, -1, null); // Test for index out of bounds try @@ -447,7 +450,7 @@ public abstract class AbstractNestableTestCase extends TestCase Throwable t = null; int index = n.indexOfThrowable(type, fromIndex); - assertEquals("index of throwable " + type.getName(), expectedIndex, index); + assertEquals("index of throwable " + (type == null ? "null" : type.getName()), expectedIndex, index); if(expectedIndex > -1) { t = n.getThrowable(index); @@ -665,5 +668,13 @@ public abstract class AbstractNestableTestCase extends TestCase * @return the class */ public abstract Class getThrowableClass(); + + /** + * Returns the base class being used, typically Error, Eception or RuntimeException. + * + * @return the class + */ + public abstract Class getBaseThrowableClass(); + } diff --git a/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java b/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java index 0fffb365e..149d83a5b 100644 --- a/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java +++ b/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java @@ -16,6 +16,8 @@ package org.apache.commons.lang.exception; import java.io.ByteArrayOutputStream; +import java.io.EOFException; +import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; @@ -28,7 +30,7 @@ import junit.textui.TestRunner; * * @author Steven Caswell * @author Daniel Rall - * @version $Id: NestableDelegateTestCase.java,v 1.8 2004/02/18 23:02:15 ggregory Exp $ + * @version $Id: NestableDelegateTestCase.java,v 1.9 2004/10/09 10:45:24 scolebourne Exp $ */ public class NestableDelegateTestCase extends junit.framework.TestCase { private static final String CONSTRUCTOR_FAILED_MSG = @@ -422,13 +424,13 @@ public class NestableDelegateTestCase extends junit.framework.TestCase { throwables[1] = NestableDelegateTester2.class; throwables[2] = NestableDelegateTester1.class; throwables[3] = NestableDelegateTester2.class; - throwables[4] = Exception.class; + throwables[4] = EOFException.class; int[] indexes = {0, 1, 0, 1, 4}; n = new NestableDelegateTester1(msgs[0], new NestableDelegateTester2(msgs[1], new NestableDelegateTester1( new NestableDelegateTester2(msgs[3], - new Exception(msgs[4]) + new EOFException(msgs[4]) ) ) ) @@ -442,8 +444,12 @@ public class NestableDelegateTestCase extends junit.framework.TestCase { doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 1, 2, msgs[2]); doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 3, -1, null); doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 4, -1, null); - doNestableDelegateIndexOfThrowable(d, Exception.class, 2, 4, msgs[4]); + doNestableDelegateIndexOfThrowable(d, EOFException.class, 2, 4, msgs[4]); + doNestableDelegateIndexOfThrowable(d, IOException.class, 2, 4, msgs[4]); + doNestableDelegateIndexOfThrowable(d, Exception.class, 2, 2, msgs[2]); + doNestableDelegateIndexOfThrowable(d, Exception.class, 0, 0, msgs[0]); doNestableDelegateIndexOfThrowable(d, java.util.Date.class, 0, -1, null); + doNestableDelegateIndexOfThrowable(d, null, 0, -1, null); // Test for index out of bounds try @@ -469,7 +475,7 @@ public class NestableDelegateTestCase extends junit.framework.TestCase { Throwable t = null; int index = d.indexOfThrowable(type, fromIndex); - assertEquals("index of throwable " + type.getName(), expectedIndex, index); + assertEquals("index of throwable " + (type == null ? "null" : type.getName()), expectedIndex, index); if(expectedIndex > -1) { t = d.getThrowable(index); diff --git a/src/test/org/apache/commons/lang/exception/NestableErrorTestCase.java b/src/test/org/apache/commons/lang/exception/NestableErrorTestCase.java index dc757b271..b65049dbd 100644 --- a/src/test/org/apache/commons/lang/exception/NestableErrorTestCase.java +++ b/src/test/org/apache/commons/lang/exception/NestableErrorTestCase.java @@ -15,6 +15,8 @@ */ package org.apache.commons.lang.exception; +import java.io.EOFException; + import junit.framework.Test; import junit.framework.TestSuite; import junit.textui.TestRunner; @@ -23,7 +25,7 @@ import junit.textui.TestRunner; * Tests the org.apache.commons.lang.exception.NestableError class. * * @author Steven Caswell - * @version $Id: NestableErrorTestCase.java,v 1.6 2004/02/18 23:02:15 ggregory Exp $ + * @version $Id: NestableErrorTestCase.java,v 1.7 2004/10/09 10:45:24 scolebourne Exp $ */ public class NestableErrorTestCase extends AbstractNestableTestCase { @@ -189,13 +191,21 @@ public class NestableErrorTestCase extends AbstractNestableTestCase { */ public Throwable getThrowable(String msg) { - return new Error(msg); + return new EOFException(msg); } /** * @see AbstractNestableTestCase#getThrowableClass() */ public Class getThrowableClass() + { + return EOFException.class; + } + + /** + * @see AbstractNestableTestCase#getBaseThrowableClass() + */ + public Class getBaseThrowableClass() { return Error.class; } diff --git a/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java b/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java index 2ddb74634..5ace943a6 100644 --- a/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java +++ b/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java @@ -17,6 +17,7 @@ package org.apache.commons.lang.exception; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.EOFException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.PrintStream; @@ -29,7 +30,7 @@ import junit.textui.TestRunner; * Tests the org.apache.commons.lang.exception.NestableException class. * * @author Steven Caswell - * @version $Id: NestableExceptionTestCase.java,v 1.11 2004/02/18 23:02:15 ggregory Exp $ + * @version $Id: NestableExceptionTestCase.java,v 1.12 2004/10/09 10:45:24 scolebourne Exp $ */ public class NestableExceptionTestCase extends AbstractNestableTestCase { @@ -195,7 +196,7 @@ public class NestableExceptionTestCase extends AbstractNestableTestCase { */ public Throwable getThrowable(String msg) { - return new Exception(msg); + return new EOFException(msg); } /** @@ -203,9 +204,17 @@ public class NestableExceptionTestCase extends AbstractNestableTestCase { */ public Class getThrowableClass() { - return Exception.class; + return EOFException.class; } + /** + * @see AbstractNestableTestCase#getBaseThrowableClass() + */ + public Class getBaseThrowableClass() + { + return Exception.class; + } + public void testSpecificPrintStackTrace() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); diff --git a/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java b/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java index fbbf7259c..c8eeabaa3 100644 --- a/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java +++ b/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java @@ -16,6 +16,7 @@ package org.apache.commons.lang.exception; import java.io.ByteArrayOutputStream; +import java.io.EOFException; import java.io.PrintStream; import junit.framework.Test; @@ -26,7 +27,7 @@ import junit.textui.TestRunner; * Tests the org.apache.commons.lang.exception.NestableRuntimeException class. * * @author Steven Caswell - * @version $Id: NestableRuntimeExceptionTestCase.java,v 1.11 2004/02/18 23:22:29 ggregory Exp $ + * @version $Id: NestableRuntimeExceptionTestCase.java,v 1.12 2004/10/09 10:45:24 scolebourne Exp $ */ public class NestableRuntimeExceptionTestCase extends AbstractNestableTestCase { @@ -192,13 +193,21 @@ public class NestableRuntimeExceptionTestCase extends AbstractNestableTestCase { */ public Throwable getThrowable(String msg) { - return new RuntimeException(msg); + return new EOFException(msg); } /** * @see AbstractNestableTestCase#getThrowableClass() */ public Class getThrowableClass() + { + return EOFException.class; + } + + /** + * @see AbstractNestableTestCase#getBaseThrowableClass() + */ + public Class getBaseThrowableClass() { return RuntimeException.class; }