1 /*
 2  * Copyright (c) 2020, 2024, 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 
24 
25 import jdk.internal.vm.annotation.ImplicitlyConstructible;
26 import jdk.internal.vm.annotation.LooselyConsistentValue;
27 import jdk.test.lib.Asserts;
28 
29 
30 /*
31  * @test
32  * @summary Test JNI IsSameObject semantic with inline types
33  * @library /testlibrary /test/lib
34  * @modules java.base/jdk.internal.vm.annotation
35  * @enablePreview
36  * @compile TestJNIIsSameObject.java
37  * @run main/othervm/native TestJNIIsSameObject
38  */
39 
40 public class TestJNIIsSameObject {
41   @ImplicitlyConstructible
42   @LooselyConsistentValue
43   static value class Value {
44     int i;
45 
46     public Value(int i) {
47       this.i = i;
48     }
49   }
50   native static boolean isSameObject(Object o0, Object o1);
51 
52   static {
53     System.loadLibrary("JNIIsSameObject");
54   }
55 
56   public static void main(String[] args) {
57     // Same value in different instances
58     Value v0 = new Value(3);
59     Value v1 = new Value(3);
60     Asserts.assertTrue(isSameObject(v0, v1));
61 
62     // Different values
63     Value v2 = new Value(4);
64     Asserts.assertFalse(isSameObject(v0, v2));
65 
66     // Same object
67     TestJNIIsSameObject t0 = new TestJNIIsSameObject();
68     Object o = t0;
69     Asserts.assertTrue(isSameObject(t0, o));
70 
71     // Different objects
72     TestJNIIsSameObject t1 = new TestJNIIsSameObject();
73     Asserts.assertFalse(isSameObject(t0, t1));
74 
75     // Comparing against null
76     Asserts.assertFalse(isSameObject(v0, null));
77     Asserts.assertFalse(isSameObject(null, v0));
78     Asserts.assertFalse(isSameObject(t0, null));
79     Asserts.assertFalse(isSameObject(null, t0));
80 
81     // Object vs inline
82     Asserts.assertFalse(isSameObject(v0, t0));
83     Asserts.assertFalse(isSameObject(t0, v0));
84 
85   }
86 }