Path: blob/master/test/jdk/java/lang/invoke/7196190/GetUnsafeTest.java
41153 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*/2223/**24* @test25* @bug 719619026* @summary Improve method of handling MethodHandles27*28* @run main/othervm/policy=jtreg.security.policy/secure=java.lang.SecurityManager GetUnsafeTest29*/3031import java.lang.invoke.*;32import java.lang.reflect.Method;33import java.util.Arrays;3435public class GetUnsafeTest {36static final String NAME = "sun.misc.Unsafe";3738private static boolean isTestFailed = false;3940private static void fail() {41isTestFailed = true;42try { throw new Exception(); } catch (Throwable e) {43StackTraceElement frame = e.getStackTrace()[1];44System.out.printf("Failed at %s:%d\n", frame.getFileName(), frame.getLineNumber());45}46}4748public static void main(String[] args) throws Throwable {49{50final MethodType mt = MethodType.methodType(Class.class, String.class);51final MethodHandle mh = MethodHandles.lookup()52.findStatic(Class.class, "forName", mt);5354try { Class.forName(NAME); fail(); } catch (Throwable e) {}5556try { mh.invoke(NAME); fail(); } catch (Throwable e) {}57try { mh.bindTo(NAME).invoke(); fail(); } catch (Throwable e) {}58try { mh.invokeWithArguments(Arrays.asList(NAME)); fail(); } catch (Throwable e) {}59try { mh.invokeWithArguments(NAME); fail(); } catch (Throwable e) {}60try { Class cls = (Class) mh.invokeExact(NAME); fail(); } catch (Throwable e) {}61}6263{64final Method fnMethod = Class.class.getMethod("forName", String.class);65final MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class);66final MethodHandle mh = MethodHandles.lookup()67.findVirtual(Method.class, "invoke", mt)68.bindTo(fnMethod);6970try { fnMethod.invoke(null, NAME); fail(); } catch (Throwable e) {}7172try { mh.bindTo(null).bindTo(new Object[]{NAME}).invoke(); fail(); } catch (Throwable e) {}73try { mh.invoke(null, new Object[]{NAME}); fail(); } catch (Throwable e) {}74try { mh.invokeWithArguments(null, new Object[]{NAME}); fail(); } catch (Throwable e) {}75try { mh.invokeWithArguments(Arrays.asList(null, new Object[]{NAME})); fail(); } catch (Throwable e) {}76try { Object obj = mh.invokeExact((Object) null, new Object[]{NAME}); fail(); } catch (Throwable e) {}77}7879{80final Method fnMethod = Class.class.getMethod("forName", String.class);81final MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class);8283final MethodHandle mh = MethodHandles.lookup().bind(fnMethod, "invoke", mt);8485try { mh.bindTo(null).bindTo(new Object[]{NAME}).invoke(); fail(); } catch (Throwable e) {}86try { mh.invoke(null, new Object[]{NAME}); fail(); } catch (Throwable e) {}87try { mh.invokeWithArguments(null, NAME); fail(); } catch (Throwable e) {}88try { mh.invokeWithArguments(Arrays.asList(null, NAME)); fail(); } catch (Throwable e) {}89try { Object obj = mh.invokeExact((Object) null, new Object[]{NAME}); fail(); } catch (Throwable e) {}90}9192{93final Method fnMethod = Class.class.getMethod("forName", String.class);94final MethodHandle mh = MethodHandles.lookup().unreflect(fnMethod);9596try { mh.bindTo(NAME).invoke(); fail(); } catch (Throwable e) {}97try { mh.invoke(NAME); fail(); } catch (Throwable e) {}98try { mh.invokeWithArguments(NAME); fail(); } catch (Throwable e) {}99try { mh.invokeWithArguments(Arrays.asList(NAME)); fail(); } catch (Throwable e) {}100try { Class cls = (Class) mh.invokeExact(NAME); fail(); } catch (Throwable e) {}101}102103if (!isTestFailed) {104System.out.println("TEST PASSED");105} else {106System.out.println("TEST FAILED");107System.exit(1);108}109}110}111112113