diff --git a/src/java/org/apache/commons/lang/exception/ContextedException.java b/src/java/org/apache/commons/lang/exception/ContextedException.java index 6885ae855..d9c4c9dcb 100644 --- a/src/java/org/apache/commons/lang/exception/ContextedException.java +++ b/src/java/org/apache/commons/lang/exception/ContextedException.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang.exception; -import java.io.Serializable; import java.util.Set; /** @@ -141,15 +140,13 @@ public class ContextedException extends Exception implements ExceptionContext { * the problem. For the information to be meaningful, the value passed * should have a reasonable toString() implementation. *

- * Note: If the value provided isn't Serializable, one solution would be - * to provide its toString() if it has a meaningful implementation or - * individual properties of the value object instead. + * Note: This exception is only serializable if the object added is serializable. * * @param label a textual label associated with information, null not recommended * @param value information needed to understand exception, may be null * @return this, for method chaining */ - public ContextedException addLabeledValue(String label, Serializable value) { + public ContextedException addLabeledValue(String label, Object value) { exceptionContext.addLabeledValue(label, value); return this; } @@ -160,7 +157,7 @@ public class ContextedException extends Exception implements ExceptionContext { * @param label the label to get the contextual value for, may be null * @return the contextual value associated with the label, may be null */ - public Serializable getLabeledValue(String label) { + public Object getLabeledValue(String label) { return exceptionContext.getLabeledValue(label); } diff --git a/src/java/org/apache/commons/lang/exception/ContextedRuntimeException.java b/src/java/org/apache/commons/lang/exception/ContextedRuntimeException.java index 46575d1d4..41ea0899e 100644 --- a/src/java/org/apache/commons/lang/exception/ContextedRuntimeException.java +++ b/src/java/org/apache/commons/lang/exception/ContextedRuntimeException.java @@ -16,7 +16,6 @@ */ package org.apache.commons.lang.exception; -import java.io.Serializable; import java.util.Set; /** @@ -142,15 +141,13 @@ public class ContextedRuntimeException extends Exception implements ExceptionCon * the problem. For the information to be meaningful, the value passed * should have a reasonable toString() implementation. *

- * Note: If the value provided isn't Serializable, one solution would be - * to provide its toString() if it has a meaningful implementation or - * individual properties of the value object instead. + * Note: This exception is only serializable if the object added is serializable. * * @param label a textual label associated with information, null not recommended * @param value information needed to understand exception, may be null * @return this, for method chaining */ - public ContextedRuntimeException addLabeledValue(String label, Serializable value) { + public ContextedRuntimeException addLabeledValue(String label, Object value) { exceptionContext.addLabeledValue(label, value); return this; } @@ -161,7 +158,7 @@ public class ContextedRuntimeException extends Exception implements ExceptionCon * @param label the label to get the contextual value for, may be null * @return the contextual value associated with the label, may be null */ - public Serializable getLabeledValue(String label) { + public Object getLabeledValue(String label) { return exceptionContext.getLabeledValue(label); } diff --git a/src/java/org/apache/commons/lang/exception/DefaultExceptionContext.java b/src/java/org/apache/commons/lang/exception/DefaultExceptionContext.java index 1153a92f1..7a4caa9ab 100644 --- a/src/java/org/apache/commons/lang/exception/DefaultExceptionContext.java +++ b/src/java/org/apache/commons/lang/exception/DefaultExceptionContext.java @@ -25,16 +25,19 @@ import org.apache.commons.lang.SystemUtils; /** * Default implementation of the context storing the label-value pairs for contexted exceptions. + *

+ * This implementation is serializable, however this is dependent on the values that + * are added also being serializable. * * @author D. Ashmore * @since 3.0 */ -class DefaultExceptionContext implements ExceptionContext { +class DefaultExceptionContext implements ExceptionContext, Serializable { /** The serialization version. */ private static final long serialVersionUID = 293747957535772807L; /** The ordered map storing the label-data pairs. */ - private Map contextValueMap = new LinkedHashMap(); + private Map contextValueMap = new LinkedHashMap(); /** * Adds a contextual label-value pair into this context. @@ -45,7 +48,7 @@ class DefaultExceptionContext implements ExceptionContext { * @param value the value of item to add, may be null * @return this, for method chaining */ - public ExceptionContext addLabeledValue(String label, Serializable value) { + public ExceptionContext addLabeledValue(String label, Object value) { contextValueMap.put(label, value); return this; } @@ -56,7 +59,7 @@ class DefaultExceptionContext implements ExceptionContext { * @param label the label to get the contextual value for, may be null * @return the contextual value associated with the label, may be null */ - public Serializable getLabeledValue(String label) { + public Object getLabeledValue(String label) { return contextValueMap.get(label); } diff --git a/src/java/org/apache/commons/lang/exception/ExceptionContext.java b/src/java/org/apache/commons/lang/exception/ExceptionContext.java index be3e0fdc7..1685f2d25 100644 --- a/src/java/org/apache/commons/lang/exception/ExceptionContext.java +++ b/src/java/org/apache/commons/lang/exception/ExceptionContext.java @@ -16,21 +16,18 @@ */ package org.apache.commons.lang.exception; -import java.io.Serializable; import java.util.Set; - /** - * Provides context information for exceptions. It is available as separate interface to allow - * it usage independently from the {@link ContextedException} and - * {@link ContextedRuntimeException}. + * Allows the storage and retrieval of contextual information based on label-value + * pairs for exceptions. * * @see ContextedException * @see ContextedRuntimeException * @author D. Ashmore * @since 3.0 */ -public interface ExceptionContext extends Serializable { +public interface ExceptionContext { /** * Adds a contextual label-value pair into this context. @@ -41,7 +38,7 @@ public interface ExceptionContext extends Serializable { * @param value the value of item to add, may be null * @return context itself to allow method chaining */ - public ExceptionContext addLabeledValue(String label, Serializable value); + public ExceptionContext addLabeledValue(String label, Object value); /** * Retrieves a contextual data value associated with the label. @@ -49,7 +46,7 @@ public interface ExceptionContext extends Serializable { * @param label the label to get the contextual value for, may be null * @return the contextual value associated with the label, may be null */ - public Serializable getLabeledValue(String label); + public Object getLabeledValue(String label); /** * Retrieves the labels defined in the contextual data. diff --git a/src/java/org/apache/commons/lang/exception/ExceptionUtils.java b/src/java/org/apache/commons/lang/exception/ExceptionUtils.java index 5ddab311b..0a3f71bb7 100644 --- a/src/java/org/apache/commons/lang/exception/ExceptionUtils.java +++ b/src/java/org/apache/commons/lang/exception/ExceptionUtils.java @@ -18,7 +18,6 @@ package org.apache.commons.lang.exception; import java.io.PrintStream; import java.io.PrintWriter; -import java.io.Serializable; import java.io.StringWriter; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -26,14 +25,9 @@ import java.lang.reflect.Method; import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; -import java.util.Set; import java.util.StringTokenizer; -//import net.jcip.annotations.GuardedBy; -//import net.jcip.annotations.ThreadSafe; - import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ClassUtils; import org.apache.commons.lang.StringUtils; @@ -88,37 +82,10 @@ public class ExceptionUtils { *

The Method object for Java 1.4 getCause.

*/ private static final Method THROWABLE_CAUSE_METHOD; - /** *

The Method object for Java 1.4 initCause.

*/ private static final Method THROWABLE_INITCAUSE_METHOD; - - /** - * An empty {@link ExceptionContext}. - * @since 3.0 - */ - public static final ExceptionContext EMPTY_CONTEXT = new ExceptionContext() { - - private static final long serialVersionUID = 1L; - - public ExceptionContext addLabeledValue(String label, Serializable value) { - throw new UnsupportedOperationException(); - } - - public Serializable getLabeledValue(String label) { - return null; - } - - public Set getLabelSet() { - return Collections.emptySet(); - } - - public String getFormattedExceptionMessage(String baseMessage) { - return baseMessage; - } - - }; static { Method causeMethod; try { @@ -134,7 +101,7 @@ public class ExceptionUtils { } THROWABLE_INITCAUSE_METHOD = causeMethod; } - + /** *

* Public constructor allows an instance of ExceptionUtils to be created, although that is not