1 /*
 2  * Copyright (c) 2022, 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.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package java.lang;
24 
25 
26 import jdk.internal.javac.PreviewFeature;
27 
28 /**
29  * Thrown when an identity object is required but a value object is supplied.
30  * <p>
31  * Identity objects are required for synchronization and locking.
32  * <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">Value-based</a>
33  * objects do not have identity and cannot be used for synchronization, locking,
34  * or any type of {@link java.lang.ref.Reference}.
35  *
36  * @since Valhalla
37  */
38 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
39 public class IdentityException extends RuntimeException {
40     @java.io.Serial
41     private static final long serialVersionUID = 1L;
42 
43     /**
44      * Create an {@code IdentityException} with no message.
45      */
46     public IdentityException() {
47     }
48 
49     /**
50      * Create an {@code IdentityException} with the class name and default message.
51      *
52      * @param clazz the class of the object
53      */
54     public IdentityException(Class<?> clazz) {
55         super(clazz.getName() + " is not an identity class");
56     }
57 
58     /**
59      * Create an {@code IdentityException} with a message.
60      *
61      * @param  message the detail message; can be {@code null}
62      */
63     public IdentityException(String message) {
64         super(message);
65     }
66 
67     /**
68      * Create an {@code IdentityException} with a cause.
69      *
70      * @param  cause the cause; {@code null} is permitted, and indicates
71      *               that the cause is nonexistent or unknown.
72      */
73     public IdentityException(Throwable cause) {
74         super(cause);
75     }
76 
77     /**
78      * Create an {@code IdentityException} with a message and cause.
79      *
80      * @param  message the detail message; can be {@code null}
81      * @param  cause the cause; {@code null} is permitted, and indicates
82      *               that the cause is nonexistent or unknown.
83      */
84     public IdentityException(String message, Throwable cause) {
85         super(message, cause);
86     }
87 }