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
This commit is contained in:
Stephen Colebourne 2004-10-09 10:45:24 +00:00
parent fbaf1c371a
commit 657eac9347
7 changed files with 125 additions and 35 deletions

View File

@ -28,7 +28,7 @@ import java.io.PrintWriter;
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @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 <code>Throwable</code>s, or -1 if the
* specified type is not found in the chain.
* specified type, or a subclass, in the chain of <code>Throwable</code>s.
* The method returns -1 if the specified type is not found in the chain.
* <p>
* NOTE: From v2.1, we have clarified the <code>Nestable</code> 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 <code>Class</code> 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 <code>Throwable</code>
* that matches the specified type in the chain of <code>Throwable</code>s
* 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 <code>Throwable</code>s
* 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.
* <p>
* NOTE: From v2.1, we have clarified the <code>Nestable</code> 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 <code>Class</code> 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
* <code>super.printStackTrace(out);</code> in most cases.

View File

@ -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 <code>NestableDelegate</code> instance to manage the
@ -212,11 +228,19 @@ public class NestableDelegate implements Serializable {
/**
* Returns the index, numbered from 0, of the first <code>Throwable</code>
* that matches the specified type in the chain of <code>Throwable</code>s
* held in this delegate's <code>Nestable</code> 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 <code>Throwable</code>s
* 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.
* <p>
* NOTE: From v2.1, we have clarified the <code>Nestable</code> 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 <code>NestableDelegate</code>, however this is not recommended.
*
* @param type <code>Class</code> 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;

View File

@ -25,7 +25,7 @@ import junit.framework.TestCase;
* interface.
*
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @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();
}

View File

@ -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 <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* @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);

View File

@ -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 <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @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;
}

View File

@ -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 <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @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();

View File

@ -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 <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @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;
}