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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
/*
* Copyright (c) 2015, 2017, 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.invoke.MethodHandle;
import java.lang.invoke.MethodHandleInfo;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;
import java.lang.invoke.WrongMethodTypeException;
import java.lang.reflect.Method;
import java.nio.ReadOnlyBufferException;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
import static org.testng.Assert.*;
abstract class VarHandleBaseTest {
static final int ITERS = Integer.getInteger("iters", 1);
static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
interface ThrowingRunnable {
void run() throws Throwable;
}
static void checkUOE(ThrowingRunnable r) {
checkWithThrowable(UnsupportedOperationException.class, null, r);
}
static void checkUOE(Object message, ThrowingRunnable r) {
checkWithThrowable(UnsupportedOperationException.class, message, r);
}
static void checkROBE(ThrowingRunnable r) {
checkWithThrowable(ReadOnlyBufferException.class, null, r);
}
static void checkROBE(Object message, ThrowingRunnable r) {
checkWithThrowable(ReadOnlyBufferException.class, message, r);
}
static void checkIOOBE(ThrowingRunnable r) {
checkWithThrowable(IndexOutOfBoundsException.class, null, r);
}
static void checkIOOBE(Object message, ThrowingRunnable r) {
checkWithThrowable(IndexOutOfBoundsException.class, message, r);
}
static void checkASE(ThrowingRunnable r) {
checkWithThrowable(ArrayStoreException.class, null, r);
}
static void checkASE(Object message, ThrowingRunnable r) {
checkWithThrowable(ArrayStoreException.class, message, r);
}
static void checkISE(ThrowingRunnable r) {
checkWithThrowable(IllegalStateException.class, null, r);
}
static void checkISE(Object message, ThrowingRunnable r) {
checkWithThrowable(IllegalStateException.class, message, r);
}
static void checkIAE(ThrowingRunnable r) {
checkWithThrowable(IllegalAccessException.class, null, r);
}
static void checkIAE(Object message, ThrowingRunnable r) {
checkWithThrowable(IllegalAccessException.class, message, r);
}
static void checkWMTE(ThrowingRunnable r) {
checkWithThrowable(WrongMethodTypeException.class, null, r);
}
static void checkWMTE(Object message, ThrowingRunnable r) {
checkWithThrowable(WrongMethodTypeException.class, message, r);
}
static void checkCCE(ThrowingRunnable r) {
checkWithThrowable(ClassCastException.class, null, r);
}
static void checkCCE(Object message, ThrowingRunnable r) {
checkWithThrowable(ClassCastException.class, message, r);
}
static void checkNPE(ThrowingRunnable r) {
checkWithThrowable(NullPointerException.class, null, r);
}
static void checkNPE(Object message, ThrowingRunnable r) {
checkWithThrowable(NullPointerException.class, message, r);
}
static void checkWithThrowable(Class<? extends Throwable> re,
Object message,
ThrowingRunnable r) {
Throwable _e = null;
try {
r.run();
}
catch (Throwable e) {
_e = e;
}
message = message == null ? "" : message + ". ";
assertNotNull(_e, String.format("%sNo throwable thrown. Expected %s", message, re));
assertTrue(re.isInstance(_e), String.format("%sIncorrect throwable thrown, %s. Expected %s", message, _e, re));
}
enum TestAccessType {
GET,
SET,
COMPARE_AND_SET,
COMPARE_AND_EXCHANGE,
GET_AND_SET,
GET_AND_ADD,
GET_AND_BITWISE;
}
enum TestAccessMode {
GET(TestAccessType.GET),
SET(TestAccessType.SET),
GET_VOLATILE(TestAccessType.GET),
SET_VOLATILE(TestAccessType.SET),
GET_ACQUIRE(TestAccessType.GET),
SET_RELEASE(TestAccessType.SET),
GET_OPAQUE(TestAccessType.GET),
SET_OPAQUE(TestAccessType.SET),
COMPARE_AND_SET(TestAccessType.COMPARE_AND_SET),
COMPARE_AND_EXCHANGE(TestAccessType.COMPARE_AND_EXCHANGE),
COMPARE_AND_EXCHANGE_ACQUIRE(TestAccessType.COMPARE_AND_EXCHANGE),
COMPARE_AND_EXCHANGE_RELEASE(TestAccessType.COMPARE_AND_EXCHANGE),
WEAK_COMPARE_AND_SET_PLAIN(TestAccessType.COMPARE_AND_SET),
WEAK_COMPARE_AND_SET(TestAccessType.COMPARE_AND_SET),
WEAK_COMPARE_AND_SET_ACQUIRE(TestAccessType.COMPARE_AND_SET),
WEAK_COMPARE_AND_SET_RELEASE(TestAccessType.COMPARE_AND_SET),
GET_AND_SET(TestAccessType.GET_AND_SET),
GET_AND_SET_ACQUIRE(TestAccessType.GET_AND_SET),
GET_AND_SET_RELEASE(TestAccessType.GET_AND_SET),
GET_AND_ADD(TestAccessType.GET_AND_ADD),
GET_AND_ADD_ACQUIRE(TestAccessType.GET_AND_ADD),
GET_AND_ADD_RELEASE(TestAccessType.GET_AND_ADD),
GET_AND_BITWISE_OR(TestAccessType.GET_AND_BITWISE),
GET_AND_BITWISE_OR_ACQUIRE(TestAccessType.GET_AND_BITWISE),
GET_AND_BITWISE_OR_RELEASE(TestAccessType.GET_AND_BITWISE),
GET_AND_BITWISE_AND(TestAccessType.GET_AND_BITWISE),
GET_AND_BITWISE_AND_ACQUIRE(TestAccessType.GET_AND_BITWISE),
GET_AND_BITWISE_AND_RELEASE(TestAccessType.GET_AND_BITWISE),
GET_AND_BITWISE_XOR(TestAccessType.GET_AND_BITWISE),
GET_AND_BITWISE_XOR_ACQUIRE(TestAccessType.GET_AND_BITWISE),
GET_AND_BITWISE_XOR_RELEASE(TestAccessType.GET_AND_BITWISE),
;
final TestAccessType at;
final boolean isPolyMorphicInReturnType;
final Class<?> returnType;
TestAccessMode(TestAccessType at) {
this.at = at;
try {
VarHandle.AccessMode vh_am = toAccessMode();
Method m = VarHandle.class.getMethod(vh_am.methodName(), Object[].class);
this.returnType = m.getReturnType();
isPolyMorphicInReturnType = returnType != Object.class;
}
catch (Exception e) {
throw new Error(e);
}
}
boolean isOfType(TestAccessType at) {
return this.at == at;
}
VarHandle.AccessMode toAccessMode() {
return VarHandle.AccessMode.valueOf(name());
}
}
static List<TestAccessMode> testAccessModes() {
return Stream.of(TestAccessMode.values()).collect(toList());
}
static List<TestAccessMode> testAccessModesOfType(TestAccessType... ats) {
Stream<TestAccessMode> s = Stream.of(TestAccessMode.values());
return s.filter(e -> Stream.of(ats).anyMatch(e::isOfType))
.collect(toList());
}
static List<VarHandle.AccessMode> accessModes() {
return Stream.of(VarHandle.AccessMode.values()).collect(toList());
}
static List<VarHandle.AccessMode> accessModesOfType(TestAccessType... ats) {
Stream<TestAccessMode> s = Stream.of(TestAccessMode.values());
return s.filter(e -> Stream.of(ats).anyMatch(e::isOfType))
.map(TestAccessMode::toAccessMode)
.collect(toList());
}
static MethodHandle toMethodHandle(VarHandle vh, TestAccessMode tam, MethodType mt) {
return vh.toMethodHandle(tam.toAccessMode());
}
static MethodHandle findVirtual(VarHandle vh, TestAccessMode tam, MethodType mt) {
MethodHandle mh;
try {
mh = MethodHandles.publicLookup().
findVirtual(VarHandle.class,
tam.toAccessMode().methodName(),
mt);
} catch (Exception e) {
throw new RuntimeException(e);
}
return bind(vh, mh, mt);
}
static MethodHandle varHandleInvoker(VarHandle vh, TestAccessMode tam, MethodType mt) {
MethodHandle mh = MethodHandles.varHandleInvoker(
tam.toAccessMode(),
mt);
return bind(vh, mh, mt);
}
static MethodHandle varHandleExactInvoker(VarHandle vh, TestAccessMode tam, MethodType mt) {
MethodHandle mh = MethodHandles.varHandleExactInvoker(
tam.toAccessMode(),
mt);
return bind(vh, mh, mt);
}
private static MethodHandle bind(VarHandle vh, MethodHandle mh, MethodType emt) {
assertEquals(mh.type(), emt.insertParameterTypes(0, VarHandle.class),
"MethodHandle type differs from access mode type");
MethodHandleInfo info = MethodHandles.lookup().revealDirect(mh);
assertEquals(info.getMethodType(), emt,
"MethodHandleInfo method type differs from access mode type");
return mh.bindTo(vh);
}
private interface TriFunction<T, U, V, R> {
R apply(T t, U u, V v);
}
enum VarHandleToMethodHandle {
VAR_HANDLE_TO_METHOD_HANDLE(
"VarHandle.toMethodHandle",
true,
VarHandleBaseTest::toMethodHandle),
METHOD_HANDLES_LOOKUP_FIND_VIRTUAL(
"Lookup.findVirtual",
false,
VarHandleBaseTest::findVirtual),
METHOD_HANDLES_VAR_HANDLE_INVOKER(
"MethodHandles.varHandleInvoker",
false,
VarHandleBaseTest::varHandleInvoker),
METHOD_HANDLES_VAR_HANDLE_EXACT_INVOKER(
"MethodHandles.varHandleExactInvoker",
true,
VarHandleBaseTest::varHandleExactInvoker);
final String desc;
final boolean isExact;
final TriFunction<VarHandle, TestAccessMode, MethodType, MethodHandle> f;
VarHandleToMethodHandle(String desc, boolean isExact,
TriFunction<VarHandle, TestAccessMode, MethodType, MethodHandle> f) {
this.desc = desc;
this.f = f;
this.isExact = isExact;
}
MethodHandle apply(VarHandle vh, TestAccessMode am, MethodType mt) {
return f.apply(vh, am, mt);
}
@Override
public String toString() {
return desc;
}
}
static class Handles {
static class AccessModeAndType {
final TestAccessMode tam;
final MethodType t;
public AccessModeAndType(TestAccessMode tam, MethodType t) {
this.tam = tam;
this.t = t;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccessModeAndType x = (AccessModeAndType) o;
if (tam != x.tam) return false;
if (t != null ? !t.equals(x.t) : x.t != null) return false;
return true;
}
@Override
public int hashCode() {
int result = tam != null ? tam.hashCode() : 0;
result = 31 * result + (t != null ? t.hashCode() : 0);
return result;
}
}
final VarHandle vh;
final VarHandleToMethodHandle f;
final EnumMap<TestAccessMode, MethodType> amToType;
final Map<AccessModeAndType, MethodHandle> amToHandle;
Handles(VarHandle vh, VarHandleToMethodHandle f) throws Exception {
this.vh = vh;
this.f = f;
this.amToHandle = new HashMap<>();
amToType = new EnumMap<>(TestAccessMode.class);
for (TestAccessMode am : testAccessModes()) {
amToType.put(am, vh.accessModeType(am.toAccessMode()));
}
}
MethodHandle get(TestAccessMode am) {
return get(am, amToType.get(am));
}
MethodHandle get(TestAccessMode am, MethodType mt) {
AccessModeAndType amt = new AccessModeAndType(am, mt);
return amToHandle.computeIfAbsent(
amt, k -> f.apply(vh, am, mt));
}
Class<? extends Throwable> getWMTEOOrOther(Class<? extends Throwable> c) {
return f.isExact ? WrongMethodTypeException.class : c;
}
void checkWMTEOrCCE(ThrowingRunnable r) {
checkWithThrowable(getWMTEOOrOther(ClassCastException.class), null, r);
}
}
interface AccessTestAction<T> {
void action(T t) throws Throwable;
}
static abstract class AccessTestCase<T> {
final String desc;
final AccessTestAction<T> ata;
final boolean loop;
AccessTestCase(String desc, AccessTestAction<T> ata, boolean loop) {
this.desc = desc;
this.ata = ata;
this.loop = loop;
}
boolean requiresLoop() {
return loop;
}
abstract T get() throws Exception;
void testAccess(T t) throws Throwable {
ata.action(t);
}
@Override
public String toString() {
return desc;
}
}
static class VarHandleAccessTestCase extends AccessTestCase<VarHandle> {
final VarHandle vh;
VarHandleAccessTestCase(String desc, VarHandle vh, AccessTestAction<VarHandle> ata) {
this(desc, vh, ata, true);
}
VarHandleAccessTestCase(String desc, VarHandle vh, AccessTestAction<VarHandle> ata, boolean loop) {
super("VarHandle -> " + desc, ata, loop);
this.vh = vh;
}
@Override
VarHandle get() {
return vh;
}
}
static class MethodHandleAccessTestCase extends AccessTestCase<Handles> {
final VarHandle vh;
final VarHandleToMethodHandle f;
MethodHandleAccessTestCase(String desc, VarHandle vh, VarHandleToMethodHandle f, AccessTestAction<Handles> ata) {
this(desc, vh, f, ata, true);
}
MethodHandleAccessTestCase(String desc, VarHandle vh, VarHandleToMethodHandle f, AccessTestAction<Handles> ata, boolean loop) {
super("VarHandle -> " + f.toString() + " -> " + desc, ata, loop);
this.vh = vh;
this.f = f;
}
@Override
Handles get() throws Exception {
return new Handles(vh, f);
}
}
static void testTypes(VarHandle vh) {
List<Class<?>> pts = vh.coordinateTypes();
for (TestAccessMode accessMode : testAccessModes()) {
MethodType amt = vh.accessModeType(accessMode.toAccessMode());
assertEquals(amt.parameterList().subList(0, pts.size()), pts);
}
for (TestAccessMode testAccessMode : testAccessModesOfType(TestAccessType.GET)) {
MethodType mt = vh.accessModeType(testAccessMode.toAccessMode());
assertEquals(mt.returnType(), vh.varType());
assertEquals(mt.parameterList(), pts);
}
for (TestAccessMode testAccessMode : testAccessModesOfType(TestAccessType.SET)) {
MethodType mt = vh.accessModeType(testAccessMode.toAccessMode());
assertEquals(mt.returnType(), void.class);
assertEquals(mt.parameterType(mt.parameterCount() - 1), vh.varType());
}
for (TestAccessMode testAccessMode : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
MethodType mt = vh.accessModeType(testAccessMode.toAccessMode());
assertEquals(mt.returnType(), boolean.class);
assertEquals(mt.parameterType(mt.parameterCount() - 1), vh.varType());
assertEquals(mt.parameterType(mt.parameterCount() - 2), vh.varType());
}
for (TestAccessMode testAccessMode : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
MethodType mt = vh.accessModeType(testAccessMode.toAccessMode());
assertEquals(mt.returnType(), vh.varType());
assertEquals(mt.parameterType(mt.parameterCount() - 1), vh.varType());
assertEquals(mt.parameterType(mt.parameterCount() - 2), vh.varType());
}
for (TestAccessMode testAccessMode : testAccessModesOfType(TestAccessType.GET_AND_SET, TestAccessType.GET_AND_ADD)) {
MethodType mt = vh.accessModeType(testAccessMode.toAccessMode());
assertEquals(mt.returnType(), vh.varType());
assertEquals(mt.parameterType(mt.parameterCount() - 1), vh.varType());
}
}
}
|