Path: blob/master/test/jdk/java/lang/invoke/8177146/TestMethodHandleBind.java
41153 views
/*1* Copyright (c) 2017, 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/* @test24* @bug 817714625* @run testng/othervm TestMethodHandleBind26*/2728import org.testng.annotations.Test;2930import java.lang.invoke.MethodHandle;31import java.lang.invoke.MethodType;3233import static java.lang.invoke.MethodHandles.lookup;3435import static org.testng.Assert.*;3637public class TestMethodHandleBind extends pkg.A {38static class B extends TestMethodHandleBind {}3940@Test41public void testInstanceOfCallerClass() throws Throwable {42MethodHandle bound = lookup().bind(new TestMethodHandleBind() , "m1", MethodType.methodType(String.class));43String x = (String)bound.invoke();44assertEquals(x, this.getClass().getSimpleName());45}4647@Test48public void testInstanceOfCallerSubclass() throws Throwable {49MethodHandle bound = lookup().bind(new B() , "m1", MethodType.methodType(String.class));50// MethodHandle bound = lookup().findVirtual(B.class, "m1", MethodType.methodType(String.class)).bindTo(new B());51String x = (String)bound.invoke();52assertEquals(x, "B");53}5455@Test56public void testInstanceOfReceiverClass() throws Throwable {57try {58MethodHandle bound = lookup().bind(new pkg.A() , "m1", MethodType.methodType(String.class));59bound.invoke();60fail("IllegalAccessException expected");61} catch (IllegalAccessException e) {62}63}6465@Test66public void testPublicMethod() throws Throwable {67MethodHandle bound = lookup().bind(new pkg.A() , "m2", MethodType.methodType(String.class));68String x = (String)bound.invoke();69assertEquals(x, "A");70}7172@Test73public void testPublicMethod2() throws Throwable {74MethodHandle bound = lookup().bind(new TestMethodHandleBind(), "m2", MethodType.methodType(String.class));75String x = (String)bound.invoke();76assertEquals(x, this.getClass().getSimpleName());77}7879@Test80public void testInstanceOfCallerClassVarargs() throws Throwable {81MethodHandle bound = lookup().bind(new TestMethodHandleBind() , "m3", MethodType.methodType(String.class, String[].class));82String x = (String)bound.invoke("a", "b", "c");83assertEquals(x, this.getClass().getSimpleName() + "abc");84}8586@Test87public void testInstanceOfReceiverClassVarargs() throws Throwable {88try {89MethodHandle bound = lookup().bind(new pkg.A(), "m3", MethodType.methodType(String.class, String[].class));90bound.invoke();91fail("IllegalAccessException expected");92} catch (IllegalAccessException e) {93}94}95}969798