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