1 /* 2 * Copyright (c) 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 import java.util.Map; 26 import java.util.Set; 27 import java.util.stream.Collectors; 28 29 import jdk.internal.value.LayoutIteration; 30 import jdk.internal.vm.annotation.LooselyConsistentValue; 31 import jdk.internal.vm.annotation.NullRestricted; 32 import org.junit.jupiter.api.Assertions; 33 import org.junit.jupiter.api.Test; 34 35 import static org.junit.jupiter.api.Assertions.assertEquals; 36 37 /* 38 * @test 39 * @summary test LayoutIteration 40 * @enablePreview 41 * @modules java.base/jdk.internal.vm.annotation 42 * java.base/jdk.internal.value 43 * @run junit/othervm LayoutIterationTest 44 */ 45 class LayoutIterationTest { 46 47 @LooselyConsistentValue 48 static value class One { 49 int a; 50 short b; 51 52 One(int a, short b) { 53 this.a = a; 54 this.b = b; 55 super(); 56 } 57 } 58 59 @LooselyConsistentValue 60 static value class Two { 61 @NullRestricted 62 One one = new One(5, (short) 3); 63 One anotherOne = new One(4, (short) 2); 64 long l = 5L; 65 } 66 67 @Test 68 void test() { 69 Two t = new Two(); 70 Set<Class<?>> classes = LayoutIteration.computeElementGetters(One.class).stream() 71 .map(mh -> mh.type().returnType()).collect(Collectors.toSet()); 72 assertEquals(Set.of(int.class, short.class), classes); 73 Map<Class<?>, Object> values = LayoutIteration.computeElementGetters(Two.class).stream() 74 .collect(Collectors.toMap(mh -> mh.type().returnType(), mh -> { 75 try { 76 return (Object) mh.invoke(t); 77 } catch (Throwable ex) { 78 return Assertions.fail(ex); 79 } 80 })); 81 assertEquals(Map.of( 82 int.class, t.one.a, 83 short.class, t.one.b, 84 One.class, t.anotherOne, 85 long.class, t.l 86 ), values); 87 } 88 }