< prev index next >

## src/java.base/share/classes/java/lang/Throwable.java

Print this page

       1 /*
       2  * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
       3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       4  *
       5  * This code is free software; you can redistribute it and/or modify it
       6  * under the terms of the GNU General Public License version 2 only, as
       7  * published by the Free Software Foundation.  Oracle designates this
       8  * particular file as subject to the "Classpath" exception as provided
       9  * by Oracle in the LICENSE file that accompanied this code.
      10  *
      11  * This code is distributed in the hope that it will be useful, but WITHOUT
      12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14  * version 2 for more details (a copy is included in the LICENSE file that
      15  * accompanied this code).
      16  *
      17  * You should have received a copy of the GNU General Public License version
      18  * 2 along with this work; if not, write to the Free Software Foundation,
      19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      20  *
      21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      22  * or visit www.oracle.com if you need additional information or have any
     885                 backtrace == null) // Test for out of protocol state
     886                 return;
     887             this.stackTrace = defensiveCopy;
     888         }
     889     }
     890
     891     /**
     892      * Reads a {@code Throwable} from a stream, enforcing
     893      * well-formedness constraints on fields.  Null entries and
     894      * self-pointers are not allowed in the list of {@code
     895      * suppressedExceptions}.  Null entries are not allowed for stack
     896      * trace elements.  A null stack trace in the serial form results
     897      * in a zero-length stack element array. A single-element stack
     898      * trace whose entry is equal to {@code new StackTraceElement("",
     899      * "", null, Integer.MIN_VALUE)} results in a {@code null} {@code
     900      * stackTrace} field.
     901      *
     902      * Note that there are no constraints on the value the {@code
     903      * cause} field can hold; both {@code null} and {@code this} are
     904      * valid values for the field.

     905      */
     906     @java.io.Serial
     907     private void readObject(ObjectInputStream s)
     908         throws IOException, ClassNotFoundException {
     909         s.defaultReadObject();     // read in all fields
     910
     911         // Set suppressed exceptions and stack trace elements fields
     912         // to marker values until the contents from the serial stream
     913         // are validated.
     914         List<Throwable> candidateSuppressedExceptions = suppressedExceptions;
     915         suppressedExceptions = SUPPRESSED_SENTINEL;
     916
     917         StackTraceElement[] candidateStackTrace = stackTrace;
     918         stackTrace = UNASSIGNED_STACK.clone();
     919
     920         if (candidateSuppressedExceptions != null) {
     921             int suppressedSize = validateSuppressedExceptionsList(candidateSuppressedExceptions);
     922             if (suppressedSize > 0) { // Copy valid Throwables to new list
     923                 var suppList  = new ArrayList<Throwable>(Math.min(100, suppressedSize));
     924
     974     private int validateSuppressedExceptionsList(List<Throwable> deserSuppressedExceptions)
     975         throws IOException {
     976         if (!Object.class.getModule().
     977             equals(deserSuppressedExceptions.getClass().getModule())) {
     978             throw new StreamCorruptedException("List implementation not in base module.");
     979         } else {
     980             int size = deserSuppressedExceptions.size();
     981             if (size < 0) {
     982                 throw new StreamCorruptedException("Negative list size reported.");
     983             }
     984             return size;
     985         }
     986     }
     987
     988     /**
     989      * Write a {@code Throwable} object to a stream.
     990      *
     991      * A {@code null} stack trace field is represented in the serial
     992      * form as a one-element array whose element is equal to {@code
     993      * new StackTraceElement("", "", null, Integer.MIN_VALUE)}.

     994      */
     995     @java.io.Serial
     996     private synchronized void writeObject(ObjectOutputStream s)
     997         throws IOException {
     998         // Ensure that the stackTrace field is initialized to a
     999         // non-null value, if appropriate.  As of JDK 7, a null stack
    1000         // trace field is a valid value indicating the stack trace
    1001         // should not be set.
    1002         getOurStackTrace();
    1003
    1004         StackTraceElement[] oldStackTrace = stackTrace;
    1005         try {
    1006             if (stackTrace == null)
    1007                 stackTrace = SentinelHolder.STACK_TRACE_SENTINEL;
    1008             s.defaultWriteObject();
    1009         } finally {
    1010             stackTrace = oldStackTrace;
    1011         }
    1012     }
    1013
       1 /*
       2  * Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved.
       3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       4  *
       5  * This code is free software; you can redistribute it and/or modify it
       6  * under the terms of the GNU General Public License version 2 only, as
       7  * published by the Free Software Foundation.  Oracle designates this
       8  * particular file as subject to the "Classpath" exception as provided
       9  * by Oracle in the LICENSE file that accompanied this code.
      10  *
      11  * This code is distributed in the hope that it will be useful, but WITHOUT
      12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14  * version 2 for more details (a copy is included in the LICENSE file that
      15  * accompanied this code).
      16  *
      17  * You should have received a copy of the GNU General Public License version
      18  * 2 along with this work; if not, write to the Free Software Foundation,
      19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      20  *
      21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      22  * or visit www.oracle.com if you need additional information or have any
     885                 backtrace == null) // Test for out of protocol state
     886                 return;
     887             this.stackTrace = defensiveCopy;
     888         }
     889     }
     890
     891     /**
     892      * Reads a {@code Throwable} from a stream, enforcing
     893      * well-formedness constraints on fields.  Null entries and
     894      * self-pointers are not allowed in the list of {@code
     895      * suppressedExceptions}.  Null entries are not allowed for stack
     896      * trace elements.  A null stack trace in the serial form results
     897      * in a zero-length stack element array. A single-element stack
     898      * trace whose entry is equal to {@code new StackTraceElement("",
     899      * "", null, Integer.MIN_VALUE)} results in a {@code null} {@code
     900      * stackTrace} field.
     901      *
     902      * Note that there are no constraints on the value the {@code
     903      * cause} field can hold; both {@code null} and {@code this} are
     904      * valid values for the field.
     905      *
     906      * @param  s the {@code ObjectInputStream} from which data is read
     907      * @throws IOException if an I/O error occurs
     908      * @throws ClassNotFoundException if a serialized class cannot be loaded
     909      */
     910     @java.io.Serial
     911     private void readObject(ObjectInputStream s)
     912         throws IOException, ClassNotFoundException {
     913         s.defaultReadObject();     // read in all fields
     914
     915         // Set suppressed exceptions and stack trace elements fields
     916         // to marker values until the contents from the serial stream
     917         // are validated.
     918         List<Throwable> candidateSuppressedExceptions = suppressedExceptions;
     919         suppressedExceptions = SUPPRESSED_SENTINEL;
     920
     921         StackTraceElement[] candidateStackTrace = stackTrace;
     922         stackTrace = UNASSIGNED_STACK.clone();
     923
     924         if (candidateSuppressedExceptions != null) {
     925             int suppressedSize = validateSuppressedExceptionsList(candidateSuppressedExceptions);
     926             if (suppressedSize > 0) { // Copy valid Throwables to new list
     927                 var suppList  = new ArrayList<Throwable>(Math.min(100, suppressedSize));
     928
     978     private int validateSuppressedExceptionsList(List<Throwable> deserSuppressedExceptions)
     979         throws IOException {
     980         if (!Object.class.getModule().
     981             equals(deserSuppressedExceptions.getClass().getModule())) {
     982             throw new StreamCorruptedException("List implementation not in base module.");
     983         } else {
     984             int size = deserSuppressedExceptions.size();
     985             if (size < 0) {
     986                 throw new StreamCorruptedException("Negative list size reported.");
     987             }
     988             return size;
     989         }
     990     }
     991
     992     /**
     993      * Write a {@code Throwable} object to a stream.
     994      *
     995      * A {@code null} stack trace field is represented in the serial
     996      * form as a one-element array whose element is equal to {@code
     997      * new StackTraceElement("", "", null, Integer.MIN_VALUE)}.
     998      *
     999      * @param  s the {@code ObjectOutputStream} to which data is written
    1000      * @throws IOException if an I/O error occurs
    1001      */
    1002     @java.io.Serial
    1003     private synchronized void writeObject(ObjectOutputStream s)
    1004         throws IOException {
    1005         // Ensure that the stackTrace field is initialized to a
    1006         // non-null value, if appropriate.  As of JDK 7, a null stack
    1007         // trace field is a valid value indicating the stack trace
    1008         // should not be set.
    1009         getOurStackTrace();
    1010
    1011         StackTraceElement[] oldStackTrace = stackTrace;
    1012         try {
    1013             if (stackTrace == null)
    1014                 stackTrace = SentinelHolder.STACK_TRACE_SENTINEL;
    1015             s.defaultWriteObject();
    1016         } finally {
    1017             stackTrace = oldStackTrace;
    1018         }
    1019     }
    1020

< prev index next >