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