1 /*
  2  * Copyright (c) 2017, 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 /*
 26  * @test
 27  * @summary test core reflection on value classes
 28  * @enablePreview
 29  * @run junit/othervm Reflection
 30  */
 31 
 32 import java.lang.reflect.Array;
 33 import java.lang.reflect.Constructor;
 34 import java.lang.reflect.Field;
 35 import java.lang.reflect.InvocationTargetException;
 36 import java.lang.reflect.Method;
 37 import java.util.Arrays;
 38 import java.util.stream.Stream;
 39 
 40 import jdk.internal.value.ValueClass;
 41 import jdk.internal.vm.annotation.ImplicitlyConstructible;
 42 import jdk.internal.vm.annotation.NullRestricted;
 43 import org.junit.jupiter.api.Test;
 44 import org.junit.jupiter.params.ParameterizedTest;
 45 import org.junit.jupiter.params.provider.Arguments;
 46 import org.junit.jupiter.params.provider.MethodSource;
 47 import static org.junit.jupiter.api.Assertions.*;
 48 
 49 public class Reflection {
 50     @ImplicitlyConstructible
 51     static value class V {
 52         int x;
 53         V(int x) {
 54             this.x = x;
 55         }
 56     }
 57 
 58     @ImplicitlyConstructible
 59     static value class Value {
 60         @NullRestricted
 61         V v1;
 62         V v2;
 63         Value(V v1, V v2) {
 64             this.v1 = v1;
 65             this.v2 = v2;
 66         }
 67 
 68         static Value newValue(V v1, V v2) {
 69             return new Value(v1, v2);
 70         }
 71     }
 72 
 73     @Test
 74     void testNewInstance() throws Exception {
 75         V v = new V(10);
 76         Constructor<Value> ctor = Value.class.getDeclaredConstructor(V.class, V.class);
 77         Value o = ctor.newInstance(v, v);
 78         assertEquals(o.getClass(), Value.class);
 79     }
 80 
 81     @Test
 82     void testAccess() throws Exception {
 83         Field field = Value.class.getDeclaredField("v1");
 84         V v = new V(10);
 85         Value o = new Value(v, null);
 86         assertEquals(v, field.get(o));
 87 
 88         // accessible but no write access
 89         assertTrue(field.trySetAccessible());
 90         assertTrue(field.isAccessible());
 91         assertThrows(IllegalAccessException.class, () -> field.set(o, v));
 92     }
 93 
 94     @Test
 95     void testNullRestricted() throws Exception {
 96         Method m = Value.class.getDeclaredMethod("newValue", V.class, V.class);
 97         Throwable t = assertThrows(InvocationTargetException.class, () -> m.invoke(null, new Object[] {null, null}));
 98         assertEquals(NullPointerException.class, t.getCause().getClass());
 99     }
100 
101     static Stream<Arguments> arrays() {
102         V v1 = new V(10);
103         V v2 = new V(20);
104         Value value = new Value(v1, v2);
105 
106         V[] varray = (V[]) Array.newInstance(V.class, 2);
107         V[] varrayNR = (V[]) ValueClass.newNullRestrictedArray(V.class, 3);
108         Value[] valuearray = (Value[]) Array.newInstance(Value.class, 2);
109         Value[] valuearrayNR = (Value[]) ValueClass.newNullRestrictedArray(Value.class, 3);
110 
111         return Stream.of(
112                 Arguments.of(V[].class, varray, false, v1),
113                 Arguments.of(V[].class, varrayNR, true, v2),
114                 Arguments.of(Value[].class, valuearray, false, value),
115                 Arguments.of(Value[].class, valuearrayNR, true, value)
116 
117         );
118     }
119 
120     /**
121      * Setting the elements of an array.
122      * NPE will be thrown if null is set on an element in a null-restricted value class array
123      */
124     @ParameterizedTest
125     @MethodSource("arrays")
126     public void testArrays(Class<?> arrayClass, Object[] array, boolean nullRestricted, Object element) {
127         Class<?> componentType = arrayClass.getComponentType();
128         assertTrue(arrayClass.isArray());
129         // TODO: check Array.getComponentType(array) instead
130         assertTrue(array.getClass() == arrayClass || nullRestricted);
131         assertTrue(array.getClass().getComponentType() == componentType || nullRestricted);
132 
133         for (int i = 0; i < array.length; i++) {
134             Object o = Array.get(array, i);
135             if (nullRestricted) {
136                 assertTrue(o == ValueClass.zeroInstance(componentType));
137             } else {
138                 assertTrue(o == null);
139             }
140         }
141 
142         // set elements
143         for (int i = 0; i < array.length; i++) {
144             Array.set(array, i, element);
145             assertTrue(Array.get(array, i) == element);
146         }
147 
148         Arrays.setAll(array, i -> array[i]);
149 
150         // test nullable
151         for (int i = 0; i < array.length; i++) {
152             if (nullRestricted) {
153                 final int index = i;
154                 assertThrows(NullPointerException.class, () -> Array.set(array, index, null));
155             } else {
156                 Array.set(array, i, null);
157             }
158         }
159     }
160 }