1 /* 2 * Copyright (c) 2012, 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 7196190 27 * @summary Improve method of handling MethodHandles 28 * 29 * @run main/othervm/policy=jtreg.security.policy/secure=java.lang.SecurityManager GetUnsafeTest 30 */ 31 32 import java.lang.invoke.*; 33 import java.lang.reflect.Method; 34 import java.util.Arrays; 35 36 public class GetUnsafeTest { 37 static final String NAME = "sun.misc.Unsafe"; 38 39 private static boolean isTestFailed = false; 40 41 private static void fail() { 42 isTestFailed = true; 43 try { throw new Exception(); } catch (Throwable e) { 44 StackTraceElement frame = e.getStackTrace()[1]; 45 System.out.printf("Failed at %s:%d\n", frame.getFileName(), frame.getLineNumber()); 46 } 47 } 48 49 public static void main(String[] args) throws Throwable { 50 { 51 final MethodType mt = MethodType.methodType(Class.class, String.class); 52 final MethodHandle mh = MethodHandles.lookup() 53 .findStatic(Class.class, "forName", mt); 54 55 try { Class.forName(NAME); fail(); } catch (Throwable e) {} 56 57 try { mh.invoke(NAME); fail(); } catch (Throwable e) {} 58 try { mh.bindTo(NAME).invoke(); fail(); } catch (Throwable e) {} 59 try { mh.invokeWithArguments(Arrays.asList(NAME)); fail(); } catch (Throwable e) {} 60 try { mh.invokeWithArguments(NAME); fail(); } catch (Throwable e) {} 61 try { Class cls = (Class) mh.invokeExact(NAME); fail(); } catch (Throwable e) {} 62 } 63 64 { 65 final Method fnMethod = Class.class.getMethod("forName", String.class); 66 final MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class); 67 final MethodHandle mh = MethodHandles.lookup() 68 .findVirtual(Method.class, "invoke", mt) 69 .bindTo(fnMethod); 70 71 try { fnMethod.invoke(null, NAME); fail(); } catch (Throwable e) {} 72 73 try { mh.bindTo(null).bindTo(new Object[]{NAME}).invoke(); fail(); } catch (Throwable e) {} 74 try { mh.invoke(null, new Object[]{NAME}); fail(); } catch (Throwable e) {} 75 try { mh.invokeWithArguments(null, new Object[]{NAME}); fail(); } catch (Throwable e) {} 76 try { mh.invokeWithArguments(Arrays.asList(null, new Object[]{NAME})); fail(); } catch (Throwable e) {} 77 try { Object obj = mh.invokeExact((Object) null, new Object[]{NAME}); fail(); } catch (Throwable e) {} 78 } 79 80 { 81 final Method fnMethod = Class.class.getMethod("forName", String.class); 82 final MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class); 83 84 final MethodHandle mh = MethodHandles.lookup().bind(fnMethod, "invoke", mt); 85 86 try { mh.bindTo(null).bindTo(new Object[]{NAME}).invoke(); fail(); } catch (Throwable e) {} 87 try { mh.invoke(null, new Object[]{NAME}); fail(); } catch (Throwable e) {} 88 try { mh.invokeWithArguments(null, NAME); fail(); } catch (Throwable e) {} 89 try { mh.invokeWithArguments(Arrays.asList(null, NAME)); fail(); } catch (Throwable e) {} 90 try { Object obj = mh.invokeExact((Object) null, new Object[]{NAME}); fail(); } catch (Throwable e) {} 91 } 92 93 { 94 final Method fnMethod = Class.class.getMethod("forName", String.class); 95 final MethodHandle mh = MethodHandles.lookup().unreflect(fnMethod); 96 97 try { mh.bindTo(NAME).invoke(); fail(); } catch (Throwable e) {} 98 try { mh.invoke(NAME); fail(); } catch (Throwable e) {} 99 try { mh.invokeWithArguments(NAME); fail(); } catch (Throwable e) {} 100 try { mh.invokeWithArguments(Arrays.asList(NAME)); fail(); } catch (Throwable e) {} 101 try { Class cls = (Class) mh.invokeExact(NAME); fail(); } catch (Throwable e) {} 102 } 103 104 if (!isTestFailed) { 105 System.out.println("TEST PASSED"); 106 } else { 107 System.out.println("TEST FAILED"); 108 System.exit(1); 109 } 110 } 111 }