1 /* 2 * Copyright (c) 2017, 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 /* @test 25 * @bug 8177146 26 * @run testng/othervm TestMethodHandleBind 27 */ 28 29 import org.testng.annotations.Test; 30 31 import java.lang.invoke.MethodHandle; 32 import java.lang.invoke.MethodType; 33 34 import static java.lang.invoke.MethodHandles.lookup; 35 36 import static org.testng.Assert.*; 37 38 public class TestMethodHandleBind extends pkg.A { 39 static class B extends TestMethodHandleBind {} 40 41 @Test testInstanceOfCallerClass()42 public void testInstanceOfCallerClass() throws Throwable { 43 MethodHandle bound = lookup().bind(new TestMethodHandleBind() , "m1", MethodType.methodType(String.class)); 44 String x = (String)bound.invoke(); 45 assertEquals(x, this.getClass().getSimpleName()); 46 } 47 48 @Test testInstanceOfCallerSubclass()49 public void testInstanceOfCallerSubclass() throws Throwable { 50 MethodHandle bound = lookup().bind(new B() , "m1", MethodType.methodType(String.class)); 51 // MethodHandle bound = lookup().findVirtual(B.class, "m1", MethodType.methodType(String.class)).bindTo(new B()); 52 String x = (String)bound.invoke(); 53 assertEquals(x, "B"); 54 } 55 56 @Test testInstanceOfReceiverClass()57 public void testInstanceOfReceiverClass() throws Throwable { 58 try { 59 MethodHandle bound = lookup().bind(new pkg.A() , "m1", MethodType.methodType(String.class)); 60 bound.invoke(); 61 fail("IllegalAccessException expected"); 62 } catch (IllegalAccessException e) { 63 } 64 } 65 66 @Test testPublicMethod()67 public void testPublicMethod() throws Throwable { 68 MethodHandle bound = lookup().bind(new pkg.A() , "m2", MethodType.methodType(String.class)); 69 String x = (String)bound.invoke(); 70 assertEquals(x, "A"); 71 } 72 73 @Test testPublicMethod2()74 public void testPublicMethod2() throws Throwable { 75 MethodHandle bound = lookup().bind(new TestMethodHandleBind(), "m2", MethodType.methodType(String.class)); 76 String x = (String)bound.invoke(); 77 assertEquals(x, this.getClass().getSimpleName()); 78 } 79 80 @Test testInstanceOfCallerClassVarargs()81 public void testInstanceOfCallerClassVarargs() throws Throwable { 82 MethodHandle bound = lookup().bind(new TestMethodHandleBind() , "m3", MethodType.methodType(String.class, String[].class)); 83 String x = (String)bound.invoke("a", "b", "c"); 84 assertEquals(x, this.getClass().getSimpleName() + "abc"); 85 } 86 87 @Test testInstanceOfReceiverClassVarargs()88 public void testInstanceOfReceiverClassVarargs() throws Throwable { 89 try { 90 MethodHandle bound = lookup().bind(new pkg.A(), "m3", MethodType.methodType(String.class, String[].class)); 91 bound.invoke(); 92 fail("IllegalAccessException expected"); 93 } catch (IllegalAccessException e) { 94 } 95 } 96 } 97