src/java.base/share/classes/java/lang/ClassNotFoundException.java
author darcy
Thu, 29 Aug 2019 16:31:34 -0700
changeset 57956 e0b8b019d2f5
parent 51772 5432cebf6627
child 58288 48e480e56aad
permissions -rw-r--r--
8229997: Apply java.io.Serial annotations in java.base Reviewed-by: alanb, rriggs
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) 1995, 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: 3311
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: 3311
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: 3311
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3311
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3311
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
 * Thrown when an application tries to load in a class through its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * string name using:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <li>The <code>forName</code> method in class <code>Class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <li>The <code>findSystemClass</code> method in class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *     <code>ClassLoader</code> .
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <li>The <code>loadClass</code> method in class <code>ClassLoader</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * but no definition for the class with the specified name could be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>As of release 1.4, this exception has been retrofitted to conform to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the general purpose exception-chaining mechanism.  The "optional exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * that was raised while loading the class" that may be provided at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * construction time and accessed via the {@link #getException()} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * now known as the <i>cause</i>, and may be accessed via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Throwable#getCause()} method, as well as the aforementioned "legacy method."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see     java.lang.Class#forName(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see     java.lang.ClassLoader#findSystemClass(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see     java.lang.ClassLoader#loadClass(java.lang.String, boolean)
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 5506
diff changeset
    56
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
3311
a219a6c036a6 6857789: (reflect) Create common superclass of reflective exceptions
darcy
parents: 2
diff changeset
    58
public class ClassNotFoundException extends ReflectiveOperationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * use serialVersionUID from JDK 1.1.X for interoperability
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51772
diff changeset
    62
     @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     private static final long serialVersionUID = 9176873029745254542L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Constructs a <code>ClassNotFoundException</code> with no detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public ClassNotFoundException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        super((Throwable)null);  // Disallow initCause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Constructs a <code>ClassNotFoundException</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * specified detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param   s   the detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public ClassNotFoundException(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super(s, null);  //  Disallow initCause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Constructs a <code>ClassNotFoundException</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * specified detail message and optional exception that was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * raised while loading the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param s the detail message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param ex the exception that was raised while loading the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public ClassNotFoundException(String s, Throwable ex) {
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
    92
        super(s, ex);  //  Disallow initCause
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Returns the exception that was raised if an error occurred while
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
    97
     * attempting to load the class. Otherwise, returns {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <p>This method predates the general-purpose exception chaining facility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * The {@link Throwable#getCause()} method is now the preferred means of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * obtaining this information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return the <code>Exception</code> that was raised while loading a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public Throwable getException() {
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   107
        return super.getCause();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   111
     * Serializable fields for ClassNotFoundException.
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   112
     *
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   113
     * @serialField ex Throwable
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   114
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51772
diff changeset
   115
    @java.io.Serial
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   116
    private static final ObjectStreamField[] serialPersistentFields = {
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   117
        new ObjectStreamField("ex", Throwable.class)
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
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   120
    /*
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   121
     * Reconstitutes the ClassNotFoundException instance from a stream
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   122
     * 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
   123
     * version.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   125
     * The getException and getCause method returns the private "ex" field
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   126
     * in the older implementation and ClassNotFoundException::cause
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   127
     * was set to null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51772
diff changeset
   129
    @java.io.Serial
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   130
    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   131
        ObjectInputStream.GetField fields = s.readFields();
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   132
        Throwable exception = (Throwable) fields.get("ex", null);
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   133
        if (exception != null) {
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   134
            setCause(exception);
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
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   138
    /*
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   139
     * To maintain compatibility with older implementation, write a serial
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   140
     * "ex" field with the cause as the value.
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   141
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 51772
diff changeset
   142
    @java.io.Serial
51772
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   143
    private void writeObject(ObjectOutputStream out) throws IOException {
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   144
        ObjectOutputStream.PutField fields = out.putFields();
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   145
        fields.put("ex", super.getCause());
5432cebf6627 8210721: Replace legacy serial exception field with Throwable::cause
mchung
parents: 47216
diff changeset
   146
        out.writeFields();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}