Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/7196190/ClassForNameTest.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 ClassForNameTest30*/3132import java.lang.invoke.*;33import java.lang.reflect.Method;34import java.util.Arrays;3536public class ClassForNameTest {37final static String NAME = ClassForNameTest.class.getName();3839public static void main(String[] args) throws Throwable {40{41final MethodType mt = MethodType.methodType(Class.class, String.class);42final MethodHandle mh = MethodHandles.lookup()43.findStatic(Class.class, "forName", mt);4445Class.forName(NAME);4647mh.invoke(NAME);48mh.bindTo(NAME).invoke();49mh.invokeWithArguments(Arrays.asList(NAME));50mh.invokeWithArguments(NAME);51Class cls = (Class) mh.invokeExact(NAME);52}5354{55final Method fnMethod = Class.class.getMethod("forName", String.class);56final MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class);57final MethodHandle mh = MethodHandles.lookup()58.findVirtual(Method.class, "invoke", mt)59.bindTo(fnMethod);6061fnMethod.invoke(null, NAME);6263mh.bindTo(null).bindTo(new Object[]{NAME}).invoke();64mh.invoke(null, new Object[]{NAME});65mh.invokeWithArguments(null, new Object[]{NAME});66mh.invokeWithArguments(Arrays.asList(null, new Object[]{NAME}));67Object obj = mh.invokeExact((Object) null, new Object[]{NAME});68}6970{71final Method fnMethod = Class.class.getMethod("forName", String.class);72final MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class);7374final MethodHandle mh = MethodHandles.lookup()75.bind(fnMethod, "invoke", mt);7677mh.bindTo(null).bindTo(new Object[]{NAME}).invoke();78mh.invoke(null, new Object[]{NAME});79mh.invokeWithArguments(null, NAME);80mh.invokeWithArguments(Arrays.asList(null, NAME));81Object obj = mh.invokeExact((Object) null, new Object[]{NAME});82}8384{85final Method fnMethod = Class.class.getMethod("forName", String.class);86final MethodHandle mh = MethodHandles.lookup().unreflect(fnMethod);8788mh.bindTo(NAME).invoke();89mh.invoke(NAME);90mh.invokeWithArguments(NAME);91mh.invokeWithArguments(Arrays.asList(NAME));92Class cls = (Class) mh.invokeExact(NAME);93}9495System.out.println("TEST PASSED");96}97}9899100