huangcm
2025-07-01 676035278781360996553c427a12bf358249ebf7
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
/*
 * Copyright (C) 2016 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.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.lang.invoke.WrongMethodTypeException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class Main {
  public static void assertTrue(boolean value) {
    if (!value) {
      throw new AssertionError("assertTrue value: " + value);
    }
  }
 
  public static void assertFalse(boolean value) {
    if (value) {
      throw new AssertionError("assertTrue value: " + value);
    }
  }
 
  public static void assertEquals(int i1, int i2) {
    if (i1 == i2) { return; }
    throw new AssertionError("assertEquals i1: " + i1 + ", i2: " + i2);
  }
 
  public static void assertEquals(long i1, long i2) {
    if (i1 == i2) { return; }
    throw new AssertionError("assertEquals l1: " + i1 + ", l2: " + i2);
  }
 
  public static void assertEquals(Object o, Object p) {
    if (o == p) { return; }
    if (o != null && p != null && o.equals(p)) { return; }
    throw new AssertionError("assertEquals: o1: " + o + ", o2: " + p);
  }
 
  public static void assertEquals(String s1, String s2) {
    if (s1 == s2) {
      return;
    }
 
    if (s1 != null && s2 != null && s1.equals(s2)) {
      return;
    }
 
    throw new AssertionError("assertEquals s1: " + s1 + ", s2: " + s2);
  }
 
  public static void fail() {
    System.out.println("fail");
    Thread.dumpStack();
  }
 
  public static void fail(String message) {
    System.out.println("fail: " + message);
    Thread.dumpStack();
  }
 
  public static int Min2Print2(int a, int b) {
    int[] values = new int[] { a, b };
    System.out.println("Running Main.Min2Print2(" + Arrays.toString(values) + ")");
    return a > b ? a : b;
  }
 
  public static int Min2Print3(int a, int b, int c) {
    int[] values = new int[] { a, b, c };
    System.out.println("Running Main.Min2Print3(" + Arrays.toString(values) + ")");
    return a > b ? a : b;
  }
 
  public static int Min2Print6(int a, int b, int c, int d, int e, int f) {
    int[] values = new int[] { a, b, c, d, e, f };
    System.out.println("Running Main.Min2Print6(" + Arrays.toString(values) + ")");
    return a > b ? a : b;
  }
 
  public static int Min2Print26(int a, int b, int c, int d,
                                int e, int f, int g, int h,
                                int i, int j, int k, int l,
                                int m, int n, int o, int p,
                                int q, int r, int s, int t,
                                int u, int v, int w, int x,
                                int y, int z) {
    int[] values = new int[] { a, b, c, d, e, f, g, h, i, j, k, l, m,
                               n, o, p, q, r, s, t, u, v, w, x, y, z };
    System.out.println("Running Main.Min2Print26(" + Arrays.toString(values) + ")");
    return a > b ? a : b;
  }
 
  public static void $opt$BasicTest() throws Throwable {
    MethodHandle mh;
    mh = MethodHandles.lookup().findStatic(
        Main.class, "Min2Print2", MethodType.methodType(int.class, int.class, int.class));
    assertEquals((int) mh.invokeExact(33, -4), 33);
    assertEquals((int) mh.invokeExact(-4, 33), 33);
 
    mh = MethodHandles.lookup().findStatic(
        Main.class, "Min2Print3",
        MethodType.methodType(int.class, int.class, int.class, int.class));
    assertEquals((int) mh.invokeExact(33, -4, 17), 33);
    assertEquals((int) mh.invokeExact(-4, 17, 33), 17);
    assertEquals((int) mh.invokeExact(17, 33, -4), 33);
 
    mh = MethodHandles.lookup().findStatic(
        Main.class, "Min2Print6",
        MethodType.methodType(
            int.class, int.class, int.class, int.class, int.class, int.class, int.class));
    assertEquals((int) mh.invokeExact(33, -4, 77, 88, 99, 111), 33);
    try {
        // Too few arguments
        assertEquals((int) mh.invokeExact(33, -4, 77, 88), 33);
        fail("No WMTE for too few arguments");
    } catch (WrongMethodTypeException e) {}
    try {
        // Too many arguments
        assertEquals((int) mh.invokeExact(33, -4, 77, 88, 89, 90, 91), 33);
        fail("No WMTE for too many arguments");
    } catch (WrongMethodTypeException e) {}
    assertEquals((int) mh.invokeExact(-4, 77, 88, 99, 111, 33), 77);
    assertEquals((int) mh.invokeExact(77, 88, 99, 111, 33, -4), 88);
    assertEquals((int) mh.invokeExact(88, 99, 111, 33, -4, 77), 99);
    assertEquals((int) mh.invokeExact(99, 111, 33, -4, 77, 88), 111);
    assertEquals((int) mh.invokeExact(111, 33, -4, 77, 88, 99), 111);
 
    // A preposterous number of arguments.
    mh = MethodHandles.lookup().findStatic(
        Main.class, "Min2Print26",
        MethodType.methodType(
            // Return-type
            int.class,
            // Arguments
            int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class,
            int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class,
            int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class,
            int.class, int.class));
    assertEquals(1, (int) mh.invokeExact(0, 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));
    assertEquals(25, (int) mh.invokeExact(25, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                                         13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24));
    assertEquals(25, (int) mh.invokeExact(24, 25, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                                         13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23));
 
    try {
        // Wrong argument type
        mh.invokeExact("a");
        fail("No WMTE for wrong arguments");
    } catch (WrongMethodTypeException wmte) {}
 
    try {
        // Invoke on null handle.
        MethodHandle mh0 = null;
        mh0.invokeExact("bad");
        fail("No NPE for you");
    } catch (NullPointerException npe) {}
 
    System.out.println("BasicTest done.");
  }
 
  private static boolean And(boolean lhs, boolean rhs) {
    return lhs & rhs;
  }
 
  private static boolean Xor(boolean lhs, boolean rhs) {
    return lhs ^ rhs;
  }
 
  private static String Multiply(String value, int n) {
    String result = "";
    for (int i = 0; i < n; ++i) {
      result = value + result;
    }
    return result;
  }
 
  private static byte Multiply(byte value, byte n) {
    return (byte)(value * n);
  }
 
  private static short Multiply(short value, short n) {
    return (short)(value * n);
  }
 
  private static int Multiply(int value, int n) {
    return value * n;
  }
 
  private static long Multiply(long value, long n) {
    return value * n;
  }
 
  private static float Multiply(float value, float n) {
    return value * n;
  }
 
  private static double Multiply(double value, double n) {
    return value * n;
  }
 
  private static char Next(char c) {
    return (char)(c + 1);
  }
 
  public static void $opt$ReturnBooleanTest() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh =
            lookup.findStatic(Main.class, "And",
                              MethodType.methodType(boolean.class, boolean.class, boolean.class));
    assertEquals(true, (boolean) mh.invokeExact(true, true));
    assertEquals(false, (boolean) mh.invokeExact(true, false));
    assertEquals(false, (boolean) mh.invokeExact(false, true));
    assertEquals(false, (boolean) mh.invokeExact(false, false));
    assertEquals(true, (boolean) mh.invoke(true, true));
    assertEquals(false, (boolean) mh.invoke(true, false));
    assertEquals(false, (boolean) mh.invoke(false, true));
    assertEquals(false, (boolean) mh.invoke(false, false));
 
    mh = lookup.findStatic(Main.class, "Xor",
                           MethodType.methodType(boolean.class, boolean.class, boolean.class));
    assertEquals(false, (boolean) mh.invokeExact(true, true));
    assertEquals(true, (boolean) mh.invokeExact(true, false));
    assertEquals(true, (boolean) mh.invokeExact(false, true));
    assertEquals(false, (boolean) mh.invokeExact(false, false));
    assertEquals(false, (boolean) mh.invoke(true, true));
    assertEquals(true, (boolean) mh.invoke(true, false));
    assertEquals(true, (boolean) mh.invoke(false, true));
    assertEquals(false, (boolean) mh.invoke(false, false));
 
    System.out.println("$opt$ReturnBooleanTest done.");
  }
 
  public static void $opt$ReturnCharTest() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh = lookup.findStatic(Main.class, "Next",
                           MethodType.methodType(char.class, char.class));
    assertEquals('B', (char) mh.invokeExact('A'));
    assertEquals((char) -55, (char) mh.invokeExact((char) -56));
    System.out.println("$opt$ReturnCharTest done.");
  }
 
  public static void $opt$ReturnByteTest() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh = lookup.findStatic(Main.class, "Multiply",
                                         MethodType.methodType(byte.class, byte.class, byte.class));
    assertEquals((byte) 30, (byte) mh.invokeExact((byte) 10, (byte) 3));
    assertEquals((byte) -90, (byte) mh.invoke((byte) -10, (byte) 9));
    System.out.println("$opt$ReturnByteTest done.");
  }
 
  public static void $opt$ReturnShortTest() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh = lookup.findStatic(Main.class, "Multiply",
                           MethodType.methodType(short.class, short.class, short.class));
    assertEquals((short) 3000, (short) mh.invokeExact((short) 1000, (short) 3));
    assertEquals((short) -3000, (short) mh.invoke((short) -1000, (short) 3));
    System.out.println("$opt$ReturnShortTest done.");
  }
 
  public static void $opt$ReturnIntTest() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh = lookup.findStatic(Main.class, "Multiply",
                           MethodType.methodType(int.class, int.class, int.class));
    assertEquals(3_000_000, (int) mh.invokeExact(1_000_000, 3));
    assertEquals(-3_000_000, (int) mh.invoke(-1_000, 3_000));
    System.out.println("$opt$ReturnIntTest done.");
  }
 
  public static void $opt$ReturnLongTest() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh = lookup.findStatic(Main.class, "Multiply",
                           MethodType.methodType(long.class, long.class, long.class));
    assertEquals(4_294_967_295_000L, (long) mh.invokeExact(1000L, 4_294_967_295L));
    assertEquals(-4_294_967_295_000L, (long) mh.invoke(-1000L, 4_294_967_295L));
    System.out.println("$opt$ReturnLongTest done.");
  }
 
  public static void $opt$ReturnFloatTest() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh = lookup.findStatic(Main.class, "Multiply",
                           MethodType.methodType(float.class, float.class, float.class));
    assertEquals(3.0F, (float) mh.invokeExact(1000.0F, 3e-3F));
    assertEquals(-3.0F, (float) mh.invoke(-1000.0F, 3e-3F));
    System.out.println("$opt$ReturnFloatTest done.");
  }
 
  public static void $opt$ReturnDoubleTest() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh = lookup.findStatic(Main.class, "Multiply",
                           MethodType.methodType(double.class, double.class, double.class));
    assertEquals(3033000.0, (double) mh.invokeExact(1000.0, 3.033e3));
    assertEquals(-3033000.0, (double) mh.invoke(-1000.0, 3.033e3));
    System.out.println("$opt$ReturnDoubleTest done.");
  }
 
  public static void $opt$ReturnStringTest() throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh = lookup.findStatic(Main.class, "Multiply",
                           MethodType.methodType(String.class, String.class, int.class));
    assertEquals("100010001000", (String) mh.invokeExact("1000", 3));
    assertEquals("100010001000", (String) mh.invoke("1000", 3));
    System.out.println("$opt$ReturnStringTest done.");
  }
 
  public static void ReturnValuesTest() throws Throwable {
    $opt$ReturnBooleanTest();
    $opt$ReturnCharTest();
    $opt$ReturnByteTest();
    $opt$ReturnShortTest();
    $opt$ReturnIntTest();
    $opt$ReturnLongTest();
    $opt$ReturnFloatTest();
    $opt$ReturnDoubleTest();
    $opt$ReturnStringTest();
    System.out.println("ReturnValuesTest done.");
  }
 
  static class ValueHolder {
    public boolean m_z;
    public static boolean s_z;
  }
 
  public static void $opt$AccessorsTest() throws Throwable {
    ValueHolder valueHolder = new ValueHolder();
    MethodHandles.Lookup lookup = MethodHandles.lookup();
 
    MethodHandle setMember = lookup.findSetter(ValueHolder.class, "m_z", boolean.class);
    MethodHandle getMember = lookup.findGetter(ValueHolder.class, "m_z", boolean.class);
    MethodHandle setStatic = lookup.findStaticSetter(ValueHolder.class, "s_z", boolean.class);
    MethodHandle getStatic = lookup.findStaticGetter(ValueHolder.class, "s_z", boolean.class);
 
    boolean [] values = { false, true, false, true, false };
    for (boolean value : values) {
      assertEquals((boolean) getStatic.invoke(), ValueHolder.s_z);
      setStatic.invoke(value);
      ValueHolder.s_z = value;
      assertEquals(ValueHolder.s_z, value);
      assertEquals((boolean) getStatic.invoke(), value);
 
      assertEquals((boolean) getMember.invoke(valueHolder), valueHolder.m_z);
      setMember.invoke(valueHolder, value);
      valueHolder.m_z = value;
      assertEquals(valueHolder.m_z, value);
      assertEquals((boolean) getMember.invoke(valueHolder), value);
    }
  }
 
  public static void main(String[] args) throws Throwable {
    $opt$BasicTest();
    ReturnValuesTest();
    $opt$AccessorsTest();
  }
}