author | dholmes |
Sat, 23 Jun 2018 01:32:41 -0400 | |
changeset 50735 | 2f2af62dfac7 |
permissions | -rw-r--r-- |
50735
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
1 |
/* |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
4 |
* |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
8 |
* |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
13 |
* accompanied this code). |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
14 |
* |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
18 |
* |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
21 |
* questions. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
22 |
*/ |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
23 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
24 |
/** |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
25 |
* @test |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
26 |
* @bug 8046171 |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
27 |
* @summary Test direct and MethodHandle access to private interface methods using invokeinterface semantics |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
28 |
* to ensure all receiver typechecks occur as required. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
29 |
* @comment This complements SpecialInterfaceCall which tests invokespecial semantics. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
30 |
* @compile PrivateInterfaceCall.java |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
31 |
* @compile PrivateInterfaceCallI4.jasm |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
32 |
* @run main/othervm -Xint PrivateInterfaceCall |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
33 |
* @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=1 PrivateInterfaceCall |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
34 |
* @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=2 PrivateInterfaceCall |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
35 |
* @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=3 PrivateInterfaceCall |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
36 |
* @run main/othervm -Xbatch -XX:-TieredCompilation PrivateInterfaceCall |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
37 |
*/ |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
38 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
39 |
// This is an adaptation of SpecialInterfaceCall to only use private interface methods and with |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
40 |
// virtual invocation semantics. Because we don't have the same corner cases as for invokespecial |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
41 |
// there's no practical difference between the I3 and I2 cases here. But we do have to ensure the |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
42 |
// correct versions of the methods get executed. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
43 |
// In addition we add tests that involve calls from nestmates - which also covers the distinction |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
44 |
// between the caller being a class and being an interface. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
45 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
46 |
import java.lang.invoke.*; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
47 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
48 |
public class PrivateInterfaceCall { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
49 |
interface I1 { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
50 |
private void priv_m() { throw new Error("Should not call this"); }; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
51 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
52 |
interface I2 extends I1 { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
53 |
private void priv_m() { }; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
54 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
55 |
static void invokeDirect(I2 i) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
56 |
i.priv_m(); // generates invokeinterface |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
57 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
58 |
static void invokeInterfaceMH(I2 i) throws Throwable { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
59 |
// emulates behaviour of invokeDirect |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
60 |
mh_I2_priv_m_from_I2.invokeExact(i); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
61 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
62 |
// special case of invoking an Object method via an interface |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
63 |
static void invokeInterfaceObjectMH(I2 i) throws Throwable { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
64 |
// emulates invokeInterface of I2.toString on i, which resolves |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
65 |
// to Object.toString |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
66 |
String s = (String) mh_I2_toString_from_I2.invokeExact(i); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
67 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
68 |
// special case of invoking a final Object method via an interface |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
69 |
static void invokeInterfaceObjectFinalMH(I2 i) throws Throwable { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
70 |
// emulates invokeInterface of I1.getClass on i, which resolves |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
71 |
// to Object.getClass |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
72 |
Class<?> c = (Class<?>) mh_I2_getClass_from_I2.invokeExact(i); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
73 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
74 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
75 |
static void init() throws Throwable { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
76 |
MethodType mt = MethodType.methodType(void.class); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
77 |
MethodHandles.Lookup lookup = MethodHandles.lookup(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
78 |
mh_I2_priv_m_from_I2 = lookup.findVirtual(I2.class, "priv_m", mt); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
79 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
80 |
mt = MethodType.methodType(String.class); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
81 |
mh_I2_toString_from_I2 = lookup.findVirtual(I2.class, "toString", mt); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
82 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
83 |
mt = MethodType.methodType(Class.class); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
84 |
mh_I2_getClass_from_I2 = lookup.findVirtual(I2.class, "getClass", mt); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
85 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
86 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
87 |
interface I3 extends I2 { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
88 |
static void invokeInterfaceMH(I2 i) throws Throwable { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
89 |
// emulates behaviour of I2.invokeDirect |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
90 |
mh_I2_priv_m_from_I3.invokeExact(i); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
91 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
92 |
static void init() throws Throwable { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
93 |
MethodType mt = MethodType.methodType(void.class); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
94 |
mh_I2_priv_m_from_I3 = MethodHandles.lookup().findVirtual(I2.class, "priv_m", mt); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
95 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
96 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
97 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
98 |
// This interface acts like I2 but we define directInvoke* methods |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
99 |
// that we will rewrite the bytecode of to use invokeinterface |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
100 |
// (see PrivateInterfaceCallI4.jasm). |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
101 |
interface I4 extends I1 { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
102 |
static void invokeDirect(I4 i) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
103 |
// invokeinterface I4.toString() |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
104 |
throw new Error("Class file for I4 is not overwritten"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
105 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
106 |
static void invokeDirectFinal(I4 i) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
107 |
// invokeinterface I4.getClass() - final method |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
108 |
throw new Error("Class file for I4 is not overwritten"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
109 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
110 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
111 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
112 |
// check invocations from nestmates outside the |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
113 |
// inheritance hierarchy - and from a class not interface |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
114 |
static void invokeDirect(I2 i) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
115 |
i.priv_m(); // generates invokeinterface |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
116 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
117 |
static void invokeInterfaceMH(I2 i) throws Throwable { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
118 |
mh_I2_priv_m_from_PIC.invokeExact(i); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
119 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
120 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
121 |
// Concrete classes |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
122 |
static class C2 implements I2 { } |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
123 |
static class C3 implements I3 { } |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
124 |
static class C4 implements I4 { } |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
125 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
126 |
// Classes that don't implement I2/I3 but do have a |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
127 |
// priv_m method in their hierarchy |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
128 |
static class D1 implements I1 { } |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
129 |
static class E { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
130 |
private void priv_m() { throw new Error("Should not call this"); } |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
131 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
132 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
133 |
// This MH acts like the invocation in I2.invokeDirect with caller I2 |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
134 |
static MethodHandle mh_I2_priv_m_from_I2; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
135 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
136 |
// This MH acts like the invocation in I3.invokeDirect with caller I3 |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
137 |
static MethodHandle mh_I2_priv_m_from_I3; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
138 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
139 |
// This MH acts like the invocation in PrivateInterfaceCall.invokeDirect |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
140 |
// with caller PrivateInterfaceCall |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
141 |
static MethodHandle mh_I2_priv_m_from_PIC; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
142 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
143 |
// This MH acts likes an invokeinterface of I2.toString from I2 |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
144 |
static MethodHandle mh_I2_toString_from_I2; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
145 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
146 |
// This MH acts likes an invokeinterface of I2.getClass from I2 |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
147 |
static MethodHandle mh_I2_getClass_from_I2; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
148 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
149 |
static { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
150 |
try { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
151 |
MethodType mt = MethodType.methodType(void.class); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
152 |
mh_I2_priv_m_from_PIC = MethodHandles.lookup().findVirtual(I2.class, "priv_m", mt); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
153 |
I2.init(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
154 |
I3.init(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
155 |
} catch (Throwable e) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
156 |
throw new Error(e); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
157 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
158 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
159 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
160 |
static void runPositiveTests() { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
161 |
shouldNotThrow(() -> PrivateInterfaceCall.invokeDirect(new C2())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
162 |
shouldNotThrow(() -> PrivateInterfaceCall.invokeDirect(new C3())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
163 |
shouldNotThrow(() -> PrivateInterfaceCall.invokeInterfaceMH(new C2())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
164 |
shouldNotThrow(() -> PrivateInterfaceCall.invokeInterfaceMH(new C3())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
165 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
166 |
shouldNotThrow(() -> I2.invokeDirect(new C2())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
167 |
shouldNotThrow(() -> I2.invokeDirect(new C3())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
168 |
shouldNotThrow(() -> I2.invokeInterfaceMH(new C2())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
169 |
shouldNotThrow(() -> I2.invokeInterfaceMH(new C3())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
170 |
shouldNotThrow(() -> I2.invokeInterfaceObjectMH(new C2())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
171 |
shouldNotThrow(() -> I2.invokeInterfaceObjectMH(new C3())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
172 |
shouldNotThrow(() -> I2.invokeInterfaceObjectFinalMH(new C2())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
173 |
shouldNotThrow(() -> I2.invokeInterfaceObjectFinalMH(new C3())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
174 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
175 |
// This looks odd but at runtime the only constraint is that the |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
176 |
// receiver is an I2. In contrast in the invokespecial case the |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
177 |
// receiver must be an I3. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
178 |
shouldNotThrow(() -> I3.invokeInterfaceMH(unsafeCastI3(new C2()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
179 |
shouldNotThrow(() -> I3.invokeInterfaceMH(new C3())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
180 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
181 |
shouldNotThrow(() -> I4.invokeDirect(new C4())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
182 |
shouldNotThrow(() -> I4.invokeDirectFinal(new C4())); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
183 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
184 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
185 |
static void runNegativeTests() { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
186 |
System.out.println("ICCE PrivateInterfaceCall.invokeDirect D1"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
187 |
shouldThrowICCE(() -> PrivateInterfaceCall.invokeDirect(unsafeCastI2(new D1()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
188 |
System.out.println("ICCE PrivateInterfaceCall.invokeDirect E"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
189 |
shouldThrowICCE(() -> PrivateInterfaceCall.invokeDirect(unsafeCastI2(new E()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
190 |
System.out.println("ICCE PrivateInterfaceCall.invokeInterfaceMH D1"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
191 |
shouldThrowICCE(() -> PrivateInterfaceCall.invokeInterfaceMH(unsafeCastI2(new D1()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
192 |
System.out.println("ICCE PrivateInterfaceCall.invokeInterfaceMH E"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
193 |
shouldThrowICCE(() -> PrivateInterfaceCall.invokeInterfaceMH(unsafeCastI2(new E()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
194 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
195 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
196 |
System.out.println("ICCE I2.invokeInterfaceMH D1"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
197 |
shouldThrowICCE(() -> I2.invokeInterfaceMH(unsafeCastI2(new D1()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
198 |
System.out.println("ICCE I2.invokeInterfaceMH E"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
199 |
shouldThrowICCE(() -> I2.invokeInterfaceMH(unsafeCastI2(new E()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
200 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
201 |
System.out.println("ICCE I2.invokeInterfaceObjectFinalMH D1"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
202 |
shouldThrowICCE(() -> I2.invokeInterfaceObjectFinalMH(unsafeCastI2(new D1()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
203 |
System.out.println("ICCE I2.invokeInterfaceObjectFinalMH E"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
204 |
shouldThrowICCE(() -> I2.invokeInterfaceObjectFinalMH(unsafeCastI2(new E()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
205 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
206 |
System.out.println("ICCE I3.invokeInterfaceMH D1"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
207 |
shouldThrowICCE(() -> I3.invokeInterfaceMH(unsafeCastI3(new D1()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
208 |
System.out.println("ICCE I3.invokeInterfaceMH E"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
209 |
shouldThrowICCE(() -> I3.invokeInterfaceMH(unsafeCastI3(new E()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
210 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
211 |
System.out.println("ICCE I4.invokeDirect D1"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
212 |
shouldThrowICCE(() -> I4.invokeDirect(unsafeCastI4(new D1()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
213 |
System.out.println("ICCE I4.invokeDirect E"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
214 |
shouldThrowICCE(() -> I4.invokeDirect(unsafeCastI4(new E()))); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
215 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
216 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
217 |
static void warmup() { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
218 |
for (int i = 0; i < 20_000; i++) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
219 |
runPositiveTests(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
220 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
221 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
222 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
223 |
public static void main(String[] args) throws Throwable { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
224 |
System.out.println("UNRESOLVED:"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
225 |
runNegativeTests(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
226 |
runPositiveTests(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
227 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
228 |
System.out.println("RESOLVED:"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
229 |
runNegativeTests(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
230 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
231 |
System.out.println("WARMUP:"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
232 |
warmup(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
233 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
234 |
System.out.println("COMPILED:"); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
235 |
runNegativeTests(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
236 |
runPositiveTests(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
237 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
238 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
239 |
static interface Test { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
240 |
void run() throws Throwable; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
241 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
242 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
243 |
static void shouldThrowICCE(Test t) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
244 |
shouldThrow(IncompatibleClassChangeError.class, |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
245 |
"does not implement the requested interface", t); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
246 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
247 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
248 |
// Depending on whether the exception originates in the linkResolver, the interpreter |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
249 |
// or the compiler, the message can be different - which is unfortunate and could be |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
250 |
// fixed. So we allow the listed reason or else a null message. |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
251 |
static void shouldThrow(Class<?> expectedError, String reason, Test t) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
252 |
try { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
253 |
t.run(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
254 |
} catch (Throwable e) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
255 |
// Don't accept subclasses as they can hide unexpected failure modes |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
256 |
if (expectedError == e.getClass()) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
257 |
String msg = e.getMessage(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
258 |
if ((msg != null && msg.contains(reason)) || msg == null) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
259 |
// passed |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
260 |
System.out.println("Threw expected: " + e); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
261 |
return; |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
262 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
263 |
else { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
264 |
throw new AssertionError("Wrong exception reason: expected '" + reason |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
265 |
+ "', got '" + msg + "'", e); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
266 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
267 |
} else { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
268 |
String msg = String.format("Wrong exception thrown: expected=%s; thrown=%s", |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
269 |
expectedError.getName(), e.getClass().getName()); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
270 |
throw new AssertionError(msg, e); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
271 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
272 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
273 |
throw new AssertionError("No exception thrown: expected " + expectedError.getName()); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
274 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
275 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
276 |
static void shouldNotThrow(Test t) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
277 |
try { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
278 |
t.run(); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
279 |
// passed |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
280 |
} catch (Throwable e) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
281 |
throw new AssertionError("Exception was thrown: ", e); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
282 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
283 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
284 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
285 |
// Note: these unsafe casts are only possible for interface types |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
286 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
287 |
static I2 unsafeCastI2(Object obj) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
288 |
try { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
289 |
MethodHandle mh = MethodHandles.identity(Object.class); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
290 |
mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I2.class)); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
291 |
return (I2)mh.invokeExact((Object) obj); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
292 |
} catch (Throwable e) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
293 |
throw new Error(e); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
294 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
295 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
296 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
297 |
static I3 unsafeCastI3(Object obj) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
298 |
try { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
299 |
MethodHandle mh = MethodHandles.identity(Object.class); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
300 |
mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I3.class)); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
301 |
return (I3)mh.invokeExact((Object) obj); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
302 |
} catch (Throwable e) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
303 |
throw new Error(e); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
304 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
305 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
306 |
|
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
307 |
static I4 unsafeCastI4(Object obj) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
308 |
try { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
309 |
MethodHandle mh = MethodHandles.identity(Object.class); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
310 |
mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I4.class)); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
311 |
return (I4)mh.invokeExact((Object) obj); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
312 |
} catch (Throwable e) { |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
313 |
throw new Error(e); |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
314 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
315 |
} |
2f2af62dfac7
8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents:
diff
changeset
|
316 |
} |