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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/*
* Copyright (c) 2023, 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.
*/
import java.lang.classfile.ClassHierarchyResolver;
import java.lang.classfile.ClassFile;
import org.junit.jupiter.api.Test;
import java.io.Closeable;
import java.io.IOException;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.WrongMethodTypeException;
import java.lang.reflect.AccessFlag;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.IntSupplier;
import java.util.function.ToLongFunction;
import static java.lang.constant.ConstantDescs.*;
import static java.lang.invoke.MethodHandleProxies.*;
import static java.lang.invoke.MethodType.genericMethodType;
import static java.lang.invoke.MethodType.methodType;
import static java.lang.classfile.ClassFile.*;
import static org.junit.jupiter.api.Assertions.*;
/*
* @test
* @bug 6983726 8206955 8269351
* @summary Basic sanity tests for MethodHandleProxies
* @build BasicTest Client
* @run junit BasicTest
*/
public class BasicTest {
@Test
public void testUsual() throws Throwable {
AtomicInteger ai = new AtomicInteger(5);
var mh = MethodHandles.lookup().findVirtual(AtomicInteger.class, "getAndIncrement", methodType(int.class));
IntSupplier is = asInterfaceInstance(IntSupplier.class, mh.bindTo(ai));
assertEquals(5, is.getAsInt());
assertEquals(6, is.getAsInt());
assertEquals(7, is.getAsInt());
}
/**
* Established null behaviors of MHP API.
*/
@Test
public void testNulls() {
assertThrows(NullPointerException.class, () ->
asInterfaceInstance(null, MethodHandles.zero(void.class)),
"asInterfaceInstance - intfc");
assertThrows(NullPointerException.class, () ->
asInterfaceInstance(Runnable.class, null),
"asInterfaceInstance - target");
assertFalse(isWrapperInstance(null), "isWrapperInstance");
assertThrows(IllegalArgumentException.class, () -> wrapperInstanceTarget(null),
"wrapperInstanceTarget");
assertThrows(IllegalArgumentException.class, () -> wrapperInstanceType(null),
"wrapperInstanceType");
}
@Test
public void testWrapperInstance() throws Throwable {
var mh = MethodHandles.publicLookup()
.findVirtual(Integer.class, "compareTo", methodType(int.class, Integer.class));
@SuppressWarnings("unchecked")
Comparator<Integer> proxy = (Comparator<Integer>) asInterfaceInstance(Comparator.class, mh);
assertTrue(isWrapperInstance(proxy));
assertSame(mh, wrapperInstanceTarget(proxy));
assertSame(Comparator.class, wrapperInstanceType(proxy));
}
/**
* Tests undeclared exceptions and declared exceptions in proxies.
*/
@Test
public void testThrowables() {
// don't wrap
assertThrows(Error.class, throwing(Error.class, new Error())::close,
"Errors should be propagated");
assertThrows(RuntimeException.class, throwing(RuntimeException.class, new RuntimeException())::close,
"RuntimeException should be propagated");
assertThrows(IOException.class, throwing(IOException.class, new IOException())::close,
"Declared IOException should be propagated");
// wrap
assertThrows(UndeclaredThrowableException.class, throwing(IllegalAccessException.class,
new IllegalAccessException())::close,
"Undeclared IllegalAccessException should be wrapped");
}
/**
* Tests that invalid interfaces are rejected.
*/
@Test
public void testRejects() {
var mh = MethodHandles.constant(String.class, "42");
assertThrows(IllegalArgumentException.class, () -> asInterfaceInstance(PackagePrivate.class, mh),
"non-public interface");
assertThrows(IllegalArgumentException.class, () -> asInterfaceInstance(loadHidden(), mh),
"hidden interface");
assertThrows(IllegalArgumentException.class, () -> asInterfaceInstance(MultiAbstractMethods.class, mh),
"multiple abstract method names");
assertThrows(IllegalArgumentException.class, () -> asInterfaceInstance(NoAbstractMethods.class, mh),
"no abstract method");
assertThrows(IllegalArgumentException.class, () -> asInterfaceInstance(Sealed.class, mh),
"sealed interface");
}
/**
* Tests that non-sealed interfaces can be implemented.
*/
@Test
public void testNonSealed() {
MethodHandle target = MethodHandles.constant(String.class, "Non-Sealed");
NonSealed proxy = asInterfaceInstance(NonSealed.class, target);
assertEquals(proxy.m(), "Non-Sealed");
}
/**
* Tests that Proxy correctly proxies potential bridge abstract methods.
*/
@Test
public void testMultiSameName() throws Throwable {
var baseAndChild = loadBaseAndChild();
var baseClass = baseAndChild.get(0);
var childClass = baseAndChild.get(1);
checkMethods(childClass.getMethods());
checkMethods(childClass.getDeclaredMethods());
var lookup = MethodHandles.lookup();
var baseValueMh = lookup.findVirtual(baseClass, "value", genericMethodType(0))
.asType(genericMethodType(1));
var childIntegerValueMh = lookup.findVirtual(childClass, "value", methodType(Integer.class))
.asType(methodType(Integer.class, Object.class));
var childIntValueMh = lookup.findVirtual(childClass, "value", methodType(int.class))
.asType(methodType(int.class, Object.class));
Object child = asInterfaceInstance(childClass, MethodHandles.constant(Integer.class, 7));
assertEquals(7, (Object) baseValueMh.invokeExact(child));
assertEquals(7, (Integer) childIntegerValueMh.invokeExact(child));
assertEquals(7, (int) childIntValueMh.invokeExact(child));
}
/**
* Tests that default methods can be used.
*/
@Test
public void testDefaultMethods() {
MethodHandle target = MethodHandles.constant(String.class, "F");
C proxy = asInterfaceInstance(C.class, target);
assertEquals(proxy.f(), "F");
assertEquals(proxy.a(), "A");
assertEquals(proxy.b(), "B");
assertEquals(proxy.c(), "C");
assertEquals(proxy.concat(), "ABC");
}
/**
* Tests that correct implementation of default methods are called,
* and correct abstract methods are implemented.
*/
@Test
public void testOverrides() {
MethodHandle target = MethodHandles.constant(String.class, "concat");
D proxy = asInterfaceInstance(D.class, target);
assertEquals(proxy.a(), "OA");
assertEquals(proxy.b(), "OB");
assertEquals(proxy.c(), "OC");
assertEquals(proxy.f(), "OF");
assertEquals(proxy.concat(), "concat");
}
/**
* Tests primitive type conversions in proxies.
*/
@Test
public void testPrimitiveConversion() throws Throwable {
var mh = MethodHandles.lookup().findStatic(BasicTest.class, "mul",
methodType(long.class, int.class));
@SuppressWarnings("unchecked")
Function<Integer, Long> func = (Function<Integer, Long>) asInterfaceInstance(Function.class, mh);
assertEquals(32423432L * 32423432L, func.apply(32423432));
@SuppressWarnings("unchecked")
ToLongFunction<Integer> func1 = (ToLongFunction<Integer>) asInterfaceInstance(ToLongFunction.class, mh);
assertEquals(32423432L * 32423432L, func1.applyAsLong(32423432));
@SuppressWarnings("unchecked")
IntFunction<Long> func2 = (IntFunction<Long>) asInterfaceInstance(IntFunction.class, mh);
assertEquals(32423432L * 32423432L, func2.apply(32423432));
}
/**
* Tests common type conversions in proxies.
*/
@Test
public void testBasicConversion() {
var mh = MethodHandles.constant(String.class, "42");
asInterfaceInstance(Client.class, mh).exec(); // return value dropped, runs fine
var nullMh = MethodHandles.zero(String.class);
var badIterable = asInterfaceInstance(Iterable.class, nullMh);
assertNull(badIterable.iterator()); // null is convertible
}
/**
* Tests incompatible type conversions in proxy construction.
*/
@Test
public void testWrongConversion() {
var mh = MethodHandles.constant(String.class, "42");
assertThrows(WrongMethodTypeException.class, () -> asInterfaceInstance(IntSupplier.class, mh),
"cannot convert String return to int under any circumstance");
var proxy = asInterfaceInstance(Iterable.class, mh);
assertThrows(ClassCastException.class, proxy::iterator);
}
private static <T extends Throwable> Closeable throwing(Class<T> clz, T value) {
return asInterfaceInstance(Closeable.class, MethodHandles.throwException(void.class, clz).bindTo(value));
}
private static long mul(int i) {
return (long) i * i;
}
void checkMethods(Method[] methods) {
assertTrue(methods.length > 1, () -> "Should have more than 1 declared methods, found only " + Arrays.toString(methods));
for (Method method : methods) {
assertTrue(method.accessFlags().contains(AccessFlag.ABSTRACT), () -> method + " is not abstract");
}
}
private Class<?> loadHidden() {
try (var is = BasicTest.class.getResourceAsStream("Client.class")) {
var bytes = Objects.requireNonNull(is).readAllBytes();
var lookup = MethodHandles.lookup();
return lookup.defineHiddenClass(bytes, true).lookupClass();
} catch (Throwable ex) {
return fail("Hidden interface loading failure", ex);
}
}
// Base: Object value();
// Child: Integer value(); int value();
private List<Class<?>> loadBaseAndChild() throws IllegalAccessException {
ClassDesc baseCd = ClassDesc.of("BasicTest$Base");
ClassDesc childCd = ClassDesc.of("BasicTest$Child");
var objMtd = MethodTypeDesc.of(CD_Object);
var integerMtd = MethodTypeDesc.of(CD_Integer);
var intMtd = MethodTypeDesc.of(CD_int);
var classfile = ClassFile.of(ClassFile.ClassHierarchyResolverOption.of(ClassHierarchyResolver.defaultResolver().orElse(
ClassHierarchyResolver.of(List.of(baseCd, childCd), Map.ofEntries(Map.entry(baseCd, CD_Object),
Map.entry(childCd, CD_Object))))));
var baseBytes = classfile.build(baseCd, clb -> {
clb.withSuperclass(CD_Object);
clb.withFlags(ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT);
clb.withMethod("value", objMtd, ACC_PUBLIC | ACC_ABSTRACT, mb -> {});
});
var lookup = MethodHandles.lookup();
var base = lookup.ensureInitialized(lookup.defineClass(baseBytes));
var childBytes = classfile.build(childCd, clb -> {
clb.withSuperclass(CD_Object);
clb.withInterfaceSymbols(baseCd);
clb.withFlags(ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT);
clb.withMethod("value", integerMtd, ACC_PUBLIC | ACC_ABSTRACT, mb -> {});
clb.withMethod("value", intMtd, ACC_PUBLIC | ACC_ABSTRACT, mb -> {});
});
var child = lookup.ensureInitialized(lookup.defineClass(childBytes));
return List.of(base, child);
}
public interface MultiAbstractMethods {
String a();
String b();
}
public interface NoAbstractMethods {
String toString();
}
interface PackagePrivate {
Object value();
}
public interface A {
default String a() {
return "A";
}
}
public interface B {
default String b() {
return "B";
}
}
public interface C extends A, B {
String f();
default String c() {
return "C";
}
default String concat() {
return a() + b() + c();
}
}
public interface D extends C {
String concat();
default String f() {
return "OF";
}
default String a() {
return "OA";
}
default String b() {
return "OB";
}
default String c() {
return "OC";
}
}
public sealed interface Sealed permits NonSealed {
String m();
}
public non-sealed interface NonSealed extends Sealed {
}
}
|