Make exception implement Nestable interface and use NestableDelegate
bug 26954 (alternate implementation) git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137821 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9250f20144
commit
5e7cf0ed0e
|
@ -15,33 +15,156 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.lang;
|
package org.apache.commons.lang;
|
||||||
|
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.exception.Nestable;
|
||||||
|
import org.apache.commons.lang.exception.NestableDelegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Thrown to indicate that a method has not been implemented.</p>
|
* <p>Thrown to indicate that a block of code has not been implemented.</p>
|
||||||
*
|
*
|
||||||
* @author Matthew Hawthorne
|
* @author Matthew Hawthorne
|
||||||
|
* @author Stephen Colebourne
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
* @version $Id: NotImplementedException.java,v 1.6 2004/02/18 22:59:49 ggregory Exp $
|
* @version $Id: NotImplementedException.java,v 1.7 2004/03/04 00:13:38 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class NotImplementedException extends UnsupportedOperationException {
|
public class NotImplementedException
|
||||||
|
extends UnsupportedOperationException implements Nestable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Constructs the exception with the specified class.</p>
|
* The exception helper to delegate nested exception handling to.
|
||||||
|
*/
|
||||||
|
protected NestableDelegate delegate = new NestableDelegate(this);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds the reference to the exception or error that caused
|
||||||
|
* this exception to be thrown.
|
||||||
|
*/
|
||||||
|
private Throwable cause;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* Constructs a new <code>NotImplementedException</code> with default
|
||||||
|
* detail message.
|
||||||
|
*/
|
||||||
|
public NotImplementedException() {
|
||||||
|
super("Code is not implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new <code>NotImplementedException</code> with specified
|
||||||
|
* detail message.
|
||||||
|
*
|
||||||
|
* @param msg The error message.
|
||||||
|
*/
|
||||||
|
public NotImplementedException(String msg) {
|
||||||
|
super(msg == null ? "Code is not implemented" : msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new <code>NotImplementedException</code> with specified
|
||||||
|
* nested <code>Throwable</code> and default message.
|
||||||
|
*
|
||||||
|
* @param cause the exception or error that caused this exception to be
|
||||||
|
* thrown
|
||||||
|
*/
|
||||||
|
public NotImplementedException(Throwable cause) {
|
||||||
|
super("Code is not implemented");
|
||||||
|
this.cause = cause;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new <code>NotImplementedException</code> with specified
|
||||||
|
* detail message and nested <code>Throwable</code>.
|
||||||
|
*
|
||||||
|
* @param msg the error message
|
||||||
|
* @param cause the exception or error that caused this exception to be
|
||||||
|
* thrown
|
||||||
|
*/
|
||||||
|
public NotImplementedException(String msg, Throwable cause) {
|
||||||
|
super(msg == null ? "Code is not implemented" : msg);
|
||||||
|
this.cause = cause;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new <code>NotImplementedException</code> referencing
|
||||||
|
* the specified class.
|
||||||
*
|
*
|
||||||
* @param clazz the <code>Class</code> that has not implemented the method
|
* @param clazz the <code>Class</code> that has not implemented the method
|
||||||
*/
|
*/
|
||||||
public NotImplementedException(Class clazz) {
|
public NotImplementedException(Class clazz) {
|
||||||
super(
|
super((clazz == null ?
|
||||||
"Method is not implemented in class "
|
"Code is not implemented" :
|
||||||
+ ((clazz == null) ? null : clazz.getName()));
|
"Code is not implemented in " + clazz));
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
public Throwable getCause() {
|
||||||
|
return cause;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Constructs the exception with the specified message.</p>
|
* Returns the detail message string of this throwable. If it was
|
||||||
*
|
* created with a null message, returns the following:
|
||||||
* @param msg the exception message.
|
* (cause==null ? null : cause.toString()).
|
||||||
*/
|
*/
|
||||||
public NotImplementedException(String msg) {
|
public String getMessage() {
|
||||||
super(msg);
|
if (super.getMessage() != null) {
|
||||||
|
return super.getMessage();
|
||||||
|
} else if (cause != null) {
|
||||||
|
return cause.toString();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage(int index) {
|
||||||
|
if (index == 0) {
|
||||||
|
return super.getMessage();
|
||||||
|
} else {
|
||||||
|
return delegate.getMessage(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getMessages() {
|
||||||
|
return delegate.getMessages();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Throwable getThrowable(int index) {
|
||||||
|
return delegate.getThrowable(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getThrowableCount() {
|
||||||
|
return delegate.getThrowableCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Throwable[] getThrowables() {
|
||||||
|
return delegate.getThrowables();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int indexOfThrowable(Class type) {
|
||||||
|
return delegate.indexOfThrowable(type, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int indexOfThrowable(Class type, int fromIndex) {
|
||||||
|
return delegate.indexOfThrowable(type, fromIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printStackTrace() {
|
||||||
|
delegate.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printStackTrace(PrintStream out) {
|
||||||
|
delegate.printStackTrace(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printStackTrace(PrintWriter out) {
|
||||||
|
delegate.printStackTrace(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void printPartialStackTrace(PrintWriter out) {
|
||||||
|
super.printStackTrace(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ import junit.textui.TestRunner;
|
||||||
* JUnit tests.
|
* JUnit tests.
|
||||||
*
|
*
|
||||||
* @author Matthew Hawthorne
|
* @author Matthew Hawthorne
|
||||||
* @version $Id: NotImplementedExceptionTest.java,v 1.3 2004/02/18 23:06:19 ggregory Exp $
|
* @version $Id: NotImplementedExceptionTest.java,v 1.4 2004/03/04 00:13:38 scolebourne Exp $
|
||||||
* @see NotImplementedException
|
* @see NotImplementedException
|
||||||
*/
|
*/
|
||||||
public class NotImplementedExceptionTest extends TestCase {
|
public class NotImplementedExceptionTest extends TestCase {
|
||||||
|
@ -41,43 +41,57 @@ public class NotImplementedExceptionTest extends TestCase {
|
||||||
super(testName);
|
super(testName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// testConstructor
|
//-----------------------------------------------------------------------
|
||||||
|
public void testConstructor_() {
|
||||||
public void testConstructor_classArg_nullInput() {
|
NotImplementedException ex = new NotImplementedException();
|
||||||
final Class c = null;
|
assertEquals("Code is not implemented", ex.getMessage());
|
||||||
new NotImplementedException(c);
|
assertEquals(null, ex.getCause());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testConstructor_stringArg_nullInput() {
|
public void testConstructor_String1() {
|
||||||
final String s = null;
|
NotImplementedException ex = new NotImplementedException((String) null);
|
||||||
new NotImplementedException(s);
|
assertEquals("Code is not implemented", ex.getMessage());
|
||||||
|
assertEquals(null, ex.getCause());
|
||||||
|
}
|
||||||
|
public void testConstructor_String2() {
|
||||||
|
NotImplementedException ex = new NotImplementedException("msg");
|
||||||
|
assertEquals("msg", ex.getMessage());
|
||||||
|
assertEquals(null, ex.getCause());
|
||||||
}
|
}
|
||||||
|
|
||||||
// testGetMessage
|
public void testConstructor_Throwable1() {
|
||||||
|
NotImplementedException ex = new NotImplementedException((Throwable) null);
|
||||||
public void testGetMessage_classArg_nullInput() {
|
assertEquals("Code is not implemented", ex.getMessage());
|
||||||
final Class c = null;
|
assertEquals(null, ex.getCause());
|
||||||
final Throwable t = new NotImplementedException(c);
|
}
|
||||||
assertEquals("Method is not implemented in class null", t.getMessage());
|
public void testConstructor_Throwable2() {
|
||||||
|
Exception npe = new NullPointerException();
|
||||||
|
NotImplementedException ex = new NotImplementedException(npe);
|
||||||
|
assertEquals("Code is not implemented", ex.getMessage());
|
||||||
|
assertSame(npe, ex.getCause());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetMessage_classArg_validInput() {
|
public void testConstructor_StringThrowable1() {
|
||||||
final Throwable t = new NotImplementedException(String.class);
|
NotImplementedException ex = new NotImplementedException((String) null, (Throwable) null);
|
||||||
assertEquals(
|
assertEquals("Code is not implemented", ex.getMessage());
|
||||||
"Method is not implemented in class java.lang.String",
|
assertEquals(null, ex.getCause());
|
||||||
t.getMessage());
|
}
|
||||||
|
public void testConstructor_StringThrowable2() {
|
||||||
|
Exception npe = new NullPointerException();
|
||||||
|
NotImplementedException ex = new NotImplementedException("msg", npe);
|
||||||
|
assertEquals("msg", ex.getMessage());
|
||||||
|
assertSame(npe, ex.getCause());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetMessage_stringArg_nullInput() {
|
public void testConstructor_Class1() {
|
||||||
final String s = null;
|
NotImplementedException ex = new NotImplementedException((Class) null);
|
||||||
final Throwable t = new NotImplementedException(s);
|
assertEquals("Code is not implemented", ex.getMessage());
|
||||||
assertEquals(null, t.getMessage());
|
assertEquals(null, ex.getCause());
|
||||||
|
}
|
||||||
|
public void testConstructor_Class2() {
|
||||||
|
NotImplementedException ex = new NotImplementedException(String.class);
|
||||||
|
assertEquals("Code is not implemented in class java.lang.String", ex.getMessage());
|
||||||
|
assertEquals(null, ex.getCause());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetMessage_stringArg_validInput() {
|
}
|
||||||
final String msg = "message";
|
|
||||||
final Throwable t = new NotImplementedException(msg);
|
|
||||||
assertEquals(msg, t.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
} // NotImplementedExceptionTest
|
|
||||||
|
|
Loading…
Reference in New Issue