suppressedExceptions = SUPPRESSED_SENTINEL;
/** Message for trying to suppress a null exception. */
private static final String NULL_CAUSE_MESSAGE = "Cannot suppress a null exception.";
+ /** Message for trying to suppress oneself. */
+ private static final String SELF_SUPPRESSION_MESSAGE = "Self-suppression not permitted";
+
/** Caption for labeling causative exception stack traces */
private static final String CAUSE_CAPTION = "Caused by: ";
@@ -572,7 +633,7 @@
s.println("\tat " + traceElement);
// Print suppressed exceptions, if any
- for (Throwable se : getSuppressedExceptions())
+ for (Throwable se : getSuppressed())
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);
// Print cause, if any
@@ -613,7 +674,7 @@
s.println(prefix + "\t... " + framesInCommon + " more");
// Print suppressed exceptions, if any
- for (Throwable se : getSuppressedExceptions())
+ for (Throwable se : getSuppressed())
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION,
prefix +"\t", dejaVu);
@@ -684,10 +745,14 @@
* {@code Throwable} object information about the current state of
* the stack frames for the current thread.
*
+ * If the stack trace of this {@code Throwable} {@linkplain
+ * Throwable#setStackTrace(StackTraceElement[]) has been set} to
+ * {@code null}, calling this method has no effect.
+ *
* @return a reference to this {@code Throwable} instance.
* @see java.lang.Throwable#printStackTrace()
*/
- public synchronized native Throwable fillInStackTrace();
+ public native synchronized Throwable fillInStackTrace();
/**
* Provides programmatic access to the stack trace information printed by
@@ -744,8 +809,7 @@
* returns will have no affect on this {@code Throwable}'s stack
* trace.
*
- * @throws NullPointerException if {@code stackTrace} is
- * {@code null}, or if any of the elements of
+ * @throws NullPointerException if any of the elements of
* {@code stackTrace} are {@code null}
*
* @since 1.4
@@ -755,7 +819,7 @@
for (int i = 0; i < defensiveCopy.length; i++)
if (defensiveCopy[i] == null)
throw new NullPointerException("stackTrace[" + i + "]");
-
+
synchronized (this) {
this.stackTrace = defensiveCopy;
}
@@ -780,25 +844,58 @@
*/
native StackTraceElement getStackTraceElement(int index);
+ /**
+ * Read a {@code Throwable} from a stream, enforcing
+ * well-formedness constraints on fields. Null entries and
+ * self-pointers are not allowed in the list of {@code
+ * suppressedExceptions}. Null entries are not allowed for stack
+ * trace elements.
+ *
+ * Note that there are no constraints on the value the {@code
+ * cause} field can hold; both {@code null} and this are valid
+ * values for the field.
+ */
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException {
s.defaultReadObject(); // read in all fields
- List suppressed = null;
- if (suppressedExceptions != null &&
- !suppressedExceptions.isEmpty()) { // Copy Throwables to new list
- suppressed = new ArrayList();
- for (Throwable t : suppressedExceptions) {
- if (t == null)
- throw new NullPointerException(NULL_CAUSE_MESSAGE);
- suppressed.add(t);
+ if (suppressedExceptions != null) {
+ List suppressed = null;
+ if(suppressedExceptions.isEmpty()) {
+ // Use the sentinel for a zero-length list
+ suppressed = SUPPRESSED_SENTINEL;
+ } else { // Copy Throwables to new list
+ suppressed = new ArrayList(1);
+ for (Throwable t : suppressedExceptions) {
+ // Enforce constraints on suppressed exceptions in
+ // case of corrupt or malicious stream.
+ if (t == null)
+ throw new NullPointerException(NULL_CAUSE_MESSAGE);
+ if (t == this)
+ throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE);
+ suppressed.add(t);
+ }
+ }
+ suppressedExceptions = suppressed;
+ } // else a null suppressedExceptions field remains null
+
+ if (stackTrace != null) {
+ for(StackTraceElement ste : stackTrace) {
+ if (ste == null)
+ throw new NullPointerException("null StackTraceElement in serial stream. ");
}
+ } else {
+ // A null stackTrace field in the serial form can result from
+ // an exception serialized without that field in older JDK releases.
+ stackTrace = EMPTY_STACK;
}
- suppressedExceptions = suppressed;
+
}
+ /**
+ * Write a {@code Throwable} object to a stream.
+ */
private synchronized void writeObject(ObjectOutputStream s)
- throws IOException
- {
+ throws IOException {
getOurStackTrace(); // Ensure that stackTrace field is initialized.
s.defaultWriteObject();
}
@@ -808,6 +905,14 @@
* were suppressed, typically by the {@code try}-with-resources
* statement, in order to deliver this exception.
*
+ * If the first exception to be suppressed is {@code null}, that
+ * indicates suppressed exception information will not be
+ * recorded for this exception. Subsequent calls to this method
+ * will not record any suppressed exceptions. Otherwise,
+ * attempting to suppress {@code null} after an exception has
+ * already been successfully suppressed results in a {@code
+ * NullPointerException}.
+ *
* Note that when one exception {@linkplain
* #initCause(Throwable) causes} another exception, the first
* exception is usually caught and then the second exception is
@@ -819,20 +924,35 @@
*
* @param exception the exception to be added to the list of
* suppressed exceptions
- * @throws NullPointerException if {@code exception} is null
* @throws IllegalArgumentException if {@code exception} is this
* throwable; a throwable cannot suppress itself.
+ * @throws NullPointerException if {@code exception} is null and
+ * an exception has already been suppressed by this exception
* @since 1.7
*/
- public synchronized void addSuppressedException(Throwable exception) {
- if (exception == null)
- throw new NullPointerException(NULL_CAUSE_MESSAGE);
+ public final synchronized void addSuppressed(Throwable exception) {
if (exception == this)
- throw new IllegalArgumentException("Self-suppression not permitted");
+ throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE);
- if (suppressedExceptions == null)
- suppressedExceptions = new ArrayList();
- suppressedExceptions.add(exception);
+ if (exception == null) {
+ if (suppressedExceptions == SUPPRESSED_SENTINEL) {
+ suppressedExceptions = null; // No suppression information recorded
+ return;
+ } else
+ throw new NullPointerException(NULL_CAUSE_MESSAGE);
+ } else {
+ assert exception != null && exception != this;
+
+ if (suppressedExceptions == null) // Suppressed exceptions not recorded
+ return;
+
+ if (suppressedExceptions == SUPPRESSED_SENTINEL)
+ suppressedExceptions = new ArrayList(1);
+
+ assert suppressedExceptions != SUPPRESSED_SENTINEL;
+
+ suppressedExceptions.add(exception);
+ }
}
private static final Throwable[] EMPTY_THROWABLE_ARRAY = new Throwable[0];
@@ -842,12 +962,15 @@
* suppressed, typically by the {@code try}-with-resources
* statement, in order to deliver this exception.
*
+ * If no exceptions were suppressed, an empty array is returned.
+ *
* @return an array containing all of the exceptions that were
* suppressed to deliver this exception.
* @since 1.7
*/
- public synchronized Throwable[] getSuppressedExceptions() {
- if (suppressedExceptions == null)
+ public final synchronized Throwable[] getSuppressed() {
+ if (suppressedExceptions == SUPPRESSED_SENTINEL ||
+ suppressedExceptions == null)
return EMPTY_THROWABLE_ARRAY;
else
return suppressedExceptions.toArray(EMPTY_THROWABLE_ARRAY);