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 // Android-added: package for test. 25 package test.java.lang.invoke.VarHandles; 26 27 /* 28 * @test 29 * @run testng VarHandleTestAccessModeMethodNames 30 * @modules java.base/java.lang.invoke:open 31 */ 32 33 import org.testng.annotations.DataProvider; 34 import org.testng.annotations.Test; 35 36 import java.lang.invoke.VarHandle; 37 import java.lang.reflect.Field; 38 import java.lang.reflect.Method; 39 import java.util.stream.Stream; 40 41 import static org.testng.Assert.assertEquals; 42 43 public class VarHandleTestAccessModeMethodNames { 44 45 @DataProvider accessModesProvider()46 public static Object[][] accessModesProvider() { 47 return Stream.of(VarHandle.AccessMode.values()). 48 map(am -> new Object[]{am}). 49 toArray(Object[][]::new); 50 } 51 52 @Test(dataProvider = "accessModesProvider") testMethodName(VarHandle.AccessMode am)53 public void testMethodName(VarHandle.AccessMode am) { 54 assertEquals(am.methodName(), toMethodName(am.name())); 55 } 56 toMethodName(String name)57 private static String toMethodName(String name) { 58 StringBuilder s = new StringBuilder(name.toLowerCase()); 59 int i; 60 while ((i = s.indexOf("_")) != -1) { 61 s.deleteCharAt(i); 62 s.setCharAt(i, Character.toUpperCase(s.charAt(i))); 63 } 64 return s.toString(); 65 } 66 67 // BEGIN Android-removed: test for AccessMode return type (not present on Android). 68 // @Test(dataProvider = "accessModesProvider") 69 // public void testReturnType(VarHandle.AccessMode am) throws Exception { 70 // assertEquals(getReturnType(am.methodName()), getAccessModeReturnType(am)); 71 // } 72 // 73 // private static Class<?> getReturnType(String name) throws Exception { 74 // return VarHandle.class.getMethod(name, Object[].class).getReturnType(); 75 // } 76 // 77 // private static Class<?> getAccessModeReturnType(VarHandle.AccessMode am) throws Exception { 78 // Field field_am_at = VarHandle.AccessMode.class.getDeclaredField("at"); 79 // field_am_at.setAccessible(true); 80 // Field field_at_returnType = field_am_at.getType().getDeclaredField("returnType"); 81 // field_at_returnType.setAccessible(true); 82 // return (Class<?>) field_at_returnType.get(field_am_at.get(am)); 83 // } 84 // END Android-removed: test for AccessMode return type (not present on Android). */ 85 86 // BEGIN Android-added: valueFromMethodName test (b/203822312) 87 @Test(dataProvider = "accessModesProvider") testValueFromMethodName(VarHandle.AccessMode am)88 public void testValueFromMethodName(VarHandle.AccessMode am) throws Exception { 89 VarHandle.AccessMode other = VarHandle.AccessMode.valueFromMethodName(am.methodName()); 90 assertEquals(am, other); 91 } 92 // END Android-added: valueFromMethodName test (b/203822312) 93 } 94