|
1 /* |
|
2 * Copyright (c) 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 * @bug 8046171 |
|
27 * @summary Test access to private static methods between nestmates and nest-host |
|
28 * using different flavours of named nested types using MethodHandles |
|
29 * @run main TestMethodHandles |
|
30 */ |
|
31 |
|
32 |
|
33 import java.lang.invoke.*; |
|
34 import static java.lang.invoke.MethodHandles.*; |
|
35 import static java.lang.invoke.MethodType.*; |
|
36 |
|
37 public class TestMethodHandles { |
|
38 |
|
39 static final MethodType INVOKE_T = MethodType.methodType(void.class); |
|
40 |
|
41 // Private static method of nest-host for nestmates to access |
|
42 private static void priv_static_invoke() { |
|
43 System.out.println("TestMethodHandles::priv_static_invoke"); |
|
44 } |
|
45 |
|
46 // public constructor so we aren't relying on private access |
|
47 public TestMethodHandles() {} |
|
48 |
|
49 // Methods that will access private static methods of nestmates |
|
50 |
|
51 // NOTE: No InnerNested calls in this test because non-static nested types |
|
52 // can't have static methods |
|
53 |
|
54 void access_priv(TestMethodHandles o) throws Throwable { |
|
55 MethodHandle mh = |
|
56 lookup().findStatic(o.getClass(), "priv_static_invoke", INVOKE_T); |
|
57 mh.invoke(); |
|
58 mh.invokeExact(); |
|
59 } |
|
60 void access_priv(StaticNested o) throws Throwable { |
|
61 MethodHandle mh = |
|
62 lookup().findStatic(o.getClass(), "priv_static_invoke", INVOKE_T); |
|
63 mh.invoke(); |
|
64 mh.invokeExact(); |
|
65 } |
|
66 void access_priv(StaticIface o) throws Throwable { |
|
67 MethodHandle mh = |
|
68 lookup().findStatic(StaticIface.class, "priv_static_invoke", INVOKE_T); |
|
69 mh.invoke(); |
|
70 mh.invokeExact(); |
|
71 } |
|
72 |
|
73 // The various nestmates |
|
74 |
|
75 static interface StaticIface { |
|
76 |
|
77 private static void priv_static_invoke() { |
|
78 System.out.println("StaticIface::priv_static_invoke"); |
|
79 } |
|
80 |
|
81 // Methods that will access private static methods of nestmates |
|
82 |
|
83 default void access_priv(TestMethodHandles o) throws Throwable { |
|
84 MethodHandle mh = |
|
85 lookup().findStatic(o.getClass(), "priv_static_invoke", INVOKE_T); |
|
86 mh.invoke(); |
|
87 mh.invokeExact(); |
|
88 } |
|
89 default void access_priv(StaticNested o) throws Throwable { |
|
90 MethodHandle mh = |
|
91 lookup().findStatic(o.getClass(), "priv_static_invoke", INVOKE_T); |
|
92 mh.invoke(); |
|
93 mh.invokeExact(); |
|
94 } |
|
95 default void access_priv(StaticIface o) throws Throwable { |
|
96 MethodHandle mh = |
|
97 lookup().findStatic(StaticIface.class, "priv_static_invoke", INVOKE_T); |
|
98 mh.invoke(); |
|
99 mh.invokeExact(); |
|
100 } |
|
101 } |
|
102 |
|
103 static class StaticNested { |
|
104 |
|
105 private static void priv_static_invoke() { |
|
106 System.out.println("StaticNested::priv_static_invoke"); |
|
107 } |
|
108 |
|
109 // public constructor so we aren't relying on private access |
|
110 public StaticNested() {} |
|
111 |
|
112 // Methods that will access private static methods of nestmates |
|
113 |
|
114 void access_priv(TestMethodHandles o) throws Throwable { |
|
115 MethodHandle mh = |
|
116 lookup().findStatic(o.getClass(), "priv_static_invoke", INVOKE_T); |
|
117 mh.invoke(); |
|
118 mh.invokeExact(); |
|
119 } |
|
120 void access_priv(StaticNested o) throws Throwable { |
|
121 MethodHandle mh = |
|
122 lookup().findStatic(o.getClass(), "priv_static_invoke", INVOKE_T); |
|
123 mh.invoke(); |
|
124 mh.invokeExact(); |
|
125 } |
|
126 void access_priv(StaticIface o) throws Throwable { |
|
127 MethodHandle mh = |
|
128 lookup().findStatic(StaticIface.class, "priv_static_invoke", INVOKE_T); |
|
129 mh.invoke(); |
|
130 mh.invokeExact(); |
|
131 } |
|
132 } |
|
133 |
|
134 class InnerNested { |
|
135 |
|
136 // public constructor so we aren't relying on private access |
|
137 public InnerNested() {} |
|
138 |
|
139 void access_priv(TestMethodHandles o) throws Throwable { |
|
140 MethodHandle mh = |
|
141 lookup().findStatic(o.getClass(), "priv_static_invoke", INVOKE_T); |
|
142 mh.invoke(); |
|
143 mh.invokeExact(); |
|
144 } |
|
145 void access_priv(StaticNested o) throws Throwable { |
|
146 MethodHandle mh = |
|
147 lookup().findStatic(o.getClass(), "priv_static_invoke", INVOKE_T); |
|
148 mh.invoke(); |
|
149 mh.invokeExact(); |
|
150 } |
|
151 void access_priv(StaticIface o) throws Throwable { |
|
152 MethodHandle mh = |
|
153 lookup().findStatic(StaticIface.class, "priv_static_invoke", INVOKE_T); |
|
154 mh.invoke(); |
|
155 mh.invokeExact(); |
|
156 } |
|
157 } |
|
158 |
|
159 public static void main(String[] args) throws Throwable { |
|
160 TestMethodHandles o = new TestMethodHandles(); |
|
161 StaticNested s = new StaticNested(); |
|
162 InnerNested i = o.new InnerNested(); |
|
163 StaticIface intf = new StaticIface() {}; |
|
164 |
|
165 o.access_priv(new TestMethodHandles()); |
|
166 o.access_priv(s); |
|
167 o.access_priv(intf); |
|
168 |
|
169 s.access_priv(o); |
|
170 s.access_priv(new StaticNested()); |
|
171 s.access_priv(intf); |
|
172 |
|
173 i.access_priv(o); |
|
174 i.access_priv(s); |
|
175 i.access_priv(intf); |
|
176 |
|
177 intf.access_priv(o); |
|
178 intf.access_priv(s); |
|
179 intf.access_priv(new StaticIface(){}); |
|
180 } |
|
181 } |