1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8200167 8010319
* @summary Test direct and MethodHandle access to interface methods using invokespecial semantics
* @comment This must be compiled so invokespecial is used
* @compile -XDdisableVirtualizedPrivateInvoke SpecialInterfaceCall.java
* @compile SpecialInterfaceCallI4.jasm
* @run main/othervm -Xint SpecialInterfaceCall
* @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=1 SpecialInterfaceCall
* @run main/othervm -Xbatch -XX:-TieredCompilation SpecialInterfaceCall
*/
import java.lang.invoke.*;
public class SpecialInterfaceCall {
interface I1 {
default void pub_m() {};
private void priv_m() {};
}
interface I2 extends I1 {
// This needs to be a public method to avoid access control issues,
// but logically we treat it as private and emulate invokespecial
// using MethodHandles.
default void pub_m() {};
private void priv_m() {};
static void invokeDirect(I2 i) {
i.priv_m(); // generates invokespecial
}
static void invokeSpecialMH(I2 i) throws Throwable {
// emulates behaviour of invokeDirect
mh_I2_priv_m_from_I2.invokeExact(i);
}
// special case of invoking an Object method via an interface
static void invokeSpecialObjectMH(I2 i) throws Throwable {
// emulates invokespecial of I1.toString on i, which resolves
// to Object.toString
String s = (String) mh_I1_toString_from_I2.invokeExact(i);
}
// special case of invoking a final Object method via an interface
static void invokeSpecialObjectFinalMH(I2 i) throws Throwable {
// emulates invokespecial of I1.getClass on i, which resolves
// to Object.getClass
Class<?> c = (Class<?>) mh_I1_getClass_from_I2.invokeExact(i);
}
}
interface I3 extends I2 {
// Must take an I3 here rather than I2 else we get
// WrongMethodTypeException: expected (I3)void but found (I2)void
// Statically the receiver type is bounded by the caller type.
static void invokeSpecialMH(I3 i) throws Throwable {
// emulates an invokespecial of ((I2)i).pub_m()
mh_I2_pub_m_from_I3.invokeExact(i);
}
}
// This interface acts like I2 but we define directInvoke* methods
// that we will rewrite the bytecode of to use invokespecial
// (see SpecialInterfaceCallI4.jasm).
interface I4 extends I1 {
static void invokeDirect(I4 i) {
// invokeSpecial Object.toString()
throw new Error("Class file for I4 is not overwritten");
}
static void invokeDirectFinal(I4 i) {
// invokeSpecial Object.getClass() - final method
throw new Error("Class file for I4 is not overwritten");
}
}
// Concrete classes
static class C1 implements I1 { }
static class C2 implements I2 { }
static class C3 implements I3 { }
static class C4 implements I4 { }
// Classes that don't implement I2/I3 but do have a
// priv_m/pub_m method in their hierarchy
static class D1 implements I1 { }
static class E {
public void pub_m() {}
private void priv_m() {}
}
// This MH acts like the direct invokespecial in I2.invokeDirect
static final MethodHandle mh_I2_priv_m_from_I2;
// This MH acts like an invokespecial of I2.pub_m from I3
static final MethodHandle mh_I2_pub_m_from_I3;
// This MH acts likes an invokespecial of I1.toString from I2
static final MethodHandle mh_I1_toString_from_I2;
// This MH acts likes an invokespecial of I1.getClass from I2
static final MethodHandle mh_I1_getClass_from_I2;
static {
try {
MethodType mt = MethodType.methodType(void.class);
MethodHandles.Lookup lookup = MethodHandles.lookup();
mh_I2_priv_m_from_I2 = lookup.findSpecial(I2.class, "priv_m", mt, I2.class);
mh_I2_pub_m_from_I3 = lookup.findSpecial(I2.class, "pub_m", mt, I3.class);
mt = MethodType.methodType(String.class);
mh_I1_toString_from_I2 = lookup.findSpecial(I1.class, "toString", mt, I2.class);
mt = MethodType.methodType(Class.class);
mh_I1_getClass_from_I2 = lookup.findSpecial(I1.class, "getClass", mt, I2.class);
} catch (Throwable e) {
throw new Error(e);
}
}
static void runPositiveTests() {
shouldNotThrow(() -> I2.invokeDirect(new C2()));
shouldNotThrow(() -> I2.invokeDirect(new C3()));
shouldNotThrow(() -> I2.invokeSpecialMH(new C2()));
shouldNotThrow(() -> I2.invokeSpecialMH(new C3()));
shouldNotThrow(() -> I2.invokeSpecialObjectMH(new C2()));
shouldNotThrow(() -> I2.invokeSpecialObjectMH(new C3()));
shouldNotThrow(() -> I2.invokeSpecialObjectFinalMH(new C2()));
shouldNotThrow(() -> I2.invokeSpecialObjectFinalMH(new C3()));
shouldNotThrow(() -> I3.invokeSpecialMH(new C3()));
shouldNotThrow(() -> I4.invokeDirect(new C4()));
shouldNotThrow(() -> I4.invokeDirectFinal(new C4()));
}
static void runNegativeTests() {
System.out.println("IAE I2.invokeDirect D1");
shouldThrowIAE(() -> I2.invokeDirect(unsafeCastI2(new D1())));
System.out.println("IAE I2.invokeDirect E");
shouldThrowIAE(() -> I2.invokeDirect(unsafeCastI2(new E())));
System.out.println("ICCE I2.invokeMH D1");
shouldThrowICCE(() -> I2.invokeSpecialMH(unsafeCastI2(new D1())));
System.out.println("ICCE I2.invokeMH E");
shouldThrowICCE(() -> I2.invokeSpecialMH(unsafeCastI2(new E())));
System.out.println("ICCE I3.invokeMH D1");
shouldThrowICCE(() -> I3.invokeSpecialMH(unsafeCastI3(new D1())));
System.out.println("ICCE I3.invokeMH E");
shouldThrowICCE(() -> I3.invokeSpecialMH(unsafeCastI3(new E())));
System.out.println("ICCE I3.invokeMH C2");
shouldThrowICCE(() -> I3.invokeSpecialMH(unsafeCastI3(new C2())));
System.out.println("ICCE I4.invokeDirect C1");
shouldThrowIAE(() -> I4.invokeDirect(unsafeCastI4(new C1())));
System.out.println("ICCE I4.invokeDirectFinal C1");
shouldThrowIAE(() -> I4.invokeDirectFinal(unsafeCastI4(new C1())));
System.out.println("ICCE I2.invokeObjectMH C1");
shouldThrowICCE(() -> I2.invokeSpecialObjectMH(unsafeCastI2(new C1())));
System.out.println("ICCE I2.invokeObjectFinalMH C1");
shouldThrowICCE(() -> I2.invokeSpecialObjectFinalMH(unsafeCastI2(new C1())));
}
static void warmup() {
for (int i = 0; i < 20_000; i++) {
runPositiveTests();
}
}
public static void main(String[] args) throws Throwable {
System.out.println("UNRESOLVED:");
runNegativeTests();
runPositiveTests();
System.out.println("RESOLVED:");
runNegativeTests();
System.out.println("WARMUP:");
warmup();
System.out.println("COMPILED:");
runNegativeTests();
runPositiveTests();
}
static interface Test {
void run() throws Throwable;
}
static void shouldThrowICCE(Test t) {
shouldThrow(IncompatibleClassChangeError.class,
"is not a subclass of caller class", t);
}
static void shouldThrowIAE(Test t) {
shouldThrow(IllegalAccessError.class,
"must be the current class or a subtype of interface", t);
}
static void shouldThrow(Class<?> expectedError, String reason, Test t) {
try {
t.run();
} catch (Throwable e) {
if (expectedError.isInstance(e)) {
if (e.getMessage().contains(reason)) {
// passed
System.out.println("Threw expected: " + e);
return;
}
else {
throw new AssertionError("Wrong exception reason: expected '" + reason
+ "', got '" + e.getMessage() + "'", e);
}
} else {
String msg = String.format("Wrong exception thrown: expected=%s; thrown=%s",
expectedError.getName(), e.getClass().getName());
throw new AssertionError(msg, e);
}
}
throw new AssertionError("No exception thrown: expected " + expectedError.getName());
}
static void shouldNotThrow(Test t) {
try {
t.run();
// passed
} catch (Throwable e) {
throw new AssertionError("Exception was thrown: ", e);
}
}
// Note: these unsafe casts are only possible for interface types
static I2 unsafeCastI2(Object obj) {
try {
MethodHandle mh = MethodHandles.identity(Object.class);
mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I2.class));
return (I2)mh.invokeExact((Object) obj);
} catch (Throwable e) {
throw new Error(e);
}
}
static I3 unsafeCastI3(Object obj) {
try {
MethodHandle mh = MethodHandles.identity(Object.class);
mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I3.class));
return (I3)mh.invokeExact((Object) obj);
} catch (Throwable e) {
throw new Error(e);
}
}
static I4 unsafeCastI4(Object obj) {
try {
MethodHandle mh = MethodHandles.identity(Object.class);
mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I4.class));
return (I4)mh.invokeExact((Object) obj);
} catch (Throwable e) {
throw new Error(e);
}
}
}
|