1 /* 2 * Copyright (c) 2017, 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 * @run testng/othervm -XX:+EnableMVT -Dvalhalla.enablePoolPatches=true UnreflectWithersTest 27 */ 28 29 import jdk.experimental.value.ValueType; 30 import jvm.internal.value.ValueCapableClass; 31 import org.testng.annotations.Test; 32 33 import java.lang.invoke.MethodHandle; 34 import java.lang.invoke.MethodHandles; 35 import java.lang.reflect.Field; 36 import java.util.Arrays; 37 import java.util.Comparator; 38 39 import static org.testng.Assert.assertEquals; 40 41 @Test 42 public class UnreflectWithersTest { 43 44 @ValueCapableClass 45 static final class H { 46 final boolean f1; 47 final byte f2; 48 final short f3; 49 final char f4; 50 final int f5; 51 final long f6; 52 final float f7; 53 final double f8; 54 final Object f9; 55 56 public H(boolean f1, byte f2, short f3, char f4, int f5, long f6, float f7, double f8, Object f9) { 57 this.f1 = f1; 58 this.f2 = f2; 59 this.f3 = f3; 60 this.f4 = f4; 61 this.f5 = f5; 62 this.f6 = f6; 63 this.f7 = f7; 64 this.f8 = f8; 65 this.f9 = f9; 66 } 67 } 68 69 static final ValueType<?> VT_H = ValueType.forClass(H.class); 70 71 static final MethodHandle VT_H_fromDefault; 72 73 static final MethodHandle VT_H_withArg; 74 75 static { 76 try { 77 MethodHandles.Lookup l = MethodHandles.privateLookupIn(VT_H.boxClass(), MethodHandles.lookup()); 78 79 Field[] fs = VT_H.valueFields(); 80 Arrays.sort(fs, Comparator.comparing(Field::getName)); 81 82 MethodHandle mh = VT_H.unreflectWithers(l, true, VT_H.valueFields()); 83 VT_H_fromDefault = MethodHandles.filterReturnValue(mh, VT_H.box()); 84 85 mh = VT_H.unreflectWithers(l, false, VT_H.valueFields()); 86 VT_H_withArg = MethodHandles.filterReturnValue(mh, VT_H.box()); 87 } 88 catch (Exception e) { 89 throw new InternalError(e); 90 } 91 } 92 93 public void testHFromDefault() throws Throwable { 94 H h = (H) VT_H_fromDefault.invoke(true, (byte)1, (short)2, (char)3, 4, 5L, 6.0f, 7.0d, "8"); 95 assertEquals(h.f1, true); 96 assertEquals(h.f2, (byte)1); 97 assertEquals(h.f3, (short)2); 98 assertEquals(h.f4, (char)3); 99 assertEquals(h.f5, 4); 100 assertEquals(h.f6, 5L); 101 assertEquals(h.f7, 6.0f); 102 assertEquals(h.f8, 7.0d); 103 assertEquals(h.f9, "8"); 104 } 105 106 public void testHWithArgumentDefault() throws Throwable { 107 MethodHandle mh = MethodHandles.collectArguments(VT_H_withArg, 0, VT_H.defaultValueConstant()); 108 109 H h = (H) mh.invoke(true, (byte)1, (short)2, (char)3, 4, 5L, 6.0f, 7.0d, "8"); 110 assertEquals(h.f1, true); 111 assertEquals(h.f2, (byte)1); 112 assertEquals(h.f3, (short)2); 113 assertEquals(h.f4, (char)3); 114 assertEquals(h.f5, 4); 115 assertEquals(h.f6, 5L); 116 assertEquals(h.f7, 6.0f); 117 assertEquals(h.f8, 7.0d); 118 assertEquals(h.f9, "8"); 119 } 120 } 121 122