Path: blob/master/test/jdk/java/lang/invoke/FindAccessTest.java
41149 views
/*1* Copyright (c) 2015, 2016, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* @test26* @bug 813988527* @run testng/othervm -ea -esa test.java.lang.invoke.FindAccessTest28*/2930package test.java.lang.invoke;3132import java.lang.invoke.MethodHandle;33import java.lang.invoke.MethodHandles;34import java.lang.invoke.MethodHandles.Lookup;35import java.lang.invoke.MethodType;3637import static org.testng.AssertJUnit.*;3839import org.testng.annotations.*;4041/**42* Tests for Lookup.findClass/accessClass extensions added in JEP 274.43*/44public class FindAccessTest {4546static final Lookup LOOKUP = MethodHandles.lookup();4748@Test49public static void testFindSpecial() throws Throwable {50FindSpecial.C c = new FindSpecial.C();51assertEquals("I1.m", c.m());52MethodType t = MethodType.methodType(String.class);53MethodHandle ci1m = LOOKUP.findSpecial(FindSpecial.I1.class, "m", t, FindSpecial.C.class);54assertEquals("I1.m", (String) ci1m.invoke(c));55}5657@Test58public static void testFindSpecialAbstract() throws Throwable {59FindSpecial.C c = new FindSpecial.C();60assertEquals("q", c.q());61MethodType t = MethodType.methodType(String.class);62boolean caught = false;63try {64MethodHandle ci3q = LOOKUP.findSpecial(FindSpecial.I3.class, "q", t, FindSpecial.C.class);65} catch (Throwable thrown) {66if (!(thrown instanceof IllegalAccessException) || !FindSpecial.ABSTRACT_ERROR.equals(thrown.getMessage())) {67throw new AssertionError(thrown.getMessage(), thrown);68}69caught = true;70}71assertTrue(caught);72}7374@Test(expectedExceptions = {ClassNotFoundException.class})75public static void testFindClassCNFE() throws ClassNotFoundException, IllegalAccessException {76LOOKUP.findClass("does.not.Exist");77}7879static class FindSpecial {8081interface I1 {82default String m() {83return "I1.m";84}85}8687interface I2 {88default String m() {89return "I2.m";90}91}9293interface I3 {94String q();95}9697static class C implements I1, I2, I3 {98public String m() {99return I1.super.m();100}101public String q() {102return "q";103}104}105106static final String ABSTRACT_ERROR = "no such method: test.java.lang.invoke.FindAccessTest$FindSpecial$I3.q()String/invokeSpecial";107108}109110}111112113