1/*
2 * Copyright (c) 2014, 2015, 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 VarHandleTestReflection
27 */
28
29import org.testng.annotations.DataProvider;
30import org.testng.annotations.Test;
31
32import java.lang.invoke.MethodHandle;
33import java.lang.invoke.MethodHandleInfo;
34import java.lang.invoke.MethodHandles;
35import java.lang.invoke.VarHandle;
36import java.lang.reflect.Method;
37import java.util.stream.Stream;
38
39public class VarHandleTestReflection extends VarHandleBaseTest {
40    String string;
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    static VarHandle handle() throws Exception {
50        return MethodHandles.lookup().
51                findVarHandle(VarHandleTestReflection.class, "string", String.class);
52    }
53
54    @Test(dataProvider = "accessModesProvider", expectedExceptions = IllegalArgumentException.class)
55    public void methodInvocation(VarHandle.AccessMode accessMode) throws Exception {
56        VarHandle v = handle();
57
58        // Try a reflective invoke using a Method
59
60        Method vhm = VarHandle.class.getMethod(accessMode.methodName(), Object[].class);
61        vhm.invoke(v, new Object[]{});
62    }
63
64    @Test(dataProvider = "accessModesProvider", expectedExceptions = UnsupportedOperationException.class)
65    public void methodHandleInvoke(VarHandle.AccessMode accessMode) throws Throwable {
66        VarHandle v = handle();
67
68        // Try a reflective invoke using a MethodHandle
69
70        MethodHandle mh = MethodHandles.lookup().unreflect(
71                VarHandle.class.getMethod(accessMode.methodName(), Object[].class));
72        // Use invoke to avoid WrongMethodTypeException for
73        // non-signature-polymorphic return types
74        Object o = (Object) mh.invoke(v, new Object[]{});
75    }
76
77    @Test(dataProvider = "accessModesProvider", expectedExceptions = IllegalArgumentException.class)
78    public void methodInvocationFromMethodInfo(VarHandle.AccessMode accessMode) throws Exception {
79        VarHandle v = handle();
80
81        // Try a reflective invoke using a Method obtained from cracking
82        // a MethodHandle
83
84        MethodHandle mh = MethodHandles.lookup().unreflect(
85                VarHandle.class.getMethod(accessMode.methodName(), Object[].class));
86        MethodHandleInfo info = MethodHandles.lookup().revealDirect(mh);
87        Method im = info.reflectAs(Method.class, MethodHandles.lookup());
88        im.invoke(v, new Object[]{});
89    }
90
91    @Test(dataProvider = "accessModesProvider", expectedExceptions = IllegalArgumentException.class)
92    public void reflectAsFromVarHandleInvoker(VarHandle.AccessMode accessMode) throws Exception {
93        VarHandle v = handle();
94
95        MethodHandle mh = MethodHandles.varHandleInvoker(
96                accessMode, v.accessModeType(accessMode));
97
98        MethodHandleInfo info = MethodHandles.lookup().revealDirect(mh);
99
100        info.reflectAs(Method.class, MethodHandles.lookup());
101    }
102
103    @Test(dataProvider = "accessModesProvider", expectedExceptions = IllegalArgumentException.class)
104    public void reflectAsFromFindVirtual(VarHandle.AccessMode accessMode) throws Exception {
105        VarHandle v = handle();
106
107        MethodHandle mh = MethodHandles.publicLookup().findVirtual(
108                VarHandle.class, accessMode.methodName(), v.accessModeType(accessMode));
109
110        MethodHandleInfo info = MethodHandles.lookup().revealDirect(mh);
111
112        info.reflectAs(Method.class, MethodHandles.lookup());
113    }
114}
115