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