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