lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
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
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;
import java.lang.invoke.WrongMethodTypeException;
 
public final class Main {
    static class TestSetupError extends Error {
        TestSetupError(String message, Throwable cause) {
            super(message, cause);
        }
    }
 
    private static void failAssertion(String message) {
        StringBuilder sb = new StringBuilder();
        sb.append("Test failure: ");
        sb.append(message);
        throw new AssertionError(sb.toString());
    }
 
    private static void assertUnreachable() throws Throwable {
        failAssertion("Unreachable");
    }
 
    private static void failAssertEquals(Object expected, Object actual) {
        StringBuilder sb = new StringBuilder();
        sb.append(expected);
        sb.append(" != ");
        sb.append(actual);
        failAssertion(sb.toString());
    }
 
    private static void assertEquals(int expected, int actual) {
        if (expected != actual) {
            failAssertEquals(expected, actual);
        }
    }
 
    private static void assertEquals(float expected, float actual) {
        if (expected != actual) {
            failAssertEquals(expected, actual);
        }
    }
 
    private static void assertEquals(double expected, double actual) {
        if (expected != actual) {
            failAssertEquals(expected, actual);
        }
    }
 
    static class FieldVarHandleExactInvokerTest {
        private static final VarHandle fieldVarHandle;
        int field;
 
        static {
            try {
                fieldVarHandle =
                        MethodHandles.lookup()
                                .findVarHandle(
                                        FieldVarHandleExactInvokerTest.class, "field", int.class);
            } catch (Exception e) {
                throw new TestSetupError("Failed to lookup of field", e);
            }
        }
 
        void run() throws Throwable {
            System.out.println("fieldVarHandleExactInvokerTest");
 
            MethodHandle invokerMethodHandle =
                    MethodHandles.varHandleExactInvoker(
                            VarHandle.AccessMode.GET_AND_SET,
                            MethodType.methodType(
                                    int.class, FieldVarHandleExactInvokerTest.class, int.class));
 
            field = 3;
            assertEquals(3, (int) invokerMethodHandle.invokeExact(fieldVarHandle, this, 4));
            assertEquals(4, field);
 
            //
            // Check invocations with MethodHandle.invokeExact()
            //
            try {
                // Check for unboxing
                int i =
                        (int)
                                invokerMethodHandle.invokeExact(
                                        fieldVarHandle, this, Integer.valueOf(3));
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(4, field);
            }
            try {
                // Check for widening conversion
                int i = (int) invokerMethodHandle.invokeExact(fieldVarHandle, this, (short) 3);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(4, field);
            }
            try {
                // Check for acceptance of void return type
                invokerMethodHandle.invokeExact(fieldVarHandle, this, 77);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(4, field);
            }
            try {
                // Check for wider return type
                long l = (long) invokerMethodHandle.invokeExact(fieldVarHandle, this, 77);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(4, field);
            }
            try {
                // Check null VarHandle instance fails
                VarHandle vhNull = null;
                int i = (int) invokerMethodHandle.invokeExact(vhNull, this, 777);
                assertUnreachable();
            } catch (NullPointerException expected) {
                assertEquals(4, field);
            }
 
            //
            // Check invocations with MethodHandle.invoke()
            //
 
            // Check for unboxing
            int i = (int) invokerMethodHandle.invoke(fieldVarHandle, this, Integer.valueOf(3));
            assertEquals(3, field);
 
            // Check for unboxing
            i = (int) invokerMethodHandle.invoke(fieldVarHandle, this, Short.valueOf((short) 4));
            assertEquals(4, field);
 
            // Check for widening conversion
            i = (int) invokerMethodHandle.invoke(fieldVarHandle, this, (short) 23);
            assertEquals(23, field);
 
            // Check for acceptance of void return type
            invokerMethodHandle.invoke(fieldVarHandle, this, 77);
            assertEquals(77, field);
 
            // Check for wider return type
            long l = (long) invokerMethodHandle.invoke(fieldVarHandle, this, 88);
            assertEquals(88, field);
 
            try {
                // Check null VarHandle instance fails
                VarHandle vhNull = null;
                i = (int) invokerMethodHandle.invoke(vhNull, this, 888);
                assertUnreachable();
            } catch (NullPointerException expected) {
                assertEquals(88, field);
            }
        }
    }
 
    static class FieldVarHandleInvokerTest {
        private static final VarHandle fieldVarHandle;
        int field;
 
        static {
            try {
                fieldVarHandle =
                        MethodHandles.lookup()
                                .findVarHandle(FieldVarHandleInvokerTest.class, "field", int.class);
            } catch (Exception e) {
                throw new TestSetupError("Failed to lookup of field", e);
            }
        }
 
        void run() throws Throwable {
            System.out.println("fieldVarHandleInvokerTest");
            MethodHandle invokerMethodHandle =
                    MethodHandles.varHandleInvoker(
                            VarHandle.AccessMode.GET_AND_SET,
                            MethodType.methodType(
                                    int.class, FieldVarHandleInvokerTest.class, int.class));
 
            field = 3;
            int oldField = (int) invokerMethodHandle.invoke(fieldVarHandle, this, 4);
            assertEquals(3, oldField);
            assertEquals(4, field);
 
            //
            // Check invocations with MethodHandle.invoke()
            //
 
            // Check for unboxing
            int i = (int) invokerMethodHandle.invoke(fieldVarHandle, this, Integer.valueOf(3));
            assertEquals(3, field);
 
            // Check for widening conversion
            i = (int) invokerMethodHandle.invoke(fieldVarHandle, this, (short) 33);
            assertEquals(33, field);
 
            // Check for widening conversion
            i = (int) invokerMethodHandle.invoke(fieldVarHandle, this, Byte.valueOf((byte) 34));
            assertEquals(34, field);
 
            // Check for acceptance of void return type
            invokerMethodHandle.invoke(fieldVarHandle, this, 77);
            assertEquals(77, field);
 
            // Check for wider return type
            long l = (long) invokerMethodHandle.invoke(fieldVarHandle, this, 88);
            assertEquals(88, field);
            try {
                // Check narrowing conversion fails
                i = (int) invokerMethodHandle.invoke(fieldVarHandle, this, 3.0);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
            }
            try {
                // Check reference type fails
                i = (int) invokerMethodHandle.invoke(fieldVarHandle, this, "Bad");
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
            }
            try {
                // Check null VarHandle instance fails
                VarHandle vhNull = null;
                i = (int) invokerMethodHandle.invoke(vhNull, this, 888);
                assertUnreachable();
            } catch (NullPointerException expected) {
                assertEquals(88, field);
            }
 
            //
            // Check invocations with MethodHandle.invokeExact()
            //
            field = -1;
            try {
                // Check for unboxing
                i = (int) invokerMethodHandle.invokeExact(fieldVarHandle, this, Integer.valueOf(3));
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(-1, field);
            }
            try {
                // Check for widening conversion
                i = (int) invokerMethodHandle.invokeExact(fieldVarHandle, this, (short) 33);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(-1, field);
            }
            try {
                // Check for acceptance of void return type
                invokerMethodHandle.invokeExact(fieldVarHandle, this, 77);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(-1, field);
            }
            try {
                // Check for wider return type
                l = (long) invokerMethodHandle.invokeExact(fieldVarHandle, this, 78);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(-1, field);
            }
            try {
                // Check narrowing conversion fails
                i = (int) invokerMethodHandle.invokeExact(fieldVarHandle, this, 3.0);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
            }
            try {
                // Check reference type fails
                i = (int) invokerMethodHandle.invokeExact(fieldVarHandle, this, "Bad");
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
            }
            try {
                // Check null VarHandle instance fails
                VarHandle vhNull = null;
                i = (int) invokerMethodHandle.invokeExact(vhNull, this, 888);
                assertUnreachable();
            } catch (NullPointerException expected) {
                assertEquals(-1, field);
            }
        }
    }
 
    static class DivergenceExactInvokerTest {
        private static final VarHandle floatsArrayVarHandle;
 
        static {
            try {
                floatsArrayVarHandle = MethodHandles.arrayElementVarHandle(float[].class);
            } catch (Exception e) {
                throw new TestSetupError("Failed to create VarHandle", e);
            }
        }
 
        void run() throws Throwable {
            System.out.println("DivergenceExactInvokerTest");
            float[] floatsArray = new float[4];
            // Exact invoker of an accessor having the form:
            //  float accessor(float[] values, int index, Float current, float replacement)
            MethodHandle exactInvoker =
                    MethodHandles.varHandleExactInvoker(
                            VarHandle.AccessMode.COMPARE_AND_EXCHANGE,
                            MethodType.methodType(
                                    float.class,
                                    float[].class,
                                    int.class,
                                    Float.class,
                                    float.class));
            floatsArray[2] = Float.valueOf(4.0f);
            // Callsite that is an exact match with exactInvoker.type().
            try {
                // exactInvoker.type() is not compatible with floatsArrayVarHandle accessor.
                float old =
                        (float)
                                exactInvoker.invoke(
                                        floatsArrayVarHandle,
                                        floatsArray,
                                        2,
                                        Float.valueOf(4.0f),
                                        8.0f);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(4.0f, floatsArray[2]);
            }
 
            // Callsites that are exact matches with exactInvoker.type()
            try {
                // Mismatch between exactInvoker.type() and VarHandle type (Float != float)
                float old =
                        (float)
                                exactInvoker.invoke(
                                        floatsArrayVarHandle, floatsArray, 2, 8.0f, 16.0f);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(4.0f, floatsArray[2]);
            }
            try {
                // short not convertible to Float
                float old =
                        (float)
                                exactInvoker.invoke(
                                        floatsArrayVarHandle, floatsArray, 2, (short) 4, 13.0f);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(4.0f, floatsArray[2]);
            }
            try {
                // int not convertible to Float
                float old =
                        (float) exactInvoker.invoke(floatsArrayVarHandle, floatsArray, 2, 8, -8.0f);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(4.0f, floatsArray[2]);
            }
        }
    }
 
    static class DivergenceInvokerTest {
        private static final VarHandle floatsArrayVarHandle;
 
        static {
            try {
                floatsArrayVarHandle = MethodHandles.arrayElementVarHandle(float[].class);
            } catch (Exception e) {
                throw new TestSetupError("Failed to create VarHandle", e);
            }
        }
 
        void run() throws Throwable {
            System.out.println("DivergenceInvokerTest");
            float[] floatsArray = new float[4];
            // Invoker of an accessor having the form:
            //  float accessor(float[] values, int index, Float current, float replacement)
            MethodHandle invoker =
                    MethodHandles.varHandleInvoker(
                            VarHandle.AccessMode.COMPARE_AND_EXCHANGE,
                            MethodType.methodType(
                                    float.class,
                                    float[].class,
                                    int.class,
                                    Float.class,
                                    float.class));
            floatsArray[2] = Float.valueOf(4.0f);
            // Callsite that is an exact match with invoker.type()
            float old =
                    (float)
                            invoker.invoke(
                                    floatsArrayVarHandle,
                                    floatsArray,
                                    2,
                                    Float.valueOf(4.0f),
                                    8.0f);
            assertEquals(4.0f, old);
            assertEquals(8.0f, floatsArray[2]);
 
            // Callsite that is convertible match to invoker.type()
            old = (float) invoker.invoke(floatsArrayVarHandle, floatsArray, 2, 8.0f, 16.0f);
            assertEquals(8.0f, old);
            assertEquals(16.0f, floatsArray[2]);
 
            // Callsites that are not convertible to invoker.type().
            try {
                // short is not convertible to Float
                old = (float) invoker.invoke(floatsArrayVarHandle, floatsArray, 2, (short) 4, 8.0f);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(16.0f, floatsArray[2]);
            }
            try {
                // int is not convertible to Float
                old = (float) invoker.invoke(floatsArrayVarHandle, floatsArray, 2, 8, -8.0f);
                assertUnreachable();
            } catch (WrongMethodTypeException expected) {
                assertEquals(16.0f, floatsArray[2]);
            }
        }
    }
 
    public static void main(String[] args) throws Throwable {
        new FieldVarHandleExactInvokerTest().run();
        new FieldVarHandleInvokerTest().run();
        new DivergenceExactInvokerTest().run();
        new DivergenceInvokerTest().run();
    }
}