blob: 3365bd923bcc30e9f2ededca44f9883613a3edf1 [file] [log] [blame]
Paul Sandoz7dd146f2016-06-29 08:30:49 +02001/*
Paul Sandoz94d019e2017-01-04 17:20:41 -08002 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
Paul Sandoz7dd146f2016-06-29 08:30:49 +02003 * 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
Paul Sandoz94d019e2017-01-04 17:20:41 -080027 * @modules java.base/java.lang.invoke:open
Paul Sandoz7dd146f2016-06-29 08:30:49 +020028 */
29
30import org.testng.annotations.DataProvider;
31import org.testng.annotations.Test;
32
33import java.lang.invoke.VarHandle;
Paul Sandoz94d019e2017-01-04 17:20:41 -080034import java.lang.reflect.Field;
35import java.lang.reflect.Method;
Paul Sandoz7dd146f2016-06-29 08:30:49 +020036import 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
Paul Sandoz7dd146f2016-06-29 08:30:49 +020049 @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 }
Paul Sandoz94d019e2017-01-04 17:20:41 -080063
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 }
Paul Sandoz7dd146f2016-06-29 08:30:49 +020081}