TestMethods.java 34.6 KB
Newer Older
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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
/*
 * Copyright (c) 2014, 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 com.oracle.testlibrary.jsr292.Helper;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Enumeration containing information about methods from
 * {@code j.l.i.MethodHandles} class that are used for testing lambda forms
 * caching.
 *
 * @author kshefov
 */
public enum TestMethods {

    FOLD_ARGUMENTS("foldArguments") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    // Arity after reducing because of long and double take 2 slots.
                    int realArity = mtTarget.parameterCount();
                    int modifierMHArgNum = Helper.RNG.nextInt(realArity + 1);
                    data.put("modifierMHArgNum", modifierMHArgNum);
                    Class<?> combinerReturnType;
                    if (realArity == 0) {
                        combinerReturnType = void.class;
                    } else {
                        combinerReturnType = Helper.RNG.nextBoolean() ? void.class : mtTarget.parameterType(0);
                    }
                    data.put("combinerReturnType", combinerReturnType);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    Class<?> combinerReturnType = (Class) data.get("combinerReturnType");
                    int modifierMHArgNum = (int) data.get("modifierMHArgNum");
                    MethodHandle target = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                            mtTarget.parameterList(), kind);
                    Class<?> rType = mtTarget.returnType();
                    int combListStart = (combinerReturnType == void.class) ? 0 : 1;
                    if (modifierMHArgNum < combListStart) {
                        modifierMHArgNum = combListStart;
                    }
                    MethodHandle combiner = TestMethods.methodHandleGenerator(combinerReturnType,
                            mtTarget.parameterList().subList(combListStart,
                                    modifierMHArgNum), kind);
                    return MethodHandles.foldArguments(target, combiner);
                }
            },
    DROP_ARGUMENTS("dropArguments") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    // Arity after reducing because of long and double take 2 slots.
                    int realArity = mtTarget.parameterCount();
                    int dropArgsPos = Helper.RNG.nextInt(realArity + 1);
                    data.put("dropArgsPos", dropArgsPos);
                    MethodType mtDropArgs = TestMethods.randomMethodTypeGenerator(
                            Helper.RNG.nextInt(Helper.MAX_ARITY - realArity));
                    data.put("mtDropArgs", mtDropArgs);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    MethodType mtDropArgs = (MethodType) data.get("mtDropArgs");
                    int dropArgsPos = (int) data.get("dropArgsPos");
                    MethodHandle target = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                            mtTarget.parameterList(), kind);
                    int mtTgtSlotsCount = TestMethods.argSlotsCount(mtTarget);
                    int mtDASlotsCount = TestMethods.argSlotsCount(mtDropArgs);
                    List<Class<?>> fakeParList;
                    if (mtTgtSlotsCount + mtDASlotsCount > Helper.MAX_ARITY - 1) {
                        fakeParList = TestMethods.reduceArgListToSlotsCount(mtDropArgs.parameterList(),
                                Helper.MAX_ARITY - mtTgtSlotsCount - 1);
                    } else {
                        fakeParList = mtDropArgs.parameterList();
                    }
                    return MethodHandles.dropArguments(target, dropArgsPos, fakeParList);
                }
            },
    EXPLICIT_CAST_ARGUMENTS("explicitCastArguments") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY / 2);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    // Arity after reducing because of long and double take 2 slots.
                    int realArity = mtTarget.parameterCount();
                    MethodType mtExcplCastArgs = TestMethods.randomMethodTypeGenerator(realArity);
                    if (mtTarget.returnType() == void.class) {
                        mtExcplCastArgs = MethodType.methodType(void.class,
                                mtExcplCastArgs.parameterArray());
                    }
                    if (mtExcplCastArgs.returnType() == void.class) {
                        mtExcplCastArgs = MethodType.methodType(mtTarget.returnType(),
                                mtExcplCastArgs.parameterArray());
                    }
                    data.put("mtExcplCastArgs", mtExcplCastArgs);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    MethodType mtExcplCastArgs = (MethodType) data.get("mtExcplCastArgs");
                    MethodHandle target = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                            mtTarget.parameterList(), kind);
                    return MethodHandles.explicitCastArguments(target, mtExcplCastArgs);
                }
            },
    FILTER_ARGUMENTS("filterArguments") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY / 2);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    // Arity after reducing because of long and double take 2 slots.
                    int realArity = mtTarget.parameterCount();
                    int filterArgsPos = Helper.RNG.nextInt(realArity + 1);
                    data.put("filterArgsPos", filterArgsPos);
                    int filtersArgsArrayLength = Helper.RNG.nextInt(realArity + 1 - filterArgsPos);
                    data.put("filtersArgsArrayLength", filtersArgsArrayLength);
                    MethodType mtFilter = TestMethods.randomMethodTypeGenerator(filtersArgsArrayLength);
                    data.put("mtFilter", mtFilter);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    MethodType mtFilter = (MethodType) data.get("mtFilter");
                    int filterArgsPos = (int) data.get("filterArgsPos");
                    int filtersArgsArrayLength = (int) data.get("filtersArgsArrayLength");
                    MethodHandle target = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                            mtTarget.parameterList(), kind);
                    MethodHandle[] filters = new MethodHandle[filtersArgsArrayLength];
                    for (int i = 0; i < filtersArgsArrayLength; i++) {
                        filters[i] = TestMethods.filterGenerator(mtFilter.parameterType(i),
                                mtTarget.parameterType(filterArgsPos + i), kind);
                    }
                    return MethodHandles.filterArguments(target, filterArgsPos, filters);
                }
            },
    FILTER_RETURN_VALUE("filterReturnValue") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    // Arity after reducing because of long and double take 2 slots.
                    int realArity = mtTarget.parameterCount();
                    int filterArgsPos = Helper.RNG.nextInt(realArity + 1);
                    int filtersArgsArrayLength = Helper.RNG.nextInt(realArity + 1 - filterArgsPos);
                    MethodType mtFilter = TestMethods.randomMethodTypeGenerator(filtersArgsArrayLength);
                    data.put("mtFilter", mtFilter);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    MethodType mtFilter = (MethodType) data.get("mtFilter");
                    MethodHandle target = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                            mtTarget.parameterList(), kind);
                    MethodHandle filter = TestMethods.filterGenerator(mtTarget.returnType(),
                            mtFilter.returnType(), kind);
                    return MethodHandles.filterReturnValue(target, filter);
                }
            },
    INSERT_ARGUMENTS("insertArguments") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    // Arity after reducing because of long and double take 2 slots.
                    int realArity = mtTarget.parameterCount();
                    int insertArgsPos = Helper.RNG.nextInt(realArity + 1);
                    data.put("insertArgsPos", insertArgsPos);
                    int insertArgsArrayLength = Helper.RNG.nextInt(realArity + 1 - insertArgsPos);
                    MethodType mtInsertArgs = MethodType.methodType(void.class, mtTarget.parameterList()
                            .subList(insertArgsPos, insertArgsPos + insertArgsArrayLength));
                    data.put("mtInsertArgs", mtInsertArgs);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    MethodType mtInsertArgs = (MethodType) data.get("mtInsertArgs");
                    int insertArgsPos = (int) data.get("insertArgsPos");
                    MethodHandle target = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                            mtTarget.parameterList(), kind);
                    Object[] insertList = Helper.randomArgs(mtInsertArgs.parameterList());
                    return MethodHandles.insertArguments(target, insertArgsPos, insertList);
                }
            },
    PERMUTE_ARGUMENTS("permuteArguments") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY / 2);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    // Arity after reducing because of long and double take 2 slots.
                    int realArity = mtTarget.parameterCount();
                    int[] permuteArgsReorderArray = new int[realArity];
                    int mtParmuteArgsNum = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    mtParmuteArgsNum = mtParmuteArgsNum == 0 ? 1 : mtParmuteArgsNum;
                    MethodType mtPermuteArgs = TestMethods.randomMethodTypeGenerator(mtParmuteArgsNum);
                    mtTarget = mtTarget.changeReturnType(mtPermuteArgs.returnType());
                    for (int i = 0; i < realArity; i++) {
                        int mtPermuteArgsParNum = Helper.RNG.nextInt(mtPermuteArgs.parameterCount());
                        permuteArgsReorderArray[i] = mtPermuteArgsParNum;
                        mtTarget = mtTarget.changeParameterType(
                                i, mtPermuteArgs.parameterType(mtPermuteArgsParNum));
                    }
                    data.put("mtTarget", mtTarget);
                    data.put("permuteArgsReorderArray", permuteArgsReorderArray);
                    data.put("mtPermuteArgs", mtPermuteArgs);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    MethodType mtPermuteArgs = (MethodType) data.get("mtPermuteArgs");
                    int[] permuteArgsReorderArray = (int[]) data.get("permuteArgsReorderArray");
                    MethodHandle target = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                            mtTarget.parameterList(), kind);
                    return MethodHandles.permuteArguments(target, mtPermuteArgs, permuteArgsReorderArray);
                }
            },
    THROW_EXCEPTION("throwException") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    Class<?> rType = mtTarget.returnType();
                    return MethodHandles.throwException(rType, Exception.class
                    );
                }
            },
    GUARD_WITH_TEST("guardWithTest") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    // Arity after reducing because of long and double take 2 slots.
                    int realArity = mtTarget.parameterCount();
                    int modifierMHArgNum = Helper.RNG.nextInt(realArity + 1);
                    data.put("modifierMHArgNum", modifierMHArgNum);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    int modifierMHArgNum = (int) data.get("modifierMHArgNum");
                    TestMethods.Kind targetKind;
                    TestMethods.Kind fallbackKind;
                    if (kind.equals(TestMethods.Kind.ONE)) {
                        targetKind = TestMethods.Kind.ONE;
                        fallbackKind = TestMethods.Kind.TWO;
                    } else {
                        targetKind = TestMethods.Kind.TWO;
                        fallbackKind = TestMethods.Kind.ONE;
                    }
                    MethodHandle target = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                            mtTarget.parameterList(), targetKind);
                    MethodHandle fallback = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                            mtTarget.parameterList(), fallbackKind);
                    MethodHandle test = TestMethods.methodHandleGenerator(boolean.class,
                            mtTarget.parameterList().subList(0, modifierMHArgNum), kind);
                    return MethodHandles.guardWithTest(test, target, fallback);
                }
            },
    CATCH_EXCEPTION("catchException") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    // Arity after reducing because of long and double take 2 slots.
                    int realArity = mtTarget.parameterCount();
                    int modifierMHArgNum = Helper.RNG.nextInt(realArity + 1);
                    data.put("modifierMHArgNum", modifierMHArgNum);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    int modifierMHArgNum = (int) data.get("modifierMHArgNum");
                    MethodHandle target;
                    if (kind.equals(TestMethods.Kind.ONE)) {
                        target = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                                mtTarget.parameterList(), TestMethods.Kind.ONE);
                    } else {
                        target = TestMethods.methodHandleGenerator(mtTarget.returnType(),
                                mtTarget.parameterList(), TestMethods.Kind.EXCEPT);
                    }
                    List<Class<?>> handlerParamList = new ArrayList<>(mtTarget.parameterCount() + 1);
                    handlerParamList.add(Exception.class);
                    handlerParamList.addAll(mtTarget.parameterList().subList(0, modifierMHArgNum));
                    MethodHandle handler = TestMethods.methodHandleGenerator(
                            mtTarget.returnType(), handlerParamList, TestMethods.Kind.TWO);
                    return MethodHandles.catchException(target, Exception.class, handler);
                }
            },
    INVOKER("invoker") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    return MethodHandles.invoker(mtTarget);
                }
            },
    EXACT_INVOKER("exactInvoker") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    return MethodHandles.exactInvoker(mtTarget);
                }
            },
    SPREAD_INVOKER("spreadInvoker") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    // Arity after reducing because of long and double take 2 slots.
                    int realArity = mtTarget.parameterCount();
                    int modifierMHArgNum = Helper.RNG.nextInt(realArity + 1);
                    data.put("modifierMHArgNum", modifierMHArgNum);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    int modifierMHArgNum = (int) data.get("modifierMHArgNum");
                    return MethodHandles.spreadInvoker(mtTarget, modifierMHArgNum);
                }
            },
    ARRAY_ELEMENT_GETTER("arrayElementGetter") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    Class<?> rType = mtTarget.returnType();
                    if (rType == void.class) {
                        rType = Object.class;
                    }
                    return MethodHandles.arrayElementGetter(Array.newInstance(rType, 2).getClass());
                }
            },
    ARRAY_ELEMENT_SETTER("arrayElementSetter") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    Class<?> rType = mtTarget.returnType();
                    if (rType == void.class) {
                        rType = Object.class;
                    }
                    return MethodHandles.arrayElementSetter(Array.newInstance(rType, 2).getClass());
                }
            },
    CONSTANT("constant") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    Class<?> rType = mtTarget.returnType();
                    if (rType == void.class) {
                        rType = Object.class;
                    }
                    if (rType.equals(boolean.class)) {
                        // There should be the same return values because for default values there are special "zero" forms
                        return MethodHandles.constant(rType, true);
                    } else {
                        return MethodHandles.constant(rType, kind.getValue(rType));
                    }
                }
            },
    IDENTITY("identity") {
                @Override
                public Map<String, Object> getTestCaseData() {
                    Map<String, Object> data = new HashMap<>();
                    int desiredArity = Helper.RNG.nextInt(Helper.MAX_ARITY);
                    MethodType mtTarget = TestMethods.randomMethodTypeGenerator(desiredArity);
                    data.put("mtTarget", mtTarget);
                    return data;
                }

                @Override
                protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) {
                    MethodType mtTarget = (MethodType) data.get("mtTarget");
                    Class<?> rType = mtTarget.returnType();
                    if (rType == void.class) {
                        rType = Object.class;
                    }
                    return MethodHandles.identity(rType);
                }
            };

    /**
     * Test method's name.
     */
    public final String name;

    private TestMethods(String name) {
        this.name = name;
    }

    protected MethodHandle getMH(Map<String, Object> data, TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
        throw new UnsupportedOperationException("TESTBUG: getMH method is not implemented for test method " + this);
    }

    /**
     * Creates an adapter method handle depending on a test method from
     * MethodHandles class. Adapter is what is returned by the test method. This
     * method is able to create two kinds of adapters, their type will be the
     * same, but return values are different.
     *
     * @param data a Map containing data to create a method handle, can be
     * obtained by {@link #getTestCaseData} method
     * @param kind defines whether adapter ONE or adapter TWO will be
     * initialized. Should be equal to TestMethods.Kind.ONE or
     * TestMethods.Kind.TWO
     * @return Method handle adapter that behaves according to
     * TestMethods.Kind.ONE or TestMethods.Kind.TWO
     * @throws java.lang.NoSuchMethodException
     * @throws java.lang.IllegalAccessException
     */
    public MethodHandle getTestCaseMH(Map<String, Object> data, TestMethods.Kind kind)
            throws NoSuchMethodException, IllegalAccessException {
        if (data == null) {
            throw new Error(String.format("TESTBUG: Data for test method %s is not prepared",
                    this.name));
        }
        if (!kind.equals(TestMethods.Kind.ONE) && !kind.equals(TestMethods.Kind.TWO)) {
            throw new IllegalArgumentException("TESTBUG: Wrong \"kind\" (" + kind
                    + ") arg to getTestCaseMH function."
                    + " Should be Kind.ONE or Kind.TWO");
        }
        return getMH(data, kind);
    }

    /**
     * Returns a data Map needed for {@link #getTestCaseMH} method.
     *
     * @return data Map needed for {@link #getTestCaseMH} method
     */
    public Map<String, Object> getTestCaseData() {
        throw new UnsupportedOperationException(
                "TESTBUG: getTestCaseData method is not implemented for test method " + this);
    }

    /**
     * Enumeration used in methodHandleGenerator to define whether a MH returned
     * by this method returns "2" in different type representations, "4", or
     * throw an Exception.
     */
    public static enum Kind {

        ONE(2),
        TWO(4),
        EXCEPT(0);

        private final int value;

        private Object getValue(Class<?> cl) {
            return Helper.castToWrapper(value, cl);
        }

        private MethodHandle getBasicMH(Class<?> rType) throws NoSuchMethodException, IllegalAccessException {
            MethodHandle result = null;
            switch (this) {
                case ONE:
                case TWO:
                    if (rType.equals(void.class)) {
                        result = MethodHandles.lookup().findVirtual(Kind.class, "returnVoid", MethodType.methodType(void.class));
                        result = MethodHandles.insertArguments(result, 0, this);
                    } else {
                        result = MethodHandles.constant(rType, getValue(rType));
                    }
                    break;
                case EXCEPT:
                    result = MethodHandles.throwException(rType, Exception.class);
                    result = MethodHandles.insertArguments(result, 0, new Exception());
                    break;
            }
            return result;
        }

        private void returnVoid() {
        }

        private Kind(int value) {
            this.value = value;
        }
    }

    /**
     * Routine used to obtain a randomly generated method type.
     *
     * @param arity Arity of returned method type.
     * @return MethodType generated randomly.
     */
    private static MethodType randomMethodTypeGenerator(int arity) {
        final Class<?>[] CLASSES = {
            Object.class,
            int.class,
            boolean.class,
            byte.class,
            short.class,
            char.class,
            long.class,
            float.class,
            double.class
        };
        if (arity > Helper.MAX_ARITY) {
            throw new IllegalArgumentException(
                    String.format("Arity should not exceed %d!", Helper.MAX_ARITY));
        }
        List<Class<?>> list = Helper.randomClasses(CLASSES, arity);
        list = Helper.getParams(list, false, arity);
        int i = Helper.RNG.nextInt(CLASSES.length + 1);
        Class<?> rtype = i == CLASSES.length ? void.class : CLASSES[i];
        return MethodType.methodType(rtype, list);
    }

    /**
     * Routine used to obtain a method handles of a given type an kind (return
     * value).
     *
     * @param returnType Type of MH return value.
     * @param argTypes Types of MH args.
     * @param kind Defines whether the obtained MH returns "1" or "2".
     * @return Method handle of the given type.
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     */
    private static MethodHandle methodHandleGenerator(Class<?> returnType,
            List<Class<?>> argTypes, TestMethods.Kind kind)
            throws NoSuchMethodException, IllegalAccessException {
        MethodHandle result;
        result = kind.getBasicMH(returnType);
        return Helper.addTrailingArgs(result, argTypes.size(), argTypes);
    }

    /**
     * Routine that generates filter method handles to test
     * MethodHandles.filterArguments method.
     *
     * @param inputType Filter's argument type.
     * @param returnType Filter's return type.
     * @param kind Filter's return value definer.
     * @return A filter method handle, that takes one argument.
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     */
    private static MethodHandle filterGenerator(Class<?> inputType, Class<?> returnType,
            TestMethods.Kind kind) throws NoSuchMethodException, IllegalAccessException {
        MethodHandle tmpMH = kind.getBasicMH(returnType);
        if (inputType.equals(void.class)) {
            return tmpMH;
        }
        ArrayList<Class<?>> inputTypeList = new ArrayList<>(1);
        inputTypeList.add(inputType);
        return Helper.addTrailingArgs(tmpMH, 1, inputTypeList);
    }

    private static int argSlotsCount(MethodType mt) {
        int result = 0;
        for (Class cl : mt.parameterArray()) {
            if (cl.equals(long.class) || cl.equals(double.class)) {
                result += 2;
            } else {
                result++;
            }
        }
        return result;
    }

    private static List<Class<?>> reduceArgListToSlotsCount(List<Class<?>> list,
            int desiredSlotCount) {
        List<Class<?>> result = new ArrayList<>(desiredSlotCount);
        int count = 0;
        for (Class<?> cl : list) {
            if (count >= desiredSlotCount) {
                break;
            }
            if (cl.equals(long.class) || cl.equals(double.class)) {
                count += 2;
            } else {
                count++;
            }
            result.add(cl);
        }
        return result;
    }
}