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 package p; 25 26 import java.lang.invoke.*; 27 import java.lang.reflect.Field; 28 import java.util.Arrays; 29 import java.util.Comparator; 30 import java.util.List; 31 import java.util.stream.Collectors; 32 33 import jdk.incubator.mvt.ValueType; 34 import p.internal.Point; 35 36 public class Main { 37 private static List<String> FIELD_NAMES = List.of("x", "y", "z"); 38 private static List<Class<?>> FIELD_TYPES = List.of(int.class, short.class, short.class); 39 40 public static void main(String... args) throws Exception { 41 ValueType<?> valueType = ValueType.forClass(Point.class); 42 Module module = Point.class.getModule(); 43 assertTrue(module.isNamed(), "unexpected " + module.toString()); 44 assertTrue(valueType.boxClass().getModule() == module, 45 "unexpected " + valueType.boxClass().getModule()); 46 assertTrue(valueType.valueClass().getModule() == module, 47 "unexpected " + valueType.valueClass().getModule() ); 48 49 Field[] fields = Point.class.getDeclaredFields(); 50 assertTrue(fields.length == FIELD_NAMES.size(), Arrays.toString(fields)); 51 52 // validate field names 53 List<String> names = Arrays.stream(fields) 54 .sorted(Comparator.comparing(Field::getName)) 55 .map(Field::getName) 56 .collect(Collectors.toList()); 57 assertTrue(names.equals(FIELD_NAMES), names.toString()); 58 59 // validate field types 60 List<Class<?>> types = Arrays.stream(fields) 61 .sorted(Comparator.comparing(Field::getName)) 62 .map(Field::getType) 63 .collect(Collectors.toList()); 64 assertTrue(types.equals(FIELD_TYPES), types.toString()); 65 66 // getting MethodHandle 67 MethodHandles.Lookup lookup = 68 MethodHandles.privateLookupIn(Point.class, MethodHandles.lookup()); 69 for (Field f : fields) { 70 MethodHandle mh1 = valueType.findGetter(lookup, f.getName(), f.getType()); 71 MethodHandle mh2 = valueType.findWither(lookup, f.getName(), f.getType()); 72 System.out.println(mh1 + " " + mh2); 73 } 74 } 75 76 private static void assertTrue(boolean b, String msg) { 77 if (!b) { 78 throw new RuntimeException(msg); 79 } 80 } 81 }