|
1 /* |
|
2 * Copyright (c) 2015, 2016, 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 /* @test |
|
27 * @bug 8139885 |
|
28 * @run testng/othervm -ea -esa test.java.lang.invoke.FindAccessTest |
|
29 */ |
|
30 |
|
31 package test.java.lang.invoke; |
|
32 |
|
33 import java.lang.invoke.MethodHandle; |
|
34 import java.lang.invoke.MethodHandles; |
|
35 import java.lang.invoke.MethodHandles.Lookup; |
|
36 import java.lang.invoke.MethodType; |
|
37 |
|
38 import static org.testng.AssertJUnit.*; |
|
39 |
|
40 import org.testng.annotations.*; |
|
41 |
|
42 /** |
|
43 * Tests for Lookup.findClass/accessClass extensions added in JEP 274. |
|
44 */ |
|
45 public class FindAccessTest { |
|
46 |
|
47 static final Lookup LOOKUP = MethodHandles.lookup(); |
|
48 |
|
49 @Test |
|
50 public static void testFindSpecial() throws Throwable { |
|
51 FindSpecial.C c = new FindSpecial.C(); |
|
52 assertEquals("I1.m", c.m()); |
|
53 MethodType t = MethodType.methodType(String.class); |
|
54 MethodHandle ci1m = LOOKUP.findSpecial(FindSpecial.I1.class, "m", t, FindSpecial.C.class); |
|
55 assertEquals("I1.m", (String) ci1m.invoke(c)); |
|
56 } |
|
57 |
|
58 @Test |
|
59 public static void testFindSpecialAbstract() throws Throwable { |
|
60 FindSpecial.C c = new FindSpecial.C(); |
|
61 assertEquals("q", c.q()); |
|
62 MethodType t = MethodType.methodType(String.class); |
|
63 boolean caught = false; |
|
64 try { |
|
65 MethodHandle ci3q = LOOKUP.findSpecial(FindSpecial.I3.class, "q", t, FindSpecial.C.class); |
|
66 } catch (Throwable thrown) { |
|
67 if (!(thrown instanceof IllegalAccessException) || !FindSpecial.ABSTRACT_ERROR.equals(thrown.getMessage())) { |
|
68 throw new AssertionError(thrown.getMessage(), thrown); |
|
69 } |
|
70 caught = true; |
|
71 } |
|
72 assertTrue(caught); |
|
73 } |
|
74 |
|
75 @Test(expectedExceptions = {ClassNotFoundException.class}) |
|
76 public static void testFindClassCNFE() throws ClassNotFoundException, IllegalAccessException { |
|
77 LOOKUP.findClass("does.not.Exist"); |
|
78 } |
|
79 |
|
80 static class FindSpecial { |
|
81 |
|
82 interface I1 { |
|
83 default String m() { |
|
84 return "I1.m"; |
|
85 } |
|
86 } |
|
87 |
|
88 interface I2 { |
|
89 default String m() { |
|
90 return "I2.m"; |
|
91 } |
|
92 } |
|
93 |
|
94 interface I3 { |
|
95 String q(); |
|
96 } |
|
97 |
|
98 static class C implements I1, I2, I3 { |
|
99 public String m() { |
|
100 return I1.super.m(); |
|
101 } |
|
102 public String q() { |
|
103 return "q"; |
|
104 } |
|
105 } |
|
106 |
|
107 static final String ABSTRACT_ERROR = "no such method: test.java.lang.invoke.FindAccessTest$FindSpecial$I3.q()String/invokeSpecial"; |
|
108 |
|
109 } |
|
110 |
|
111 } |