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 M_T = MethodType.methodType(void.class); 40 41 // Private method of nest-top for nestmates to access 42 private void priv_invoke() { 43 System.out.println("TestMethodHandles::priv_invoke"); 44 } 45 46 // public constructor so we aren't relying on private access 47 public TestMethodHandles() {} 48 49 // Methods that will access private methods of nestmates 50 51 void access_priv(TestMethodHandles o) throws Throwable { 52 MethodHandle mh = 53 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 54 mh.invoke(o); 55 mh.invokeExact(o); 56 checkBadInvoke(mh, new StaticNested()); // wrong nestmate 57 checkBadInvoke(mh, mh); // completely wrong type 58 } 59 void access_priv(InnerNested o) throws Throwable { 60 MethodHandle mh = 61 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 62 mh.invoke(o); 63 mh.invokeExact(o); 64 checkBadInvoke(mh, this); // wrong nestmate 65 checkBadInvoke(mh, mh); // completely wrong type 66 } 67 void access_priv(StaticNested o) throws Throwable { 68 MethodHandle mh = 69 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 70 mh.invoke(o); 71 mh.invokeExact(o); 72 checkBadInvoke(mh, this); // wrong nestmate 73 checkBadInvoke(mh, mh); // completely wrong type 74 } 75 void access_priv(StaticIface o) throws Throwable { 76 MethodHandle mh = 77 lookup().findSpecial(StaticIface.class, "priv_invoke", M_T, this.getClass()); 78 mh.invoke(o); 79 mh.invokeExact(o); 80 checkBadInvoke(mh, this); // wrong nestmate 81 checkBadInvoke(mh, mh); // completely wrong type 82 } 83 84 // The various nestmates 85 86 static interface StaticIface { 87 88 private void priv_invoke() { 89 System.out.println("StaticIface::priv_invoke"); 90 } 91 92 // Methods that will access private methods of nestmates 93 94 default void access_priv(TestMethodHandles o) throws Throwable { 95 MethodHandle mh = 96 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, StaticIface.class); 97 mh.invoke(o); 98 mh.invokeExact(o); 99 checkBadInvoke(mh, this); // wrong nestmate 100 checkBadInvoke(mh, mh); // completely wrong type 101 } 102 default void access_priv(InnerNested o) throws Throwable { 103 MethodHandle mh = 104 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, StaticIface.class); 105 mh.invoke(o); 106 mh.invokeExact(o); 107 checkBadInvoke(mh, this); // wrong nestmate 108 checkBadInvoke(mh, mh); // completely wrong type 109 } 110 default void access_priv(StaticNested o) throws Throwable { 111 MethodHandle mh = 112 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, StaticIface.class); 113 mh.invoke(o); 114 mh.invokeExact(o); 115 checkBadInvoke(mh, this); // wrong nestmate 116 checkBadInvoke(mh, mh); // completely wrong type 117 } 118 default void access_priv(StaticIface o) throws Throwable { 119 MethodHandle mh = 120 lookup().findSpecial(StaticIface.class, "priv_invoke", M_T, StaticIface.class); 121 mh.invoke(o); 122 mh.invokeExact(o); 123 checkBadInvoke(mh, new StaticNested()); // wrong nestmate 124 checkBadInvoke(mh, mh); // completely wrong type 125 } 126 } 127 128 static class StaticNested { 129 130 private void priv_invoke() { 131 System.out.println("StaticNested::priv_invoke"); 132 } 133 134 // public constructor so we aren't relying on private access 135 public StaticNested() {} 136 137 // Methods that will access private methods of nestmates 138 139 void access_priv(TestMethodHandles o) throws Throwable { 140 MethodHandle mh = 141 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 142 mh.invoke(o); 143 mh.invokeExact(o); 144 checkBadInvoke(mh, this); // wrong nestmate 145 checkBadInvoke(mh, mh); // completely wrong type 146 } 147 void access_priv(InnerNested o) throws Throwable { 148 MethodHandle mh = 149 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 150 mh.invoke(o); 151 mh.invokeExact(o); 152 checkBadInvoke(mh, this); // wrong nestmate 153 checkBadInvoke(mh, mh); // completely wrong type 154 } 155 void access_priv(StaticNested o) throws Throwable { 156 MethodHandle mh = 157 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 158 mh.invoke(o); 159 mh.invokeExact(o); 160 checkBadInvoke(mh, new TestMethodHandles()); // wrong nestmate 161 checkBadInvoke(mh, mh); // completely wrong type 162 } 163 void access_priv(StaticIface o) throws Throwable { 164 MethodHandle mh = 165 lookup().findSpecial(StaticIface.class, "priv_invoke", M_T, this.getClass()); 166 mh.invoke(o); 167 mh.invokeExact(o); 168 checkBadInvoke(mh, this); // wrong nestmate 169 checkBadInvoke(mh, mh); // completely wrong type 170 } 171 } 172 173 class InnerNested { 174 175 private void priv_invoke() { 176 System.out.println("InnerNested::priv_invoke"); 177 } 178 179 // public constructor so we aren't relying on private access 180 public InnerNested() {} 181 182 void access_priv(TestMethodHandles o) throws Throwable { 183 MethodHandle mh = 184 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 185 mh.invoke(o); 186 mh.invokeExact(o); 187 checkBadInvoke(mh, this); // wrong nestmate 188 checkBadInvoke(mh, mh); // completely wrong type 189 } 190 void access_priv(InnerNested o) throws Throwable { 191 MethodHandle mh = 192 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 193 mh.invoke(o); 194 mh.invokeExact(o); 195 checkBadInvoke(mh, new StaticNested()); // wrong nestmate 196 checkBadInvoke(mh, mh); // completely wrong type 197 } 198 void access_priv(StaticNested o) throws Throwable { 199 MethodHandle mh = 200 lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 201 mh.invoke(o); 202 mh.invokeExact(o); 203 checkBadInvoke(mh, this); // wrong nestmate 204 checkBadInvoke(mh, mh); // completely wrong type 205 } 206 void access_priv(StaticIface o) throws Throwable { 207 MethodHandle mh = 208 lookup().findSpecial(StaticIface.class, "priv_invoke", M_T, this.getClass()); 209 mh.invoke(o); 210 mh.invokeExact(o); 211 checkBadInvoke(mh, this); // wrong nestmate 212 checkBadInvoke(mh, mh); // completely wrong type 213 } 214 } 215 216 static void checkBadInvoke(MethodHandle mh, Object o) throws Throwable { 217 try { 218 mh.invoke(o); 219 throw new Error("Invoke on MethodHandle " + mh + " with receiver " 220 + o + "should have failed with ClassCastException!"); 221 } 222 catch (ClassCastException expected) { 223 System.out.println("invoke got expected exception: " + expected); 224 } 225 } 226 227 public static void main(String[] args) throws Throwable { 228 TestMethodHandles o = new TestMethodHandles(); 229 StaticNested s = new StaticNested(); 230 InnerNested i = o.new InnerNested(); 231 StaticIface intf = new StaticIface() {}; 232 | 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 M_T = MethodType.methodType(void.class); 40 41 // Private method of nest-top for nestmates to access 42 private void priv_invoke() { 43 System.out.println("TestMethodHandles::priv_invoke"); 44 } 45 46 // public constructor so we aren't relying on private access 47 public TestMethodHandles() {} 48 49 // Methods that will access private methods of nestmates 50 51 void access_priv(TestMethodHandles o) throws Throwable { 52 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 53 mh.invoke(o); 54 mh.invokeExact(o); 55 checkBadInvoke(mh, new StaticNested()); // wrong nestmate 56 checkBadInvoke(mh, mh); // completely wrong type 57 // findSpecial also works when this and o are the same class 58 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 59 mh.invoke(o); 60 mh.invokeExact(o); 61 checkBadInvoke(mh, new StaticNested()); // wrong nestmate 62 checkBadInvoke(mh, mh); // completely wrong type 63 } 64 void access_priv(InnerNested o) throws Throwable { 65 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 66 mh.invoke(o); 67 mh.invokeExact(o); 68 checkBadInvoke(mh, this); // wrong nestmate 69 checkBadInvoke(mh, mh); // completely wrong type 70 try { 71 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 72 throw new Error("findSpecial() succeeded unexpectedly"); 73 } 74 catch (IllegalAccessException expected) {} 75 } 76 void access_priv(StaticNested o) throws Throwable { 77 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 78 mh.invoke(o); 79 mh.invokeExact(o); 80 checkBadInvoke(mh, this); // wrong nestmate 81 checkBadInvoke(mh, mh); // completely wrong type 82 try { 83 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 84 throw new Error("findSpecial() succeeded unexpectedly"); 85 } 86 catch (IllegalAccessException expected) {} 87 } 88 void access_priv(StaticIface o) throws Throwable { 89 MethodHandle mh = lookup().findVirtual(StaticIface.class, "priv_invoke", M_T); 90 mh.invoke(o); 91 mh.invokeExact(o); 92 checkBadInvoke(mh, this); // wrong nestmate 93 checkBadInvoke(mh, mh); // completely wrong type 94 try { 95 mh = lookup().findSpecial(StaticIface.class, "priv_invoke", M_T, this.getClass()); 96 throw new Error("findSpecial() succeeded unexpectedly"); 97 } 98 catch (IllegalAccessException expected) {} 99 } 100 101 // The various nestmates 102 103 static interface StaticIface { 104 105 private void priv_invoke() { 106 System.out.println("StaticIface::priv_invoke"); 107 } 108 109 // Methods that will access private methods of nestmates 110 111 default void access_priv(TestMethodHandles o) throws Throwable { 112 MethodHandle mh = 113 lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 114 mh.invoke(o); 115 mh.invokeExact(o); 116 checkBadInvoke(mh, this); // wrong nestmate 117 checkBadInvoke(mh, mh); // completely wrong type 118 try { 119 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 120 throw new Error("findSpecial() succeeded unexpectedly"); 121 } 122 catch (IllegalAccessException expected) {} 123 } 124 default void access_priv(InnerNested o) throws Throwable { 125 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 126 mh.invoke(o); 127 mh.invokeExact(o); 128 checkBadInvoke(mh, this); // wrong nestmate 129 checkBadInvoke(mh, mh); // completely wrong type 130 try { 131 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 132 throw new Error("findSpecial() succeeded unexpectedly"); 133 } 134 catch (IllegalAccessException expected) {} 135 } 136 default void access_priv(StaticNested o) throws Throwable { 137 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 138 mh.invoke(o); 139 mh.invokeExact(o); 140 checkBadInvoke(mh, this); // wrong nestmate 141 checkBadInvoke(mh, mh); // completely wrong type 142 try { 143 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 144 throw new Error("findSpecial() succeeded unexpectedly"); 145 } 146 catch (IllegalAccessException expected) {} 147 } 148 default void access_priv(StaticIface o) throws Throwable { 149 MethodHandle mh = lookup().findVirtual(StaticIface.class, "priv_invoke", M_T); 150 mh.invoke(o); 151 mh.invokeExact(o); 152 checkBadInvoke(mh, new StaticNested()); // wrong nestmate 153 checkBadInvoke(mh, mh); // completely wrong type 154 // findSpecial also works when this and o are the same interface 155 mh = lookup().findSpecial(StaticIface.class, "priv_invoke", M_T, StaticIface.class); 156 mh.invoke(o); 157 mh.invokeExact(o); 158 checkBadInvoke(mh, new StaticNested()); // wrong nestmate 159 checkBadInvoke(mh, mh); // completely wrong type 160 } 161 } 162 163 static class StaticNested { 164 165 private void priv_invoke() { 166 System.out.println("StaticNested::priv_invoke"); 167 } 168 169 // public constructor so we aren't relying on private access 170 public StaticNested() {} 171 172 // Methods that will access private methods of nestmates 173 174 void access_priv(TestMethodHandles o) throws Throwable { 175 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 176 mh.invoke(o); 177 mh.invokeExact(o); 178 checkBadInvoke(mh, this); // wrong nestmate 179 checkBadInvoke(mh, mh); // completely wrong type 180 try { 181 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 182 throw new Error("findSpecial() succeeded unexpectedly"); 183 } 184 catch (IllegalAccessException expected) {} 185 } 186 void access_priv(InnerNested o) throws Throwable { 187 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 188 mh.invoke(o); 189 mh.invokeExact(o); 190 checkBadInvoke(mh, this); // wrong nestmate 191 checkBadInvoke(mh, mh); // completely wrong type 192 try { 193 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 194 throw new Error("findSpecial() succeeded unexpectedly"); 195 } 196 catch (IllegalAccessException expected) {} 197 } 198 void access_priv(StaticNested o) throws Throwable { 199 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 200 mh.invoke(o); 201 mh.invokeExact(o); 202 checkBadInvoke(mh, new TestMethodHandles()); // wrong nestmate 203 checkBadInvoke(mh, mh); // completely wrong type 204 // findSpecial also works when this and o are the same class 205 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 206 mh.invoke(o); 207 mh.invokeExact(o); 208 checkBadInvoke(mh, new TestMethodHandles()); // wrong nestmate 209 checkBadInvoke(mh, mh); // completely wrong type 210 } 211 void access_priv(StaticIface o) throws Throwable { 212 MethodHandle mh = lookup().findVirtual(StaticIface.class, "priv_invoke", M_T); 213 mh.invoke(o); 214 mh.invokeExact(o); 215 checkBadInvoke(mh, this); // wrong nestmate 216 checkBadInvoke(mh, mh); // completely wrong type 217 try { 218 mh = lookup().findSpecial(StaticIface.class, "priv_invoke", M_T, this.getClass()); 219 throw new Error("findSpecial() succeeded unexpectedly"); 220 } 221 catch (IllegalAccessException expected) {} 222 } 223 } 224 225 class InnerNested { 226 227 private void priv_invoke() { 228 System.out.println("InnerNested::priv_invoke"); 229 } 230 231 // public constructor so we aren't relying on private access 232 public InnerNested() {} 233 234 void access_priv(TestMethodHandles o) throws Throwable { 235 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 236 mh.invoke(o); 237 mh.invokeExact(o); 238 checkBadInvoke(mh, this); // wrong nestmate 239 checkBadInvoke(mh, mh); // completely wrong type 240 try { 241 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 242 throw new Error("findSpecial() succeeded unexpectedly"); 243 } 244 catch (IllegalAccessException expected) {} 245 } 246 void access_priv(InnerNested o) throws Throwable { 247 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 248 mh.invoke(o); 249 mh.invokeExact(o); 250 checkBadInvoke(mh, new StaticNested()); // wrong nestmate 251 checkBadInvoke(mh, mh); // completely wrong type 252 // findSpecial also works when this and o are the same class 253 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 254 mh.invoke(o); 255 mh.invokeExact(o); 256 checkBadInvoke(mh, new StaticNested()); // wrong nestmate 257 checkBadInvoke(mh, mh); // completely wrong type 258 } 259 void access_priv(StaticNested o) throws Throwable { 260 MethodHandle mh = lookup().findVirtual(o.getClass(), "priv_invoke", M_T); 261 mh.invoke(o); 262 mh.invokeExact(o); 263 checkBadInvoke(mh, this); // wrong nestmate 264 checkBadInvoke(mh, mh); // completely wrong type 265 try { 266 mh = lookup().findSpecial(o.getClass(), "priv_invoke", M_T, this.getClass()); 267 throw new Error("findSpecial() succeeded unexpectedly"); 268 } 269 catch (IllegalAccessException expected) {} 270 } 271 void access_priv(StaticIface o) throws Throwable { 272 MethodHandle mh = lookup().findVirtual(StaticIface.class, "priv_invoke", M_T); 273 mh.invoke(o); 274 mh.invokeExact(o); 275 checkBadInvoke(mh, this); // wrong nestmate 276 checkBadInvoke(mh, mh); // completely wrong type 277 try { 278 mh = lookup().findSpecial(StaticIface.class, "priv_invoke", M_T, this.getClass()); 279 throw new Error("findSpecial() succeeded unexpectedly"); 280 } 281 catch (IllegalAccessException expected) {} 282 } 283 } 284 285 static void checkBadInvoke(MethodHandle mh, Object o) throws Throwable { 286 try { 287 mh.invoke(o); 288 throw new Error("Invoke on MethodHandle " + mh + " with receiver " 289 + o + "should have failed with ClassCastException!"); 290 } 291 catch (ClassCastException expected) { 292 System.out.println("invoke got expected exception: " + expected); 293 } 294 } 295 296 public static void main(String[] args) throws Throwable { 297 TestMethodHandles o = new TestMethodHandles(); 298 StaticNested s = new StaticNested(); 299 InnerNested i = o.new InnerNested(); 300 StaticIface intf = new StaticIface() {}; 301 |