-
Notifications
You must be signed in to change notification settings - Fork 41
/
Gizmo.java
1353 lines (1187 loc) · 58.2 KB
/
Gizmo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.quarkus.gizmo;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import org.objectweb.asm.Opcodes;
public final class Gizmo {
public static final int ASM_API_VERSION = Opcodes.ASM9;
private Gizmo() {
}
//================================================================================
// utils used to simplify invocation of static/non-static methods of JDK classes
// these constructs are not thread-safe and should not be used concurrently
//================================================================================
/**
* Generates the bytecode that calls {@link Object#toString()}.
*
* @param target
* @param obj
* @return the result
*/
public static ResultHandle toString(BytecodeCreator target, ResultHandle obj) {
return target.invokeVirtualMethod(TO_STRING, obj);
}
/**
* Generates the bytecode that calls {@link Object#equals(Object)}.
*
* @param target
* @param obj1
* @param obj2
*
* @return the result
*/
public static ResultHandle equals(BytecodeCreator target, ResultHandle obj1, ResultHandle obj2) {
return target.invokeVirtualMethod(EQUALS, obj1, obj2);
}
/**
* Generates the bytecode that calls {@code System.out.println(obj)}.
*
* @param target
* @param obj
*/
public static void systemOutPrintln(BytecodeCreator target, ResultHandle obj) {
target.invokeVirtualMethod(PRINTLN, target.readStaticField(SYSTEM_OUT), obj);
}
/**
* Generates the bytecode that calls {@code System.err.println(obj)}.
*
* @param target
* @param obj
*/
public static void systemErrPrintln(BytecodeCreator target, ResultHandle obj) {
target.invokeVirtualMethod(PRINTLN, target.readStaticField(SYSTEM_ERR), obj);
}
/**
* <pre>
* ResultHandle firstElement = Gizmo.invokeList(methodBytecode).instance(myList).get(1);
* </pre>
*
* @param target
* @return the generator
*/
public static JdkList listOperations(BytecodeCreator target) {
return new JdkList(target);
}
/**
* <pre>
* ResultHandle size = Gizmo.invokeCollection(methodBytecode).instance(myList).size();
* </pre>
*
* @param target
* @return the generator
*/
public static JdkCollection collectionOperations(BytecodeCreator target) {
return new JdkCollection(target);
}
/**
* <pre>
* ResultHandle set = Gizmo.invokeSet(methodBytecode).of(element);
* </pre>
*
* @param target
* @return the generator
*/
public static JdkSet setOperations(BytecodeCreator target) {
return new JdkSet(target);
}
/**
* <pre>
* ResultHandle optionalFoo = Gizmo.invokeOptional(methodBytecode).ofNullable(foo);
* </pre>
*
* @param target
* @return the generator
*/
public static JdkOptional optionalOperations(BytecodeCreator target) {
return new JdkOptional(target);
}
/**
* <pre>
* ResultHandle iterator = Gizmo.invokeIterable(methodBytecode).instance(myCollection).iterator();
* </pre>
*
* @param target
* @return the generator
*/
public static JdkIterable iterableOperations(BytecodeCreator target) {
return new JdkIterable(target);
}
/**
* <pre>
* ResultHandle hasNext = Gizmo.invokeIterator(methodBytecode).instance(myIterator).hasNext();
* </pre>
*
* @param target
* @return the generator
*/
public static JdkIterator iteratorOperations(BytecodeCreator target) {
return new JdkIterator(target);
}
/**
* <pre>
* ResultHandle mapping = Gizmo.invokeMap(methodBytecode).instance(myMap).get(myKey);
* </pre>
*
* @param target
* @return the generator
*/
public static JdkMap mapOperations(BytecodeCreator target) {
return new JdkMap(target);
}
/**
* Creates a {@code StringBuilder} generator that helps to generate a chain of
* {@code append} calls and a final {@code toString} call.
*
* <pre>
* StringBuilderGenerator str = Gizmo.newStringBuilder(bytecode);
* str.append("constant");
* str.append(someResultHandle);
* ResultHandle result = str.callToString();
* </pre>
*
* The {@code append} method mimics the regular {@code StringBuilder.append}, so
* it accepts {@code ResultHandle}s of all types for which {@code StringBuilder}
* has an overload:
* <ul>
* <li>primitive types</li>
* <li>{@code char[]}</li>
* <li>{@code java.lang.String}</li>
* <li>{@code java.lang.Object}</li>
* </ul>
*
* Notably, arrays except of {@code char[]} are appended using {@code Object.toString}
* and if {@code Arrays.toString} should be used, it must be generated manually.
* <p>
* Methods for appending only a part of {@code char[]} or {@code CharSequence} are not
* provided. Other {@code StringBuilder} methods are not provided either. This is just
* a simple utility for generating code that concatenates strings, e.g. for implementing
* the {@code toString} method.
*
* @param target
* @return the generator
*/
public static StringBuilderGenerator newStringBuilder(BytecodeCreator target) {
return new StringBuilderGenerator(target);
}
/**
* Generates a structural {@code equals} method in given {@code clazz} that compares
* given {@code fields}. The generated code is similar to what IDEs would typically
* generate from a template:
* <ol>
* <li>Reference equality is tested. If {@code this} is idential to the
* <em>other</em> object, {@code true} is returned.</li>
* <li>Type of the <em>other</em> object is tested using {@code instanceof}.
* If the <em>other</em> object is not an instance of this class, {@code false}
* is returned.</li>
* <li>All fields are compared. Primitive types are compared using {@code ==},
* object types are compared using {@code Objects.equals}, single-dimension arrays
* are compared using {@code Arrays.equals}, and multi-dimensional arrays are
* compared using {@code Arrays.deepEquals}. If one of the comparisons fails,
* {@code false} is returned.</li>
* <li>Otherwise, {@code true} is returned.</li>
* </ol>
* <p>
* If the class already has an {@code equals} method, an exception is thrown.
* If one of the fields doesn't belong to the class, an exception is thrown.
*
* @param clazz class for which {@code equals} should be generated
* @param fields fields to consider in the {@code equals} method
*/
public static void generateEquals(ClassCreator clazz, FieldDescriptor... fields) {
generateEquals(clazz, Arrays.asList(fields));
}
/**
* Generates a structural {@code equals} method in given {@code clazz} that compares
* given {@code fields}. The generated code is similar to what IDEs would typically
* generate from a template:
* <ol>
* <li>Reference equality is tested. If {@code this} is idential to the
* <em>other</em> object, {@code true} is returned.</li>
* <li>Type of the <em>other</em> object is tested using {@code instanceof}.
* If the <em>other</em> object is not an instance of this class, {@code false}
* is returned.</li>
* <li>All fields are compared. Primitive types are compared using {@code ==},
* object types are compared using {@code Objects.equals}, single-dimension arrays
* are compared using {@code Arrays.equals}, and multi-dimensional arrays are
* compared using {@code Arrays.deepEquals}. If one of the comparisons fails,
* {@code false} is returned.</li>
* <li>Otherwise, {@code true} is returned.</li>
* </ol>
* <p>
* If the class already has an {@code equals} method, an exception is thrown.
* If one of the fields doesn't belong to the class, an exception is thrown.
*
* @param clazz class for which {@code equals} should be generated
* @param fields fields to consider in the {@code equals} method
*/
public static void generateEquals(ClassCreator clazz, Collection<FieldDescriptor> fields) {
new EqualsHashCodeToStringGenerator(clazz, fields).generateEquals();
}
/**
* Generates structural {@code equals} and {@code hashCode} methods in given
* {@code clazz}, based on given {@code fields}. The generated code is similar
* to what IDEs would typically generate from a template. See
* {@link #generateEquals(ClassCreator, FieldDescriptor...)} for description
* of the generated {@code equals} method. The {@code hashCode} method is generated
* like so:
* <ol>
* <li>If no field is given, 0 is returned.</li>
* <li>Otherwise, a result variable is allocated with initial value of 1.</li>
* <li>For each field, a hash code is computed. Hash code for primitive types
* is computed using {@code Integer.hashCode} and equivalent methods, for object
* types using {@code Objects.hashCode}, for single-dimension arrays using
* {@code Arrays.hashCode}, and for multi-dimensional arrays using
* {@code Arrays.deepHashCode}. Then, the result is updated like so:
* {@code result = 31 * result + fieldHashCode}.</li>
* <li>At the end, the result is returned.</li>
* </ol>
* <p>
* If the class already has an {@code equals} or {@code hashCode} method, an exception
* is thrown. If one of the fields doesn't belong to the class, an exception is thrown.
*
* @param clazz class for which {@code equals} and {@code hashCode} should be generated
* @param fields fields to consider in the {@code equals} and {@code hashCode} methods
*/
public static void generateEqualsAndHashCode(ClassCreator clazz, FieldDescriptor... fields) {
generateEqualsAndHashCode(clazz, Arrays.asList(fields));
}
/**
* Generates structural {@code equals} and {@code hashCode} methods in given
* {@code clazz}, based on given {@code fields}. The generated code is similar
* to what IDEs would typically generate from a template. See
* {@link #generateEquals(ClassCreator, Collection)} for description
* of the generated {@code equals} method. The {@code hashCode} method is generated
* like so:
* <ol>
* <li>If no field is given, 0 is returned.</li>
* <li>Otherwise, a result variable is allocated with initial value of 1.</li>
* <li>For each field, a hash code is computed. Hash code for primitive types
* is computed using {@code Integer.hashCode} and equivalent methods, for object
* types using {@code Objects.hashCode}, for single-dimension arrays using
* {@code Arrays.hashCode}, and for multi-dimensional arrays using
* {@code Arrays.deepHashCode}. Then, the result is updated like so:
* {@code result = 31 * result + fieldHashCode}.</li>
* <li>At the end, the result is returned.</li>
* </ol>
* <p>
* If the class already has an {@code equals} or {@code hashCode} method, an exception
* is thrown. If one of the fields doesn't belong to the class, an exception is thrown.
*
* @param clazz class for which {@code equals} and {@code hashCode} should be generated
* @param fields fields to consider in the {@code equals} and {@code hashCode} methods
*/
public static void generateEqualsAndHashCode(ClassCreator clazz, Collection<FieldDescriptor> fields) {
EqualsHashCodeToStringGenerator generator = new EqualsHashCodeToStringGenerator(clazz, fields);
generator.generateEquals();
generator.generateHashCode();
}
/**
* Generates a naive {@code toString} methods in given {@code clazz}, based on given
* {@code fields}. The generated code is similar to what IDEs would typically generate
* from a template:
* <ol>
* <li>An empty {@code StringBuilder} is allocated.</li>
* <li>Simple name of the class is appended.</li>
* <li>An opening parenthesis {@code '('} is appended.</li>
* <li>For each field, its name is appended, followed by the equals sign {@code '='},
* followed by the field value. Primitive types and object types are appended
* to the {@code StringBuilder} directly, {@code Arrays.toString} is used for
* single-dimension arrays, and {@code Arrays.deepToString} for multi-dimensional
* arrays. A comma followed by a space {@code ", "} are appended between fields.
* </li>
* <li>A closing parenthesis {@code ')'} is appended.</li>
* <li>The {@code StringBuilder.toString} outcome is returned.</li>
* </ol>
* <p>
* If the class already has a {@code toString} method, an exception is thrown.
* If one of the fields doesn't belong to the class, an exception is thrown.
*
* @param clazz class for which {@code toString} should be generated
* @param fields fields to consider in the {@code toString} methods
*/
public static void generateNaiveToString(ClassCreator clazz, FieldDescriptor... fields) {
generateNaiveToString(clazz, Arrays.asList(fields));
}
/**
* Generates a naive {@code toString} methods in given {@code clazz}, based on given
* {@code fields}. The generated code is similar to what IDEs would typically generate
* from a template:
* <ol>
* <li>An empty {@code StringBuilder} is allocated.</li>
* <li>Simple name of the class is appended.</li>
* <li>An opening parenthesis {@code '('} is appended.</li>
* <li>For each field, its name is appended, followed by the equals sign {@code '='},
* followed by the field value. Primitive types and object types are appended
* to the {@code StringBuilder} directly, {@code Arrays.toString} is used for
* single-dimension arrays, and {@code Arrays.deepToString} for multi-dimensional
* arrays. A comma followed by a space {@code ", "} are appended between fields.
* </li>
* <li>A closing parenthesis {@code ')'} is appended.</li>
* <li>The {@code StringBuilder.toString} outcome is returned.</li>
* </ol>
* <p>
* If the class already has a {@code toString} method, an exception is thrown.
* If one of the fields doesn't belong to the class, an exception is thrown.
*
* @param clazz class for which {@code toString} should be generated
* @param fields fields to consider in the {@code toString} methods
*/
public static void generateNaiveToString(ClassCreator clazz, Collection<FieldDescriptor> fields) {
new EqualsHashCodeToStringGenerator(clazz, fields).generateToString();
}
/**
* Generates the bytecode that calls the no-args {@link HashMap} constructor.
*
* @param target
* @return the result handle
*/
public static ResultHandle newHashMap(BytecodeCreator target) {
return target.newInstance(MethodDescriptor.ofConstructor(HashMap.class));
}
/**
* Generates the bytecode that calls the no-args {@link HashSet} constructor.
*
* @param target
* @return the result handle
*/
public static ResultHandle newHashSet(BytecodeCreator target) {
return target.newInstance(MethodDescriptor.ofConstructor(HashSet.class));
}
/**
* Generates the bytecode that calls the no-args {@link ArrayList} constructor.
*
* @param target
* @return the result handle
*/
public static ResultHandle newArrayList(BytecodeCreator target) {
return target.newInstance(MethodDescriptor.ofConstructor(ArrayList.class));
}
/**
* Generates the bytecode that calls the {@link ArrayList} constructor with the specified initial capacity.
*
* @param target
* @return the result handle
*/
public static ResultHandle newArrayList(BytecodeCreator target, int initialCapacity) {
return target.newInstance(MethodDescriptor.ofConstructor(ArrayList.class, int.class), target.load(initialCapacity));
}
// Implementation classes
/**
* An abstract base for all generators.
* <p>
* This construct is not thread-safe and should not be used concurrently.
*/
public abstract static class StaticInvocationGenerator {
protected BytecodeCreator target;
StaticInvocationGenerator(BytecodeCreator target) {
setTarget(target);
}
void setTarget(BytecodeCreator target) {
this.target = Objects.requireNonNull(target);
}
}
/**
* An abstract base for all instance invokers.
*/
public abstract static class InstanceInvocationGenerator {
protected final ResultHandle instance;
InstanceInvocationGenerator(ResultHandle instance) {
this.instance = Objects.requireNonNull(instance);
}
}
/**
* Helper class to build a bytecode generator for repetitive tasks.
*
* <pre>
* CustomInvocationGenerator alwaysReturnTrue = new CustomInvocationGenerator(methodBytecode, (bc, args) -> bc.load(true));
* ResultHandle true1 = alwaysReturnTrue.invoke();
* ResultHandle true2 = alwaysReturnTrue.invoke();
* </pre>
*
*/
public static class CustomInvocationGenerator extends StaticInvocationGenerator {
private final BiFunction<BytecodeCreator, ResultHandle[], ResultHandle> fun;
public CustomInvocationGenerator(BytecodeCreator target,
BiFunction<BytecodeCreator, ResultHandle[], ResultHandle> fun) {
super(target);
this.fun = fun;
}
public ResultHandle invoke(ResultHandle... args) {
return fun.apply(target, args);
}
}
/**
* Bytecode generator for static methods.
*
* @see Optional
* @see #on(ResultHandle)
*/
public static class JdkOptional extends StaticInvocationGenerator {
public static final MethodDescriptor OF = MethodDescriptor.ofMethod(Optional.class, "of", Optional.class, Object.class);
public static final MethodDescriptor OF_NULLABLE = MethodDescriptor.ofMethod(Optional.class, "ofNullable",
Optional.class,
Object.class);
public static final MethodDescriptor GET = MethodDescriptor.ofMethod(Optional.class, "get", Object.class);
public static final MethodDescriptor IS_PRESENT = MethodDescriptor.ofMethod(Optional.class, "isPresent", boolean.class);
public static final MethodDescriptor IS_EMPTY = MethodDescriptor.ofMethod(Optional.class, "isEmpty", boolean.class);
public JdkOptional(BytecodeCreator target) {
super(target);
}
/**
*
* @return bytecode generator for instance methods
*/
public JdkOptionalInstance on(ResultHandle optional) {
return new JdkOptionalInstance(optional);
}
/**
*
* @param value
* @return the result handle
* @see Optional#of(Object)
*/
public ResultHandle of(ResultHandle value) {
return target.invokeStaticMethod(OF, value);
}
/**
*
* @param value
* @return the result handle
* @see Optional#ofNullable(Object)
*/
public ResultHandle ofNullable(ResultHandle value) {
return target.invokeStaticMethod(OF_NULLABLE, value);
}
public class JdkOptionalInstance extends InstanceInvocationGenerator {
JdkOptionalInstance(ResultHandle instance) {
super(instance);
}
/**
*
* @return the result handle
* @see Optional#isPresent()
*/
public ResultHandle isPresent() {
return target.invokeVirtualMethod(IS_PRESENT,
instance);
}
/**
*
* @return the result handle
* @see Optional#isEmpty()
*/
public ResultHandle isEmpty() {
return target.invokeVirtualMethod(IS_EMPTY,
instance);
}
/**
*
* @return the result handle
* @see Optional#get()
*/
public ResultHandle get() {
return target.invokeVirtualMethod(GET,
instance);
}
}
}
/**
* Bytecode generator for static methods.
*
* @see Iterable
* @see #on(ResultHandle)
*/
public static class JdkIterable extends StaticInvocationGenerator {
public static final MethodDescriptor ITERATOR = MethodDescriptor.ofMethod(Iterable.class, "iterator", Iterator.class);
public JdkIterable(BytecodeCreator target) {
super(target);
}
/**
*
* @return bytecode generator for instance methods
*/
public JdkIterableInstance on(ResultHandle iterable) {
return new JdkIterableInstance(iterable);
}
public class JdkIterableInstance extends InstanceInvocationGenerator {
JdkIterableInstance(ResultHandle instance) {
super(instance);
}
/**
*
* @return the result handle
* @see Iterable#iterator()
*/
public ResultHandle iterator() {
return target.invokeInterfaceMethod(ITERATOR, instance);
}
}
}
/**
* Bytecode generator for static methods.
*
* @see Iterator
* @see #on(ResultHandle)
*/
public static class JdkIterator extends StaticInvocationGenerator {
public static final MethodDescriptor NEXT = MethodDescriptor.ofMethod(Iterator.class, "next", Object.class);
public static final MethodDescriptor HAS_NEXT = MethodDescriptor.ofMethod(Iterator.class, "hasNext", boolean.class);
public JdkIterator(BytecodeCreator target) {
super(target);
}
/**
*
* @return bytecode generator for instance methods
*/
public JdkIteratorInstance on(ResultHandle iterator) {
return new JdkIteratorInstance(iterator);
}
public class JdkIteratorInstance extends InstanceInvocationGenerator {
JdkIteratorInstance(ResultHandle instance) {
super(instance);
}
/**
*
* @return the result handle
* @see Iterator#next()
*/
public ResultHandle next() {
return target.invokeInterfaceMethod(NEXT, instance);
}
/**
*
* @return the result handle
* @see Iterator#hasNext()
*/
public ResultHandle hasNext() {
return target.invokeInterfaceMethod(HAS_NEXT, instance);
}
}
}
/**
* Bytecode generator for static methods.
*
* @see Collection
* @see #on(ResultHandle)
*/
public static class JdkCollection extends JdkIterable {
public static final MethodDescriptor SIZE = MethodDescriptor.ofMethod(Collection.class, "size", int.class);
public static final MethodDescriptor IS_EMPTY = MethodDescriptor.ofMethod(Collection.class, "isEmpty", boolean.class);
public static final MethodDescriptor CONTAINS = MethodDescriptor.ofMethod(Collection.class, "contains", boolean.class,
Object.class);
public static final MethodDescriptor ADD = MethodDescriptor.ofMethod(Collection.class, "add", boolean.class,
Object.class);
public static final MethodDescriptor ADD_ALL = MethodDescriptor.ofMethod(Collection.class, "addAll", boolean.class,
Collection.class);
public static final MethodDescriptor CLEAR = MethodDescriptor.ofMethod(Collection.class, "clear", void.class);
public JdkCollection(BytecodeCreator target) {
super(target);
}
/**
*
* @return bytecode generator for instance methods
*/
public JdkCollectionInstance on(ResultHandle list) {
return new JdkCollectionInstance(list);
}
public class JdkCollectionInstance extends JdkIterableInstance {
JdkCollectionInstance(ResultHandle instance) {
super(instance);
}
/**
*
* @return the result handle
* @see Collection#size()
*/
public ResultHandle size() {
return target.invokeInterfaceMethod(SIZE, instance);
}
/**
*
* @return the result handle
* @see Collection#isEmpty()
*/
public ResultHandle isEmpty() {
return target.invokeInterfaceMethod(IS_EMPTY,
instance);
}
/**
*
* @return the result handle
* @see Collection#contains(Object)
*/
public ResultHandle contains(ResultHandle obj) {
return target.invokeInterfaceMethod(CONTAINS,
instance, obj);
}
/**
*
* @return the result handle
* @see Collection#add(Object)
*/
public ResultHandle add(ResultHandle element) {
return target.invokeInterfaceMethod(ADD,
instance, element);
}
/**
*
* @return the result handle
* @see Collection#addAll(Collection)
*/
public ResultHandle addAll(ResultHandle collection) {
return target.invokeInterfaceMethod(ADD_ALL,
instance, collection);
}
/**
*
* @see Collection#clear()
*/
public void clear() {
target.invokeInterfaceMethod(CLEAR, instance);
}
}
}
/**
* Bytecode generator for static methods.
*
* @see List
* @see #on(ResultHandle)
*/
public static class JdkList extends JdkCollection {
public static final MethodDescriptor GET = MethodDescriptor.ofMethod(List.class, "get", Object.class, int.class);
public static final MethodDescriptor OF1 = MethodDescriptor.ofMethod(List.class, "of", List.class, Object.class);
public static final MethodDescriptor OF2 = MethodDescriptor.ofMethod(List.class, "of", List.class, Object.class,
Object.class);
public static final MethodDescriptor OF3 = MethodDescriptor.ofMethod(List.class, "of", List.class, Object.class,
Object.class, Object.class);
public JdkList(BytecodeCreator target) {
super(target);
}
/**
*
* @return bytecode generator for instance methods
*/
public JdkListInstance on(ResultHandle list) {
return new JdkListInstance(list);
}
/**
*
* @param e1
* @return the result handle
* @see List#of(Object)
*/
public ResultHandle of(ResultHandle e1) {
return target.invokeStaticInterfaceMethod(OF1, e1);
}
/**
*
* @param e1
* @param e2
* @return the result handle
* @see List#of(Object, Object)
*/
public ResultHandle of(ResultHandle e1, ResultHandle e2) {
return target.invokeStaticInterfaceMethod(OF2, e1, e2);
}
/**
*
* @param e1
* @param e2
* @param e3
* @return the result handle
* @see List#of(Object, Object, Object)
*/
public ResultHandle of(ResultHandle e1, ResultHandle e2, ResultHandle e3) {
return target.invokeStaticInterfaceMethod(OF3, e1, e2, e3);
}
public class JdkListInstance extends JdkCollectionInstance {
JdkListInstance(ResultHandle instance) {
super(instance);
}
/**
*
* @param index
* @return the result handle
* @see List#get(int)
*/
public ResultHandle get(int index) {
return get(target.load(index));
}
/**
*
* @param index
* @return the result handle
* @see List#get(int)
*/
public ResultHandle get(ResultHandle index) {
return target.invokeInterfaceMethod(GET, instance,
index);
}
}
}
/**
*
* @see Set
*/
public static class JdkSet extends JdkCollection {
public static final MethodDescriptor OF1 = MethodDescriptor.ofMethod(Set.class, "of", Set.class, Object.class);
public static final MethodDescriptor OF2 = MethodDescriptor.ofMethod(Set.class, "of", Set.class, Object.class,
Object.class);
public static final MethodDescriptor OF3 = MethodDescriptor.ofMethod(Set.class, "of", Set.class, Object.class,
Object.class, Object.class);
public JdkSet(BytecodeCreator target) {
super(target);
}
public JdkSetInstance on(ResultHandle set) {
return new JdkSetInstance(set);
}
public ResultHandle of(ResultHandle e1) {
return target.invokeStaticInterfaceMethod(OF1, e1);
}
public ResultHandle of(ResultHandle e1, ResultHandle e2) {
return target.invokeStaticInterfaceMethod(OF2, e1, e2);
}
public ResultHandle of(ResultHandle e1, ResultHandle e2, ResultHandle e3) {
return target.invokeStaticInterfaceMethod(OF3, e1, e2, e3);
}
public class JdkSetInstance extends JdkCollectionInstance {
JdkSetInstance(ResultHandle instance) {
super(instance);
}
}
}
/**
*
* @see Map
*/
public static class JdkMap extends StaticInvocationGenerator {
public static final MethodDescriptor GET = MethodDescriptor.ofMethod(Map.class, "get", Object.class, Object.class);
public static final MethodDescriptor PUT = MethodDescriptor.ofMethod(Map.class, "put", Object.class, Object.class,
Object.class);
public static final MethodDescriptor OF1 = MethodDescriptor.ofMethod(Map.class, "of", Map.class, Object.class,
Object.class);
public static final MethodDescriptor SIZE = MethodDescriptor.ofMethod(Map.class, "size", int.class);
public static final MethodDescriptor IS_EMPTY = MethodDescriptor.ofMethod(Map.class, "isEmpty", boolean.class);
public static final MethodDescriptor CONTAINS_KEY = MethodDescriptor.ofMethod(Map.class, "containsKey", boolean.class,
Object.class);
public JdkMap(BytecodeCreator target) {
super(target);
}
/**
* @deprecated use {@link #on(ResultHandle)}
*/
@Deprecated
public JdkMapInstance instance(ResultHandle map) {
return new JdkMapInstance(map);
}
public JdkMapInstance on(ResultHandle map) {
return new JdkMapInstance(map);
}
public ResultHandle of(ResultHandle k1, ResultHandle v1) {
return target.invokeStaticInterfaceMethod(OF1, k1, v1);
}
public class JdkMapInstance extends InstanceInvocationGenerator {
JdkMapInstance(ResultHandle instance) {
super(instance);
}
public ResultHandle get(ResultHandle key) {
return target.invokeInterfaceMethod(GET, instance,
key);
}
public ResultHandle put(ResultHandle key, ResultHandle val) {
return target.invokeInterfaceMethod(PUT, instance,
key, val);
}
public ResultHandle size() {
return target.invokeInterfaceMethod(SIZE, instance);
}
public ResultHandle isEmpty() {
return target.invokeInterfaceMethod(IS_EMPTY,
instance);
}
public ResultHandle containsKey(ResultHandle key) {
return target.invokeInterfaceMethod(CONTAINS_KEY,
instance, key);
}
}
}
public static class StringBuilderGenerator {
private static final MethodDescriptor CONSTRUCTOR = MethodDescriptor.ofConstructor(StringBuilder.class);
private static final MethodDescriptor APPEND_BOOLEAN = MethodDescriptor.ofMethod(StringBuilder.class, "append", StringBuilder.class, boolean.class);
private static final MethodDescriptor APPEND_INT = MethodDescriptor.ofMethod(StringBuilder.class, "append", StringBuilder.class, int.class);
private static final MethodDescriptor APPEND_LONG = MethodDescriptor.ofMethod(StringBuilder.class, "append", StringBuilder.class, long.class);
private static final MethodDescriptor APPEND_FLOAT = MethodDescriptor.ofMethod(StringBuilder.class, "append", StringBuilder.class, float.class);
private static final MethodDescriptor APPEND_DOUBLE = MethodDescriptor.ofMethod(StringBuilder.class, "append", StringBuilder.class, double.class);
private static final MethodDescriptor APPEND_CHAR = MethodDescriptor.ofMethod(StringBuilder.class, "append", StringBuilder.class, char.class);
private static final MethodDescriptor APPEND_CHAR_ARRAY = MethodDescriptor.ofMethod(StringBuilder.class, "append", StringBuilder.class, char[].class);
private static final MethodDescriptor APPEND_STRING = MethodDescriptor.ofMethod(StringBuilder.class, "append", StringBuilder.class, String.class);
private static final MethodDescriptor APPEND_CHAR_SEQUENCE = MethodDescriptor.ofMethod(StringBuilder.class, "append", StringBuilder.class, CharSequence.class);
private static final MethodDescriptor APPEND_OBJECT = MethodDescriptor.ofMethod(StringBuilder.class, "append", StringBuilder.class, Object.class);
private static final MethodDescriptor TO_STRING = MethodDescriptor.ofMethod(StringBuilder.class, "toString", String.class);
private final BytecodeCreator bytecode;
private final ResultHandle instance;
private StringBuilderGenerator(BytecodeCreator bytecode) {
this.bytecode = bytecode;
this.instance = bytecode.newInstance(CONSTRUCTOR);
}
public StringBuilderGenerator append(ResultHandle value) {
switch (value.getType()) {
case "Z": // boolean
bytecode.invokeVirtualMethod(APPEND_BOOLEAN, instance, value);
break;
case "B": // byte
case "S": // short
case "I": // int
bytecode.invokeVirtualMethod(APPEND_INT, instance, value);
break;
case "J": // long
bytecode.invokeVirtualMethod(APPEND_LONG, instance, value);
break;
case "F": // float
bytecode.invokeVirtualMethod(APPEND_FLOAT, instance, value);
break;
case "D": // double
bytecode.invokeVirtualMethod(APPEND_DOUBLE, instance, value);
break;
case "C": // char
bytecode.invokeVirtualMethod(APPEND_CHAR, instance, value);
break;
case "[C": // char[]
bytecode.invokeVirtualMethod(APPEND_CHAR_ARRAY, instance, value);
break;
case "Ljava/lang/String;":
bytecode.invokeVirtualMethod(APPEND_STRING, instance, value);
break;
case "Ljava/lang/CharSequence;":
bytecode.invokeVirtualMethod(APPEND_CHAR_SEQUENCE, instance, value);
break;