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
|
/*
* Copyright (C) 2024 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 static java.lang.invoke.MethodType.methodType;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.WrongMethodTypeException;
import java.util.Arrays;
import java.util.Optional;
public class Main {
public static void main(String[] args) throws Throwable {
$noinline$testNoArgsCalls();
$noinline$testMethodHandleFromOtherDex();
Multi.$noinline$testMHFromMain(OPTIONAL_GET);
$noinline$testWithArgs();
}
private static void $noinline$testMethodHandleFromOtherDex() throws Throwable {
MethodHandle mh = Multi.$noinline$getMethodHandle();
Optional<String> nonEmpty = Optional.<String>of("hello");
Object returnedObject = mh.invokeExact(nonEmpty);
System.out.println(
"Multi.$noinline$getMethodHandle().invokeExact(nonEmpty)=" + returnedObject);
try {
mh.invokeExact(nonEmpty);
unreachable("mh.type() is (Optional)Object, but callsite is (Optional)V");
} catch (WrongMethodTypeException expected) {}
}
private static void $noinline$testNoArgsCalls() throws Throwable {
VOID_METHOD.invokeExact(new A());
int returnedInt = (int) RETURN_INT.invokeExact(new A());
System.out.println("A.returnInt()=" + returnedInt);
double returnedDouble = (double) RETURN_DOUBLE.invokeExact(new A());
System.out.println("A.returnDouble()=" + returnedDouble);
try {
INTERFACE_DEFAULT_METHOD.invokeExact(new A());
unreachable("MethodHandle's type is (Main$I)V, but callsite is (Main$A)V");
} catch (WrongMethodTypeException ignored) {}
INTERFACE_DEFAULT_METHOD.invokeExact((I) new A());
OVERWRITTEN_INTERFACE_DEFAULT_METHOD.invokeExact((I) new A());
System.out.println((String) PRIVATE_INTERFACE_METHOD.invokeExact((I) new A()));
int privateIntA = (int) A_PRIVATE_RETURN_INT.invokeExact(new A());
System.out.println("A.privateReturnInt()=" + privateIntA);
int privateIntB = (int) B_PRIVATE_RETURN_INT.invokeExact(new B());
System.out.println("B.privateReturnInt()=" + privateIntB);
privateIntB = (int) B_PRIVATE_RETURN_INT.invokeExact((B) new A());
System.out.println("((B) new A()).privateReturnInt()=" + privateIntB);
try {
EXCEPTION_THROWING_METHOD.invokeExact(new A());
unreachable("Target method always throws");
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
try {
RETURN_INT.invokeExact(new A());
unreachable("MethodHandle's type is (Main$A)I, but callsite type is (Main$A)V");
} catch (WrongMethodTypeException ignored) {}
String returnedString = (String) STATIC_METHOD.invokeExact(new A());
System.out.println("A.staticMethod()=" + returnedString);
}
private static void $noinline$testWithArgs() throws Throwable {
int sum = (int) SUM_I.invokeExact(new Sums(), 1);
System.out.println("Sums.sum(1)=" + sum);
sum = (int) SUM_2I.invokeExact(new Sums(), 1, 2);
System.out.println("Sums.sum(1, 2)=" + sum);
sum = (int) SUM_3I.invokeExact(new Sums(), 1, 2, 3);
System.out.println("Sums.sum(1, 2, 3)=" + sum);
sum = (int) SUM_4I.invokeExact(new Sums(), 1, 2, 3, 4);
System.out.println("Sums.sum(1, 2, 3, 4)=" + sum);
sum = (int) SUM_5I.invokeExact(new Sums(), 1, 2, 3, 4, 5);
System.out.println("Sums.sum(1, 2, 3, 4, 5)=" + sum);
sum = (int) SUM_6I.invokeExact(new Sums(), 1, 2, 3, 4, 5, 6);
System.out.println("Sums.sum(1, 2, 3, 4, 5, 6)=" + sum);
sum = (int) SUM_7I.invokeExact(new Sums(), 1, 2, 3, 4, 5, 6, 7);
System.out.println("Sums.sum(1, 2, 3, 4, 5, 6, 7)=" + sum);
sum = (int) SUM_8I.invokeExact(new Sums(), 1, 2, 3, 4, 5, 6, 7, 8);
System.out.println("Sums.sum(1, 2, 3, 4, 5, 6, 7, 8)=" + sum);
sum = (int) SUM_9I.invokeExact(new Sums(), 1, 2, 3, 4, 5, 6, 7, 8, 9);
System.out.println("Sums.sum(1, 2, 3, 4, 5, 6, 7, 8, 9)=" + sum);
sum = (int) SUM_10I.invokeExact(new Sums(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println("Sums.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)=" + sum);
long lsum = (long) SUM_IJ.invokeExact(new Sums(), 1, 2L);
System.out.println("Sums.sum(1, 2L)=" + lsum);
lsum = (long) SUM_2IJ.invokeExact(new Sums(), 1, 2L, 3, 4L);
System.out.println("Sums.sum(1, 2L, 3, 4L)=" + lsum);
lsum = (long) SUM_3IJ.invokeExact(new Sums(), 1, 2L, 3, 4L, 5, 6L);
System.out.println("Sums.sum(1, 2L, 3, 4L, 5, 6L)=" + lsum);
lsum = (long) SUM_4IJ.invokeExact(new Sums(), 1, 2L, 3, 4L, 5, 6L, 7, 8L);
System.out.println("Sums.sum(1, 2L, 3, 4L, 5, 6L, 7, 8L)=" + lsum);
lsum = (long) SUM_5IJ.invokeExact(new Sums(), 1, 2L, 3, 4L, 5, 6L, 7, 8L, 9, 10L);
System.out.println("Sums.sum(1, 2L, 3, 4L, 5, 6L, 7, 8L, 9, 10L)=" + lsum);
}
private static void unreachable(String msg) {
throw new AssertionError("Unexpectedly reached this point, but shouldn't: " + msg);
}
private static final MethodHandle VOID_METHOD;
private static final MethodHandle RETURN_DOUBLE;
private static final MethodHandle RETURN_INT;
private static final MethodHandle PRIVATE_INTERFACE_METHOD;
private static final MethodHandle B_PRIVATE_RETURN_INT;
private static final MethodHandle A_PRIVATE_RETURN_INT;
private static final MethodHandle STATIC_METHOD;
private static final MethodHandle EXCEPTION_THROWING_METHOD;
private static final MethodHandle INTERFACE_DEFAULT_METHOD;
private static final MethodHandle OVERWRITTEN_INTERFACE_DEFAULT_METHOD;
private static final MethodHandle OPTIONAL_GET;
private static final MethodHandle SUM_I;
private static final MethodHandle SUM_2I;
private static final MethodHandle SUM_3I;
private static final MethodHandle SUM_4I;
private static final MethodHandle SUM_5I;
private static final MethodHandle SUM_6I;
private static final MethodHandle SUM_7I;
private static final MethodHandle SUM_8I;
private static final MethodHandle SUM_9I;
private static final MethodHandle SUM_10I;
private static final MethodHandle SUM_IJ;
private static final MethodHandle SUM_2IJ;
private static final MethodHandle SUM_3IJ;
private static final MethodHandle SUM_4IJ;
private static final MethodHandle SUM_5IJ;
static {
try {
VOID_METHOD = MethodHandles.lookup()
.findVirtual(A.class, "voidMethod", methodType(void.class));
RETURN_DOUBLE = MethodHandles.lookup()
.findVirtual(A.class, "returnDouble", methodType(double.class));
RETURN_INT = MethodHandles.lookup()
.findVirtual(A.class, "returnInt", methodType(int.class));
PRIVATE_INTERFACE_METHOD = MethodHandles.privateLookupIn(I.class, MethodHandles.lookup())
.findVirtual(I.class, "innerPrivateMethod", methodType(String.class));
A_PRIVATE_RETURN_INT = MethodHandles.privateLookupIn(A.class, MethodHandles.lookup())
.findVirtual(A.class, "privateReturnInt", methodType(int.class));
B_PRIVATE_RETURN_INT = MethodHandles.privateLookupIn(B.class, MethodHandles.lookup())
.findVirtual(B.class, "privateReturnInt", methodType(int.class));
STATIC_METHOD = MethodHandles.lookup()
.findStatic(A.class, "staticMethod", methodType(String.class, A.class));
EXCEPTION_THROWING_METHOD = MethodHandles.lookup()
.findVirtual(A.class, "throwException", methodType(void.class));
INTERFACE_DEFAULT_METHOD = MethodHandles.lookup()
.findVirtual(I.class, "defaultMethod", methodType(void.class));
OVERWRITTEN_INTERFACE_DEFAULT_METHOD = MethodHandles.lookup()
.findVirtual(I.class, "overrideMe", methodType(void.class));
OPTIONAL_GET = MethodHandles.lookup()
.findVirtual(Optional.class, "get", methodType(Object.class));
SUM_I = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(int.class, repeat(1, int.class)));
SUM_2I = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(int.class, repeat(2, int.class)));
SUM_3I = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(int.class, repeat(3, int.class)));
SUM_4I = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(int.class, repeat(4, int.class)));
SUM_5I = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(int.class, repeat(5, int.class)));
SUM_6I = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(int.class, repeat(6, int.class)));
SUM_7I = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(int.class, repeat(7, int.class)));
SUM_8I = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(int.class, repeat(8, int.class)));
SUM_9I = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(int.class, repeat(9, int.class)));
SUM_10I = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(int.class, repeat(10, int.class)));
SUM_IJ = MethodHandles.lookup()
.findVirtual(Sums.class, "sum", methodType(long.class, int.class, long.class));
SUM_2IJ = MethodHandles.lookup()
.findVirtual(Sums.class,
"sum",
methodType(long.class, repeat(2, int.class, long.class)));
SUM_3IJ = MethodHandles.lookup()
.findVirtual(Sums.class,
"sum",
methodType(long.class, repeat(3, int.class, long.class)));
SUM_4IJ = MethodHandles.lookup()
.findVirtual(Sums.class,
"sum",
methodType(long.class, repeat(4, int.class, long.class)));
SUM_5IJ = MethodHandles.lookup()
.findVirtual(Sums.class,
"sum",
methodType(long.class, repeat(5, int.class, long.class)));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static Class<?>[] repeat(int times, Class<?> clazz) {
Class<?>[] classes = new Class<?>[times];
Arrays.fill(classes, clazz);
return classes;
}
private static Class<?>[] repeat(int times, Class<?> first, Class<?> second) {
Class<?>[] classes = new Class<?>[times * 2];
for (int i = 0; i < 2 * times;) {
classes[i++] = first;
classes[i++] = second;
}
return classes;
}
static interface I {
public default void defaultMethod() {
System.out.println("in I.defaultMethod");
}
public default void overrideMe() {
throw new RuntimeException("should be overwritten");
}
private String innerPrivateMethod() {
return "I'm private interface method";
}
}
static class A extends B implements I {
public int field;
public void voidMethod() {
System.out.println("in voidMethod");
}
public void throwException() {
throw new RuntimeException("I am from throwException");
}
@Override
public void overrideMe() {
System.out.println("in A.overrideMe");
}
public double returnDouble() {
return 42.0d;
}
public int returnInt() {
return 42;
}
private int privateReturnInt() {
return 1042;
}
public static String staticMethod(A a) {
return "staticMethod";
}
public static double staticMethod() {
return 41.0d;
}
}
static class B {
private int privateReturnInt() {
return 9999;
}
}
static class Sums {
public int sum(int a) {
return a;
}
public int sum(int a1, int a2) {
return a1 + a2;
}
public int sum(int a1, int a2, int a3) {
return a1 + a2 + a3;
}
public int sum(int a1, int a2, int a3, int a4) {
return a1 + a2 + a3 + a4;
}
public int sum(int a1, int a2, int a3, int a4, int a5) {
return a1 + a2 + a3 + a4 + a5;
}
public int sum(int a1, int a2, int a3, int a4, int a5, int a6) {
return a1 + a2 + a3 + a4 + a5 + a6;
}
public int sum(int a1, int a2, int a3, int a4, int a5, int a6, int a7) {
return a1 + a2 + a3 + a4 + a5 + a6 + a7;
}
public int sum(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) {
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8;
}
public int sum(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9) {
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
}
public int sum(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9,
int a10) {
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10;
}
public long sum(int a1, long a2) {
return a1 + a2;
}
public long sum(int a1, long a2, int a3, long a4) {
return a1 + a2 + a3 + a4;
}
public long sum(int a1, long a2, int a3, long a4, int a5, long a6) {
return a1 + a2 + a3 + a4 + a5 + a6;
}
public long sum(int a1, long a2, int a3, long a4, int a5, long a6, int a7, long a8) {
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8;
}
public long sum(int a1, long a2, int a3, long a4, int a5, long a6, int a7, long a8, int a9,
long a10) {
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10;
}
}
}
|