Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/7196190/GetUnsafeTest.java
47311 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/**25* @test26* @bug 719619027* @summary Improve method of handling MethodHandles28*29* @run main/othervm/policy=jtreg.security.policy/secure=java.lang.SecurityManager GetUnsafeTest30*/3132import java.lang.invoke.*;33import java.lang.reflect.Method;34import java.util.Arrays;3536public class GetUnsafeTest {37final static String NAME = "sun.misc.Unsafe";3839private static boolean isTestFailed = false;4041private static void fail() {42isTestFailed = true;43try { throw new Exception(); } catch (Throwable e) {44StackTraceElement frame = e.getStackTrace()[1];45System.out.printf("Failed at %s:%d\n", frame.getFileName(), frame.getLineNumber());46}47}4849public static void main(String[] args) throws Throwable {50{51final MethodType mt = MethodType.methodType(Class.class, String.class);52final MethodHandle mh = MethodHandles.lookup()53.findStatic(Class.class, "forName", mt);5455try { Class.forName(NAME); fail(); } catch (Throwable e) {}5657try { mh.invoke(NAME); fail(); } catch (Throwable e) {}58try { mh.bindTo(NAME).invoke(); fail(); } catch (Throwable e) {}59try { mh.invokeWithArguments(Arrays.asList(NAME)); fail(); } catch (Throwable e) {}60try { mh.invokeWithArguments(NAME); fail(); } catch (Throwable e) {}61try { Class cls = (Class) mh.invokeExact(NAME); fail(); } catch (Throwable e) {}62}6364{65final Method fnMethod = Class.class.getMethod("forName", String.class);66final MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class);67final MethodHandle mh = MethodHandles.lookup()68.findVirtual(Method.class, "invoke", mt)69.bindTo(fnMethod);7071try { fnMethod.invoke(null, NAME); fail(); } catch (Throwable e) {}7273try { mh.bindTo(null).bindTo(new Object[]{NAME}).invoke(); fail(); } catch (Throwable e) {}74try { mh.invoke(null, new Object[]{NAME}); fail(); } catch (Throwable e) {}75try { mh.invokeWithArguments(null, new Object[]{NAME}); fail(); } catch (Throwable e) {}76try { mh.invokeWithArguments(Arrays.asList(null, new Object[]{NAME})); fail(); } catch (Throwable e) {}77try { Object obj = mh.invokeExact((Object) null, new Object[]{NAME}); fail(); } catch (Throwable e) {}78}7980{81final Method fnMethod = Class.class.getMethod("forName", String.class);82final MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class);8384final MethodHandle mh = MethodHandles.lookup().bind(fnMethod, "invoke", mt);8586try { mh.bindTo(null).bindTo(new Object[]{NAME}).invoke(); fail(); } catch (Throwable e) {}87try { mh.invoke(null, new Object[]{NAME}); fail(); } catch (Throwable e) {}88try { mh.invokeWithArguments(null, NAME); fail(); } catch (Throwable e) {}89try { mh.invokeWithArguments(Arrays.asList(null, NAME)); fail(); } catch (Throwable e) {}90try { Object obj = mh.invokeExact((Object) null, new Object[]{NAME}); fail(); } catch (Throwable e) {}91}9293{94final Method fnMethod = Class.class.getMethod("forName", String.class);95final MethodHandle mh = MethodHandles.lookup().unreflect(fnMethod);9697try { mh.bindTo(NAME).invoke(); fail(); } catch (Throwable e) {}98try { mh.invoke(NAME); fail(); } catch (Throwable e) {}99try { mh.invokeWithArguments(NAME); fail(); } catch (Throwable e) {}100try { mh.invokeWithArguments(Arrays.asList(NAME)); fail(); } catch (Throwable e) {}101try { Class cls = (Class) mh.invokeExact(NAME); fail(); } catch (Throwable e) {}102}103104if (!isTestFailed) {105System.out.println("TEST PASSED");106} else {107System.out.println("TEST FAILED");108System.exit(1);109}110}111}112113114