VarHandleTestAccessModeMethodNames.java revision 16410:2866f5e1d57b
1/* 2 * Copyright (c) 2016, 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 VarHandleTestAccessModeMethodNames 27 * @modules java.base/java.lang.invoke:open 28 */ 29 30import org.testng.annotations.DataProvider; 31import org.testng.annotations.Test; 32 33import java.lang.invoke.VarHandle; 34import java.lang.reflect.Field; 35import java.lang.reflect.Method; 36import java.util.stream.Stream; 37 38import static org.testng.Assert.assertEquals; 39 40public class VarHandleTestAccessModeMethodNames { 41 42 @DataProvider 43 public static Object[][] accessModesProvider() { 44 return Stream.of(VarHandle.AccessMode.values()). 45 map(am -> new Object[]{am}). 46 toArray(Object[][]::new); 47 } 48 49 @Test(dataProvider = "accessModesProvider") 50 public void testMethodName(VarHandle.AccessMode am) { 51 assertEquals(am.methodName(), toMethodName(am.name())); 52 } 53 54 private static String toMethodName(String name) { 55 StringBuilder s = new StringBuilder(name.toLowerCase()); 56 int i; 57 while ((i = s.indexOf("_")) != -1) { 58 s.deleteCharAt(i); 59 s.setCharAt(i, Character.toUpperCase(s.charAt(i))); 60 } 61 return s.toString(); 62 } 63 64 65 @Test(dataProvider = "accessModesProvider") 66 public void testReturnType(VarHandle.AccessMode am) throws Exception { 67 assertEquals(getReturnType(am.methodName()), getAccessModeReturnType(am)); 68 } 69 70 private static Class<?> getReturnType(String name) throws Exception { 71 return VarHandle.class.getMethod(name, Object[].class).getReturnType(); 72 } 73 74 private static Class<?> getAccessModeReturnType(VarHandle.AccessMode am) throws Exception { 75 Field field_am_at = VarHandle.AccessMode.class.getDeclaredField("at"); 76 field_am_at.setAccessible(true); 77 Field field_at_returnType = field_am_at.getType().getDeclaredField("returnType"); 78 field_at_returnType.setAccessible(true); 79 return (Class<?>) field_at_returnType.get(field_am_at.get(am)); 80 } 81} 82