1 /*
 2  * Copyright (c) 2023, 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
23  * questions.
24  */
25 
26 package jdk.internal.value;
27 
28 import jdk.internal.access.JavaLangAccess;
29 import jdk.internal.access.SharedSecrets;
30 import jdk.internal.misc.Unsafe;
31 import jdk.internal.vm.annotation.IntrinsicCandidate;
32 
33 /**
34  * Utilities to access
35  */
36 public class ValueClass {
37     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
38 
39     /**
40      * Returns true if the given {@code Class} object is implicitly constructible
41      */
42     public static native boolean isImplicitlyConstructible(Class<?> cls);
43 
44     /**
45      * Returns the default value of the given value class type.
46      *
47      * @throws IllegalArgumentException if {@code cls} is not a
48      *         value class type or is not annotated with
49      *         {@link jdk.internal.vm.annotation.ImplicitlyConstructible}
50      */
51     public static <T> T zeroInstance(Class<T> cls) {
52         if (!cls.isValue()) {
53             throw new IllegalArgumentException(cls.getName() + " not a value class");
54         }
55         if (!isImplicitlyConstructible(cls)) {
56             throw new IllegalArgumentException(cls.getName() + " not implicitly constructible");
57         }
58         UNSAFE.ensureClassInitialized(cls);
59         return UNSAFE.uninitializedDefaultValue(cls);
60     }
61 
62     /**
63      * Allocate an array of a value class type with components that behave in
64      * the same way as a {@link jdk.internal.vm.annotation.NullRestricted}
65      * field.
66      * <p>
67      * Because these behaviors are not specified by Java SE, arrays created with
68      * this method should only be used by internal JDK code for experimental
69      * purposes and should not affect user-observable outcomes.
70      *
71      * @throws IllegalArgumentException if {@code componentType} is not a
72      *         value class type or is not annotated with
73      *         {@link jdk.internal.vm.annotation.ImplicitlyConstructible}
74      */
75     @IntrinsicCandidate
76     public static native Object[] newNullRestrictedArray(Class<?> componentType,
77                                                          int length);
78 }