jdk/test/java/lang/invoke/VarHandles/VarHandleBaseTest.java
author vlivanov
Mon, 09 May 2016 12:39:41 +0300
changeset 38358 cb99c6d2af1b
parent 37719 add11bc0e6e2
parent 38355 674cfd9b90cf
child 38368 c8eb5d6812c5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     1
/*
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     4
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     8
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    13
 * accompanied this code).
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    14
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    18
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    21
 * questions.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    22
 */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    23
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    24
import java.lang.invoke.MethodHandle;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    25
import java.lang.invoke.MethodHandleInfo;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    26
import java.lang.invoke.MethodHandles;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    27
import java.lang.invoke.MethodType;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    28
import java.lang.invoke.VarHandle;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    29
import java.lang.invoke.WrongMethodTypeException;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    30
import java.lang.reflect.Method;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    31
import java.nio.ReadOnlyBufferException;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    32
import java.util.EnumMap;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    33
import java.util.HashMap;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    34
import java.util.List;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    35
import java.util.Map;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    36
import java.util.stream.Stream;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    37
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    38
import static java.util.stream.Collectors.toList;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    39
import static org.testng.Assert.*;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    40
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    41
abstract class VarHandleBaseTest {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    42
    static final int ITERS = Integer.getInteger("iters", 1);
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
    43
    static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    44
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    45
    interface ThrowingRunnable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    46
        void run() throws Throwable;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    47
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    48
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    49
    static void checkUOE(ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    50
        checkWithThrowable(UnsupportedOperationException.class, null, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    51
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    52
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    53
    static void checkUOE(Object message, ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    54
        checkWithThrowable(UnsupportedOperationException.class, message, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    55
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    56
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    57
    static void checkROBE(ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    58
        checkWithThrowable(ReadOnlyBufferException.class, null, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    59
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    60
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    61
    static void checkROBE(Object message, ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    62
        checkWithThrowable(ReadOnlyBufferException.class, message, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    63
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    64
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    65
    static void checkIOOBE(ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    66
        checkWithThrowable(IndexOutOfBoundsException.class, null, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    67
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    68
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    69
    static void checkIOOBE(Object message, ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    70
        checkWithThrowable(IndexOutOfBoundsException.class, message, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    71
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    72
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    73
    static void checkISE(ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    74
        checkWithThrowable(IllegalStateException.class, null, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    75
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    76
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    77
    static void checkISE(Object message, ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    78
        checkWithThrowable(IllegalStateException.class, message, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    79
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    80
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    81
    static void checkIAE(ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    82
        checkWithThrowable(IllegalAccessException.class, null, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    83
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    84
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    85
    static void checkIAE(Object message, ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    86
        checkWithThrowable(IllegalAccessException.class, message, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    87
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    88
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    89
    static void checkWMTE(ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    90
        checkWithThrowable(WrongMethodTypeException.class, null, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    91
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    92
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    93
    static void checkWMTE(Object message, ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    94
        checkWithThrowable(WrongMethodTypeException.class, message, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    95
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    96
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    97
    static void checkCCE(ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    98
        checkWithThrowable(ClassCastException.class, null, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    99
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   100
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   101
    static void checkCCE(Object message, ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   102
        checkWithThrowable(ClassCastException.class, message, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   103
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   104
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   105
    static void checkNPE(ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   106
        checkWithThrowable(NullPointerException.class, null, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   107
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   108
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   109
    static void checkNPE(Object message, ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   110
        checkWithThrowable(NullPointerException.class, message, r);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   111
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   112
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   113
    static void checkWithThrowable(Class<? extends Throwable> re,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   114
                                   Object message,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   115
                                   ThrowingRunnable r) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   116
        Throwable _e = null;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   117
        try {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   118
            r.run();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   119
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   120
        catch (Throwable e) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   121
            _e = e;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   122
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   123
        message = message == null ? "" : message + ". ";
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   124
        assertNotNull(_e, String.format("%sNo throwable thrown. Expected %s", message, re));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   125
        assertTrue(re.isInstance(_e), String.format("%sIncorrect throwable thrown, %s. Expected %s", message, _e, re));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   126
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   127
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   128
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   129
    enum TestAccessType {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   130
        GET,
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   131
        SET,
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   132
        COMPARE_AND_SET,
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   133
        COMPARE_AND_EXCHANGE,
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   134
        GET_AND_SET,
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   135
        GET_AND_ADD;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   136
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   137
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   138
    enum TestAccessMode {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   139
        GET(TestAccessType.GET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   140
        SET(TestAccessType.SET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   141
        GET_VOLATILE(TestAccessType.GET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   142
        SET_VOLATILE(TestAccessType.SET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   143
        GET_ACQUIRE(TestAccessType.GET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   144
        SET_RELEASE(TestAccessType.SET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   145
        GET_OPAQUE(TestAccessType.GET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   146
        SET_OPAQUE(TestAccessType.SET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   147
        COMPARE_AND_SET(TestAccessType.COMPARE_AND_SET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   148
        COMPARE_AND_EXCHANGE_VOLATILE(TestAccessType.COMPARE_AND_EXCHANGE),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   149
        COMPARE_AND_EXCHANGE_ACQUIRE(TestAccessType.COMPARE_AND_EXCHANGE),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   150
        COMPARE_AND_EXCHANGE_RELEASE(TestAccessType.COMPARE_AND_EXCHANGE),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   151
        WEAK_COMPARE_AND_SET(TestAccessType.COMPARE_AND_SET),
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37343
diff changeset
   152
        WEAK_COMPARE_AND_SET_VOLATILE(TestAccessType.COMPARE_AND_SET),
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   153
        WEAK_COMPARE_AND_SET_ACQUIRE(TestAccessType.COMPARE_AND_SET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   154
        WEAK_COMPARE_AND_SET_RELEASE(TestAccessType.COMPARE_AND_SET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   155
        GET_AND_SET(TestAccessType.GET_AND_SET),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   156
        GET_AND_ADD(TestAccessType.GET_AND_ADD),
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   157
        ADD_AND_GET(TestAccessType.GET_AND_ADD),;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   158
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   159
        final TestAccessType at;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   160
        final boolean isPolyMorphicInReturnType;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   161
        final Class<?> returnType;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   162
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   163
        TestAccessMode(TestAccessType at) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   164
            this.at = at;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   165
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   166
            try {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   167
                VarHandle.AccessMode vh_am = toAccessMode();
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   168
                Method m = VarHandle.class.getMethod(vh_am.methodName(), Object[].class);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   169
                this.returnType = m.getReturnType();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   170
                isPolyMorphicInReturnType = returnType != Object.class;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   171
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   172
            catch (Exception e) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   173
                throw new Error(e);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   174
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   175
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   176
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   177
        boolean isOfType(TestAccessType at) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   178
            return this.at == at;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   179
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   180
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   181
        VarHandle.AccessMode toAccessMode() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   182
            return VarHandle.AccessMode.valueOf(name());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   183
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   184
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   185
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   186
    static List<TestAccessMode> testAccessModes() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   187
        return Stream.of(TestAccessMode.values()).collect(toList());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   188
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   189
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   190
    static List<TestAccessMode> testAccessModesOfType(TestAccessType... ats) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   191
        Stream<TestAccessMode> s = Stream.of(TestAccessMode.values());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   192
        for (TestAccessType at : ats) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   193
            s = s.filter(e -> e.isOfType(at));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   194
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   195
        return s.collect(toList());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   196
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   197
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   198
    static List<VarHandle.AccessMode> accessModes() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   199
        return Stream.of(VarHandle.AccessMode.values()).collect(toList());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   200
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   201
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   202
    static List<VarHandle.AccessMode> accessModesOfType(TestAccessType... ats) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   203
        Stream<TestAccessMode> s = Stream.of(TestAccessMode.values());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   204
        for (TestAccessType at : ats) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   205
            s = s.filter(e -> e.isOfType(at));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   206
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   207
        return s.map(TestAccessMode::toAccessMode).collect(toList());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   208
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   209
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   210
    static MethodHandle toMethodHandle(VarHandle vh, TestAccessMode tam, MethodType mt) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   211
        return vh.toMethodHandle(tam.toAccessMode());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   212
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   213
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   214
    static MethodHandle findVirtual(VarHandle vh, TestAccessMode tam, MethodType mt) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   215
        mt = vh.accessModeType(tam.toAccessMode());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   216
        MethodHandle mh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   217
        try {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   218
            mh = MethodHandles.publicLookup().
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   219
                    findVirtual(VarHandle.class,
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   220
                                tam.toAccessMode().methodName(),
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   221
                                mt);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   222
        } catch (Exception e) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   223
            throw new RuntimeException(e);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   224
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   225
        return bind(vh, tam, mh, mt);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   226
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   227
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   228
    static MethodHandle varHandleInvokerWithAccessModeType(VarHandle vh, TestAccessMode tam, MethodType mt) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   229
        mt = vh.accessModeType(tam.toAccessMode());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   230
        MethodHandle mh = MethodHandles.varHandleInvoker(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   231
                tam.toAccessMode(),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   232
                mt);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   233
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   234
        return bind(vh, tam, mh, mt);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   235
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   236
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   237
    static MethodHandle varHandleInvokerWithSymbolicTypeDescriptor(VarHandle vh, TestAccessMode tam, MethodType mt) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   238
        MethodHandle mh = MethodHandles.varHandleInvoker(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   239
                tam.toAccessMode(),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   240
                mt);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   241
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   242
        return bind(vh, tam, mh, mt);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   243
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   244
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   245
    static MethodHandle varHandleExactInvokerWithAccessModeType(VarHandle vh, TestAccessMode tam, MethodType mt) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   246
        mt = vh.accessModeType(tam.toAccessMode());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   247
        MethodHandle mh = MethodHandles.varHandleExactInvoker(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   248
                tam.toAccessMode(),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   249
                mt);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   250
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   251
        return bind(vh, tam, mh, mt);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   252
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   253
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   254
    private static MethodHandle bind(VarHandle vh, TestAccessMode testAccessMode, MethodHandle mh, MethodType emt) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   255
        assertEquals(mh.type(), emt.insertParameterTypes(0, VarHandle.class),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   256
                     "MethodHandle type differs from access mode type");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   257
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   258
        MethodHandleInfo info = MethodHandles.lookup().revealDirect(mh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   259
        assertEquals(info.getMethodType(), emt,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   260
                     "MethodHandleInfo method type differs from access mode type");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   261
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   262
        return mh.bindTo(vh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   263
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   264
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   265
    private interface TriFunction<T, U, V, R> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   266
        R apply(T t, U u, V v);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   267
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   268
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   269
    enum VarHandleToMethodHandle {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   270
        VAR_HANDLE_TO_METHOD_HANDLE(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   271
                "VarHandle.toMethodHandle",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   272
                VarHandleBaseTest::toMethodHandle),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   273
        METHOD_HANDLES_LOOKUP_FIND_VIRTUAL(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   274
                "Lookup.findVirtual",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   275
                VarHandleBaseTest::findVirtual),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   276
        METHOD_HANDLES_VAR_HANDLE_INVOKER_WITH_ACCESS_MODE_TYPE(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   277
                "MethodHandles.varHandleInvoker(accessModeType)",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   278
                VarHandleBaseTest::varHandleInvokerWithAccessModeType),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   279
        METHOD_HANDLES_VAR_HANDLE_INVOKER_WITH_SYMBOLIC_TYPE_DESCRIPTOR(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   280
                "MethodHandles.varHandleInvoker(symbolicTypeDescriptor)",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   281
                VarHandleBaseTest::varHandleInvokerWithSymbolicTypeDescriptor),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   282
        METHOD_HANDLES_VAR_HANDLE_EXACT_INVOKER_WITH_ACCESS_MODE_TYPE(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   283
                "MethodHandles.varHandleExactInvoker(accessModeType)",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   284
                VarHandleBaseTest::varHandleExactInvokerWithAccessModeType);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   285
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   286
        final String desc;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   287
        final TriFunction<VarHandle, TestAccessMode, MethodType, MethodHandle> f;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   288
        final boolean exact;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   289
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   290
        VarHandleToMethodHandle(String desc, TriFunction<VarHandle, TestAccessMode, MethodType, MethodHandle> f) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   291
            this(desc, f, false);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   292
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   293
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   294
        VarHandleToMethodHandle(String desc, TriFunction<VarHandle, TestAccessMode, MethodType, MethodHandle> f,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   295
                                boolean exact) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   296
            this.desc = desc;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   297
            this.f = f;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   298
            this.exact = exact;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   299
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   300
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   301
        MethodHandle apply(VarHandle vh, TestAccessMode am, MethodType mt) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   302
            return f.apply(vh, am, mt);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   303
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   304
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   305
        @Override
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   306
        public String toString() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   307
            return desc;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   308
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   309
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   310
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   311
    static class Handles {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   312
        static class AccessModeAndType {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   313
            final TestAccessMode tam;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   314
            final MethodType t;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   315
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   316
            public AccessModeAndType(TestAccessMode tam, MethodType t) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   317
                this.tam = tam;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   318
                this.t = t;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   319
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   320
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   321
            @Override
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   322
            public boolean equals(Object o) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   323
                if (this == o) return true;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   324
                if (o == null || getClass() != o.getClass()) return false;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   325
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   326
                AccessModeAndType x = (AccessModeAndType) o;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   327
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   328
                if (tam != x.tam) return false;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   329
                if (t != null ? !t.equals(x.t) : x.t != null) return false;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   330
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   331
                return true;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   332
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   333
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   334
            @Override
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   335
            public int hashCode() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   336
                int result = tam != null ? tam.hashCode() : 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   337
                result = 31 * result + (t != null ? t.hashCode() : 0);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   338
                return result;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   339
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   340
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   341
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   342
        final VarHandle vh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   343
        final VarHandleToMethodHandle f;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   344
        final EnumMap<TestAccessMode, MethodType> amToType;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   345
        final Map<AccessModeAndType, MethodHandle> amToHandle;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   346
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   347
        Handles(VarHandle vh, VarHandleToMethodHandle f) throws Exception {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   348
            this.vh = vh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   349
            this.f = f;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   350
            this.amToHandle = new HashMap<>();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   351
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   352
            amToType = new EnumMap<>(TestAccessMode.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   353
            for (TestAccessMode am : testAccessModes()) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   354
                amToType.put(am, vh.accessModeType(am.toAccessMode()));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   355
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   356
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   357
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   358
        MethodHandle get(TestAccessMode am) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   359
            return get(am, amToType.get(am));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   360
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   361
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   362
        MethodHandle get(TestAccessMode am, MethodType mt) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   363
            AccessModeAndType amt = new AccessModeAndType(am, mt);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   364
            return amToHandle.computeIfAbsent(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   365
                    amt, k -> f.apply(vh, am, mt));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   366
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   367
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   368
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   369
    interface AccessTestAction<T> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   370
        void action(T t) throws Throwable;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   371
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   372
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   373
    static abstract class AccessTestCase<T> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   374
        final String desc;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   375
        final AccessTestAction<T> ata;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   376
        final boolean loop;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   377
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   378
        AccessTestCase(String desc, AccessTestAction<T> ata, boolean loop) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   379
            this.desc = desc;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   380
            this.ata = ata;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   381
            this.loop = loop;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   382
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   383
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   384
        boolean requiresLoop() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   385
            return loop;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   386
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   387
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   388
        abstract T get() throws Exception;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   389
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   390
        void testAccess(T t) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   391
            ata.action(t);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   392
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   393
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   394
        @Override
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   395
        public String toString() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   396
            return desc;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   397
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   398
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   399
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   400
    static class VarHandleAccessTestCase extends AccessTestCase<VarHandle> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   401
        final VarHandle vh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   402
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   403
        VarHandleAccessTestCase(String desc, VarHandle vh, AccessTestAction<VarHandle> ata) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   404
            this(desc, vh, ata, true);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   405
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   406
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   407
        VarHandleAccessTestCase(String desc, VarHandle vh, AccessTestAction<VarHandle> ata, boolean loop) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   408
            super("VarHandle -> " + desc, ata, loop);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   409
            this.vh = vh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   410
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   411
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   412
        @Override
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   413
        VarHandle get() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   414
            return vh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   415
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   416
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   417
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   418
    static class MethodHandleAccessTestCase extends AccessTestCase<Handles> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   419
        final VarHandle vh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   420
        final VarHandleToMethodHandle f;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   421
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   422
        MethodHandleAccessTestCase(String desc, VarHandle vh, VarHandleToMethodHandle f, AccessTestAction<Handles> ata) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   423
            this(desc, vh, f, ata, true);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   424
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   425
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   426
        MethodHandleAccessTestCase(String desc, VarHandle vh, VarHandleToMethodHandle f, AccessTestAction<Handles> ata, boolean loop) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   427
            super("VarHandle -> " + f.toString() + " -> " + desc, ata, loop);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   428
            this.vh = vh;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   429
            this.f = f;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   430
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   431
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   432
        @Override
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   433
        Handles get() throws Exception {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   434
            return new Handles(vh, f);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   435
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   436
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   437
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   438
    static void testTypes(VarHandle vh) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   439
        List<Class<?>> pts = vh.coordinateTypes();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   440
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   441
        for (TestAccessMode accessMode : testAccessModes()) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   442
            MethodType amt = vh.accessModeType(accessMode.toAccessMode());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   443
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   444
            assertEquals(amt.parameterList().subList(0, pts.size()), pts);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   445
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   446
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   447
        for (TestAccessMode testAccessMode : testAccessModesOfType(TestAccessType.GET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   448
            MethodType mt = vh.accessModeType(testAccessMode.toAccessMode());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   449
            assertEquals(mt.returnType(), vh.varType());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   450
            assertEquals(mt.parameterList(), pts);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   451
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   452
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   453
        for (TestAccessMode testAccessMode : testAccessModesOfType(TestAccessType.SET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   454
            MethodType mt = vh.accessModeType(testAccessMode.toAccessMode());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   455
            assertEquals(mt.returnType(), void.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   456
            assertEquals(mt.parameterType(mt.parameterCount() - 1), vh.varType());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   457
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   458
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   459
        for (TestAccessMode testAccessMode : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   460
            MethodType mt = vh.accessModeType(testAccessMode.toAccessMode());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   461
            assertEquals(mt.returnType(), boolean.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   462
            assertEquals(mt.parameterType(mt.parameterCount() - 1), vh.varType());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   463
            assertEquals(mt.parameterType(mt.parameterCount() - 2), vh.varType());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   464
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   465
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   466
        for (TestAccessMode testAccessMode : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   467
            MethodType mt = vh.accessModeType(testAccessMode.toAccessMode());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   468
            assertEquals(mt.returnType(), vh.varType());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   469
            assertEquals(mt.parameterType(mt.parameterCount() - 1), vh.varType());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   470
            assertEquals(mt.parameterType(mt.parameterCount() - 2), vh.varType());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   471
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   472
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   473
        for (TestAccessMode testAccessMode : testAccessModesOfType(TestAccessType.GET_AND_SET, TestAccessType.GET_AND_ADD)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   474
            MethodType mt = vh.accessModeType(testAccessMode.toAccessMode());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   475
            assertEquals(mt.returnType(), vh.varType());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   476
            assertEquals(mt.parameterType(mt.parameterCount() - 1), vh.varType());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   477
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   478
    }
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   479
}