1 /*
 2  * Copyright (c) 2022, 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  * @test
26  * @summary Test MethodHandles::zero, MethodHandles::empty and MethodHandles::constant
27  *          on value classes.
28  * @enablePreview
29  * @run junit/othervm -XX:InlineFieldMaxFlatSize=128 MHZeroValue
30  * @run junit/othervm -XX:InlineFieldMaxFlatSize=0 MHZeroValue
31  */
32 
33 import java.lang.invoke.MethodHandles;
34 import java.lang.invoke.MethodType;
35 import java.util.stream.Stream;
36 
37 import static java.lang.invoke.MethodType.*;
38 import jdk.internal.vm.annotation.ImplicitlyConstructible;
39 import jdk.internal.vm.annotation.NullRestricted;
40 import org.junit.jupiter.params.ParameterizedTest;
41 import org.junit.jupiter.params.provider.Arguments;
42 import org.junit.jupiter.params.provider.MethodSource;
43 import static org.junit.jupiter.api.Assertions.*;
44 
45 public class MHZeroValue {
46     @ImplicitlyConstructible
47     static value class V {}
48 
49     @ImplicitlyConstructible
50     static value class P {
51         @NullRestricted
52         V empty;
53         P() {
54             this.empty = new V();
55         }
56     }
57 
58     static Stream<Arguments> defaultValue() {
59         return Stream.of(
60                 // for any type T, default value is always the same as (new T[1])[0]
61                 Arguments.of(int.class,         (new int[1])[0],      0 /* default value */),
62                 Arguments.of(Integer.class,     (new Integer[1])[0],  null),
63                 Arguments.of(P.class,           (new P[1])[0],        null),
64                 Arguments.of(V.class,           (new V[1])[0],        null)
65         );
66     }
67 
68     @ParameterizedTest
69     @MethodSource("defaultValue")
70     public void zero(Class<?> type, Object value, Object expected) throws Throwable {
71         var mh = MethodHandles.zero(type);
72         assertEquals(mh.invoke(), expected);
73         assertEquals(value, expected);
74     }
75 
76     static Stream<Arguments> testCases() {
77         return Stream.of(
78                 Arguments.of(methodType(int.class, int.class, Object.class),     new V(), 0),
79                 Arguments.of(methodType(Integer.class, int.class, Object.class), new P(), null),
80                 Arguments.of(methodType(P.class, int.class, P.class),            new P(), null),
81                 Arguments.of(methodType(V.class, int.class, P.class),            new P(), null),
82                 Arguments.of(methodType(V.class, int.class, V.class),            new V(), null)
83         );
84     }
85 
86     @ParameterizedTest
87     @MethodSource("testCases")
88     public void empty(MethodType mtype, Object param, Object value) throws Throwable {
89         var mh = MethodHandles.empty(mtype);
90         assertEquals(mh.invoke(1, param), value);
91     }
92 }