src/java.base/share/classes/java/lang/ExceptionInInitializerError.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51772
diff changeset
     2
 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
    28
import java.io.IOException;
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
    29
import java.io.ObjectInputStream;
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
    30
import java.io.ObjectOutputStream;
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
    31
import java.io.ObjectStreamField;
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Signals that an unexpected exception has occurred in a static initializer.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    35
 * An {@code ExceptionInInitializerError} is thrown to indicate that an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * exception occurred during evaluation of a static initializer or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * initializer for a static variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>As of release 1.4, this exception has been retrofitted to conform to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the general purpose exception-chaining mechanism.  The "saved throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * object" that may be provided at construction time and accessed via
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the {@link #getException()} method is now known as the <i>cause</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * and may be accessed via the {@link Throwable#getCause()} method, as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * as the aforementioned "legacy method."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author  Frank Yellin
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 5506
diff changeset
    47
 * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class ExceptionInInitializerError extends LinkageError {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Use serialVersionUID from JDK 1.1.X for interoperability
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51772
diff changeset
    53
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final long serialVersionUID = 1521711792217232256L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    57
     * Constructs an {@code ExceptionInInitializerError} with
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    58
     * {@code null} as its detail message string and with no saved
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * throwable object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * A detail message is a String that describes this particular exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public ExceptionInInitializerError() {
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
    63
        initCause(null); // Disallow subsequent initCause
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    67
     * Constructs a new {@code ExceptionInInitializerError} class by
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    68
     * saving a reference to the {@code Throwable} object thrown for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * later retrieval by the {@link #getException()} method. The detail
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    70
     * message string is set to {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param thrown The exception thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public ExceptionInInitializerError(Throwable thrown) {
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
    75
        super(null, thrown); // Disallow subsequent initCause
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
    79
     * Constructs an {@code ExceptionInInitializerError} with the specified detail
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * message string.  A detail message is a String that describes this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * particular exception. The detail message string is saved for later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * retrieval by the {@link Throwable#getMessage()} method. There is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * saved throwable object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param s the detail message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public ExceptionInInitializerError(String s) {
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
    88
        super(s, null);  // Disallow subsequent initCause
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Returns the exception that occurred during a static initialization that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * caused this error to be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>This method predates the general-purpose exception chaining facility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * The {@link Throwable#getCause()} method is now the preferred means of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * obtaining this information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @return the saved throwable object of this
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   100
     *         {@code ExceptionInInitializerError}, or {@code null}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   101
     *         if this {@code ExceptionInInitializerError} has no saved
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *         throwable object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public Throwable getException() {
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   105
        return super.getCause();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   109
     * Serializable fields for ExceptionInInitializerError.
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   110
     *
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   111
     * @serialField exception Throwable
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   112
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51772
diff changeset
   113
    @java.io.Serial
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   114
    private static final ObjectStreamField[] serialPersistentFields = {
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   115
        new ObjectStreamField("exception", Throwable.class)
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   116
    };
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   117
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   118
    /*
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   119
     * Reconstitutes the ExceptionInInitializerError instance from a stream
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   120
     * and initialize the cause properly when deserializing from an older
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   121
     * version.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   123
     * The getException and getCause method returns the private "exception"
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   124
     * field in the older implementation and ExceptionInInitializerError::cause
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   125
     * was set to null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51772
diff changeset
   127
    @java.io.Serial
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   128
    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   129
        ObjectInputStream.GetField fields = s.readFields();
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   130
        Throwable exception = (Throwable) fields.get("exception", null);
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   131
        if (exception != null) {
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   132
            setCause(exception);
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   133
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   135
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   136
    /*
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   137
     * To maintain compatibility with older implementation, write a serial
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   138
     * "exception" field with the cause as the value.
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   139
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51772
diff changeset
   140
    @java.io.Serial
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   141
    private void writeObject(ObjectOutputStream out) throws IOException {
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   142
        ObjectOutputStream.PutField fields = out.putFields();
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   143
        fields.put("exception", super.getCause());
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   144
        out.writeFields();
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   145
    }
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   146
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}