Repository navigation
Expand file tree
/
Copy pathSymbolTable.java
More file actions
1463 lines (1355 loc) · 59.4 KB
/
Copy pathSymbolTable.java
File metadata and controls
1463 lines (1355 loc) · 59.4 KB
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
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm;
/**
* The constant pool entries, the BootstrapMethods attribute entries and the (ASM specific) type
* table entries of a class.
*
* @author Eric Bruneton
* @see <a href="https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.4">JVMS
* 4.4</a>
* @see <a href="https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.7.23">JVMS
* 4.7.23</a>
*/
final class SymbolTable {
/**
* The ClassWriter to which this SymbolTable belongs. This is only used to get access to {@link
* ClassWriter#getCommonSuperClass} and to serialize custom attributes with {@link
* Attribute#write}.
*/
final ClassWriter classWriter;
/**
* The ClassReader from which this SymbolTable was constructed, or {@literal null} if it was
* constructed from scratch.
*/
private final ClassReader sourceClassReader;
/** The major version number of the class to which this symbol table belongs. */
private int majorVersion;
/** The internal name of the class to which this symbol table belongs. */
private String className;
/**
* The total number of {@link Entry} instances in {@link #entries}. This includes entries that are
* accessible (recursively) via {@link Entry#next}.
*/
private int entryCount;
/**
* A hash set of all the entries in this SymbolTable (this includes the constant pool entries, the
* bootstrap method entries and the type table entries). Each {@link Entry} instance is stored at
* the array index given by its hash code modulo the array size. If several entries must be stored
* at the same array index, they are linked together via their {@link Entry#next} field. The
* factory methods of this class make sure that this table does not contain duplicated entries.
*/
private Entry[] entries;
/**
* The number of constant pool items in {@link #constantPool}, plus 1. The first constant pool
* item has index 1, and long and double items count for two items.
*/
private int constantPoolCount;
/**
* The content of the ClassFile's constant_pool JVMS structure corresponding to this SymbolTable.
* The ClassFile's constant_pool_count field is <i>not</i> included.
*/
private ByteVector constantPool;
/**
* The number of bootstrap methods in {@link #bootstrapMethods}. Corresponds to the
* BootstrapMethods_attribute's num_bootstrap_methods field value.
*/
private int bootstrapMethodCount;
/**
* The content of the BootstrapMethods attribute 'bootstrap_methods' array corresponding to this
* SymbolTable. Note that the first 6 bytes of the BootstrapMethods_attribute, and its
* num_bootstrap_methods field, are <i>not</i> included.
*/
private ByteVector bootstrapMethods;
/**
* The actual number of elements in {@link #typeTable}. These elements are stored from index 0 to
* typeCount (excluded). The other array entries are empty.
*/
private int typeCount;
/**
* An ASM specific type table used to temporarily store internal names that will not necessarily
* be stored in the constant pool. This type table is used by the control flow and data flow
* analysis algorithm used to compute stack map frames from scratch. This array stores {@link
* Symbol#TYPE_TAG}, {@link Symbol#UNINITIALIZED_TYPE_TAG},{@link
* Symbol#FORWARD_UNINITIALIZED_TYPE_TAG} and {@link Symbol#MERGED_TYPE_TAG} entries. The type
* symbol at index {@code i} has its {@link Symbol#index} equal to {@code i} (and vice versa).
*/
private Entry[] typeTable;
/**
* The actual number of {@link LabelEntry} in {@link #labelTable}. These elements are stored from
* index 0 to labelCount (excluded). The other array entries are empty. These label entries are
* also stored in the {@link #labelEntries} hash set.
*/
private int labelCount;
/**
* The labels corresponding to the "forward uninitialized" types in the ASM specific {@link
* typeTable} (see {@link Symbol#FORWARD_UNINITIALIZED_TYPE_TAG}). The label entry at index {@code
* i} has its {@link LabelEntry#index} equal to {@code i} (and vice versa).
*/
private LabelEntry[] labelTable;
/**
* A hash set of all the {@link LabelEntry} elements in the {@link #labelTable}. Each {@link
* LabelEntry} instance is stored at the array index given by its hash code modulo the array size.
* If several entries must be stored at the same array index, they are linked together via their
* {@link LabelEntry#next} field. The {@link #getOrAddLabelEntry(Label)} method ensures that this
* table does not contain duplicated entries.
*/
private LabelEntry[] labelEntries;
/**
* Constructs a new, empty SymbolTable for the given ClassWriter.
*
* @param classWriter a ClassWriter.
*/
SymbolTable(final ClassWriter classWriter) {
this.classWriter = classWriter;
this.sourceClassReader = null;
this.entries = new Entry[256];
this.constantPoolCount = 1;
this.constantPool = new ByteVector();
}
/**
* Constructs a new SymbolTable for the given ClassWriter, initialized with the constant pool and
* bootstrap methods of the given ClassReader.
*
* @param classWriter a ClassWriter.
* @param classReader the ClassReader whose constant pool and bootstrap methods must be copied to
* initialize the SymbolTable.
*/
SymbolTable(final ClassWriter classWriter, final ClassReader classReader) {
this.classWriter = classWriter;
this.sourceClassReader = classReader;
// Copy the constant pool binary content.
byte[] inputBytes = classReader.classFileBuffer;
int constantPoolOffset = classReader.getItem(1) - 1;
int constantPoolLength = classReader.header - constantPoolOffset;
constantPoolCount = classReader.getItemCount();
constantPool = new ByteVector(constantPoolLength);
constantPool.putByteArray(inputBytes, constantPoolOffset, constantPoolLength);
// Add the constant pool items in the symbol table entries. Reserve enough space in 'entries' to
// avoid too many hash set collisions (entries is not dynamically resized by the addConstant*
// method calls below), and to account for bootstrap method entries.
entries = new Entry[constantPoolCount * 2];
char[] charBuffer = new char[classReader.getMaxStringLength()];
boolean hasBootstrapMethods = false;
int itemIndex = 1;
while (itemIndex < constantPoolCount) {
int itemOffset = classReader.getItem(itemIndex);
int itemTag = inputBytes[itemOffset - 1];
int nameAndTypeItemOffset;
switch (itemTag) {
case Symbol.CONSTANT_FIELDREF_TAG:
case Symbol.CONSTANT_METHODREF_TAG:
case Symbol.CONSTANT_INTERFACE_METHODREF_TAG:
nameAndTypeItemOffset =
classReader.getItem(classReader.readUnsignedShort(itemOffset + 2));
addConstantMemberReference(
itemIndex,
itemTag,
classReader.readClass(itemOffset, charBuffer),
classReader.readUTF8(nameAndTypeItemOffset, charBuffer),
classReader.readUTF8(nameAndTypeItemOffset + 2, charBuffer));
break;
case Symbol.CONSTANT_INTEGER_TAG:
case Symbol.CONSTANT_FLOAT_TAG:
addConstantIntegerOrFloat(itemIndex, itemTag, classReader.readInt(itemOffset));
break;
case Symbol.CONSTANT_NAME_AND_TYPE_TAG:
addConstantNameAndType(
itemIndex,
classReader.readUTF8(itemOffset, charBuffer),
classReader.readUTF8(itemOffset + 2, charBuffer));
break;
case Symbol.CONSTANT_LONG_TAG:
case Symbol.CONSTANT_DOUBLE_TAG:
addConstantLongOrDouble(itemIndex, itemTag, classReader.readLong(itemOffset));
break;
case Symbol.CONSTANT_UTF8_TAG:
addConstantUtf8(itemIndex, classReader.readUtf(itemIndex, charBuffer));
break;
case Symbol.CONSTANT_METHOD_HANDLE_TAG:
int memberRefItemOffset =
classReader.getItem(classReader.readUnsignedShort(itemOffset + 1));
nameAndTypeItemOffset =
classReader.getItem(classReader.readUnsignedShort(memberRefItemOffset + 2));
addConstantMethodHandle(
itemIndex,
classReader.readByte(itemOffset),
classReader.readClass(memberRefItemOffset, charBuffer),
classReader.readUTF8(nameAndTypeItemOffset, charBuffer),
classReader.readUTF8(nameAndTypeItemOffset + 2, charBuffer));
break;
case Symbol.CONSTANT_DYNAMIC_TAG:
case Symbol.CONSTANT_INVOKE_DYNAMIC_TAG:
hasBootstrapMethods = true;
nameAndTypeItemOffset =
classReader.getItem(classReader.readUnsignedShort(itemOffset + 2));
addConstantDynamicOrInvokeDynamicReference(
itemTag,
itemIndex,
classReader.readUTF8(nameAndTypeItemOffset, charBuffer),
classReader.readUTF8(nameAndTypeItemOffset + 2, charBuffer),
classReader.readUnsignedShort(itemOffset));
break;
case Symbol.CONSTANT_STRING_TAG:
case Symbol.CONSTANT_CLASS_TAG:
case Symbol.CONSTANT_METHOD_TYPE_TAG:
case Symbol.CONSTANT_MODULE_TAG:
case Symbol.CONSTANT_PACKAGE_TAG:
addConstantUtf8Reference(
itemIndex, itemTag, classReader.readUTF8(itemOffset, charBuffer));
break;
default:
throw new IllegalArgumentException();
}
itemIndex +=
(itemTag == Symbol.CONSTANT_LONG_TAG || itemTag == Symbol.CONSTANT_DOUBLE_TAG) ? 2 : 1;
}
// Copy the BootstrapMethods, if any.
if (hasBootstrapMethods) {
copyBootstrapMethods(classReader, charBuffer);
}
}
/**
* Read the BootstrapMethods 'bootstrap_methods' array binary content and add them as entries of
* the SymbolTable.
*
* @param classReader the ClassReader whose bootstrap methods must be copied to initialize the
* SymbolTable.
* @param charBuffer a buffer used to read strings in the constant pool.
*/
private void copyBootstrapMethods(final ClassReader classReader, final char[] charBuffer) {
// Find attributOffset of the 'bootstrap_methods' array.
byte[] inputBytes = classReader.classFileBuffer;
int currentAttributeOffset = classReader.getFirstAttributeOffset();
for (int i = classReader.readUnsignedShort(currentAttributeOffset - 2); i > 0; --i) {
String attributeName = classReader.readUTF8(currentAttributeOffset, charBuffer);
if (Constants.BOOTSTRAP_METHODS.equals(attributeName)) {
bootstrapMethodCount = classReader.readUnsignedShort(currentAttributeOffset + 6);
break;
}
currentAttributeOffset += 6 + classReader.readInt(currentAttributeOffset + 2);
}
if (bootstrapMethodCount > 0) {
// Compute the offset and the length of the BootstrapMethods 'bootstrap_methods' array.
int bootstrapMethodsOffset = currentAttributeOffset + 8;
int bootstrapMethodsLength = classReader.readInt(currentAttributeOffset + 2) - 2;
bootstrapMethods = new ByteVector(bootstrapMethodsLength);
bootstrapMethods.putByteArray(inputBytes, bootstrapMethodsOffset, bootstrapMethodsLength);
// Add each bootstrap method in the symbol table entries.
int currentOffset = bootstrapMethodsOffset;
for (int i = 0; i < bootstrapMethodCount; i++) {
int offset = currentOffset - bootstrapMethodsOffset;
int bootstrapMethodRef = classReader.readUnsignedShort(currentOffset);
currentOffset += 2;
int numBootstrapArguments = classReader.readUnsignedShort(currentOffset);
currentOffset += 2;
int hashCode = classReader.readConst(bootstrapMethodRef, charBuffer).hashCode();
while (numBootstrapArguments-- > 0) {
int bootstrapArgument = classReader.readUnsignedShort(currentOffset);
currentOffset += 2;
hashCode ^= classReader.readConst(bootstrapArgument, charBuffer).hashCode();
}
add(new Entry(i, Symbol.BOOTSTRAP_METHOD_TAG, offset, hashCode & 0x7FFFFFFF));
}
}
}
/**
* Returns the ClassReader from which this SymbolTable was constructed.
*
* @return the ClassReader from which this SymbolTable was constructed, or {@literal null} if it
* was constructed from scratch.
*/
ClassReader getSource() {
return sourceClassReader;
}
/**
* Returns the major version of the class to which this symbol table belongs.
*
* @return the major version of the class to which this symbol table belongs.
*/
int getMajorVersion() {
return majorVersion;
}
/**
* Returns the internal name of the class to which this symbol table belongs.
*
* @return the internal name of the class to which this symbol table belongs.
*/
String getClassName() {
return className;
}
/**
* Sets the major version and the name of the class to which this symbol table belongs. Also adds
* the class name to the constant pool.
*
* @param majorVersion a major ClassFile version number.
* @param className an internal class name.
* @return the constant pool index of a new or already existing Symbol with the given class name.
*/
int setMajorVersionAndClassName(final int majorVersion, final String className) {
this.majorVersion = majorVersion;
this.className = className;
return addConstantClass(className).index;
}
/**
* Returns the number of items in this symbol table's constant_pool array (plus 1).
*
* @return the number of items in this symbol table's constant_pool array (plus 1).
*/
int getConstantPoolCount() {
return constantPoolCount;
}
/**
* Returns the length in bytes of this symbol table's constant_pool array.
*
* @return the length in bytes of this symbol table's constant_pool array.
*/
int getConstantPoolLength() {
return constantPool.length;
}
/**
* Puts this symbol table's constant_pool array in the given ByteVector, preceded by the
* constant_pool_count value.
*
* @param output where the JVMS ClassFile's constant_pool array must be put.
*/
void putConstantPool(final ByteVector output) {
output.putShort(constantPoolCount).putByteArray(constantPool.data, 0, constantPool.length);
}
/**
* Returns the size in bytes of this symbol table's BootstrapMethods attribute. Also adds the
* attribute name in the constant pool.
*
* @return the size in bytes of this symbol table's BootstrapMethods attribute.
*/
int computeBootstrapMethodsSize() {
if (bootstrapMethods != null) {
addConstantUtf8(Constants.BOOTSTRAP_METHODS);
return 8 + bootstrapMethods.length;
} else {
return 0;
}
}
/**
* Puts this symbol table's BootstrapMethods attribute in the given ByteVector. This includes the
* 6 attribute header bytes and the num_bootstrap_methods value.
*
* @param output where the JVMS BootstrapMethods attribute must be put.
*/
void putBootstrapMethods(final ByteVector output) {
if (bootstrapMethods != null) {
output
.putShort(addConstantUtf8(Constants.BOOTSTRAP_METHODS))
.putInt(bootstrapMethods.length + 2)
.putShort(bootstrapMethodCount)
.putByteArray(bootstrapMethods.data, 0, bootstrapMethods.length);
}
}
// -----------------------------------------------------------------------------------------------
// Generic symbol table entries management.
// -----------------------------------------------------------------------------------------------
/**
* Returns the list of entries which can potentially have the given hash code.
*
* @param hashCode a {@link Entry#hashCode} value.
* @return the list of entries which can potentially have the given hash code. The list is stored
* via the {@link Entry#next} field.
*/
private Entry get(final int hashCode) {
return entries[hashCode % entries.length];
}
/**
* Puts the given entry in the {@link #entries} hash set. This method does <i>not</i> check
* whether {@link #entries} already contains a similar entry or not. {@link #entries} is resized
* if necessary to avoid hash collisions (multiple entries needing to be stored at the same {@link
* #entries} array index) as much as possible, with reasonable memory usage.
*
* @param entry an Entry (which must not already be contained in {@link #entries}).
* @return the given entry
*/
private Entry put(final Entry entry) {
if (entryCount > (entries.length * 3) / 4) {
int currentCapacity = entries.length;
int newCapacity = currentCapacity * 2 + 1;
Entry[] newEntries = new Entry[newCapacity];
for (int i = currentCapacity - 1; i >= 0; --i) {
Entry currentEntry = entries[i];
while (currentEntry != null) {
int newCurrentEntryIndex = currentEntry.hashCode % newCapacity;
Entry nextEntry = currentEntry.next;
currentEntry.next = newEntries[newCurrentEntryIndex];
newEntries[newCurrentEntryIndex] = currentEntry;
currentEntry = nextEntry;
}
}
entries = newEntries;
}
entryCount++;
int index = entry.hashCode % entries.length;
entry.next = entries[index];
return entries[index] = entry;
}
/**
* Adds the given entry in the {@link #entries} hash set. This method does <i>not</i> check
* whether {@link #entries} already contains a similar entry or not, and does <i>not</i> resize
* {@link #entries} if necessary.
*
* @param entry an Entry (which must not already be contained in {@link #entries}).
*/
private void add(final Entry entry) {
entryCount++;
int index = entry.hashCode % entries.length;
entry.next = entries[index];
entries[index] = entry;
}
// -----------------------------------------------------------------------------------------------
// Constant pool entries management.
// -----------------------------------------------------------------------------------------------
/**
* Adds a number or string constant to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param value the value of the constant to be added to the constant pool. This parameter must be
* an {@link Integer}, {@link Byte}, {@link Character}, {@link Short}, {@link Boolean}, {@link
* Float}, {@link Long}, {@link Double}, {@link String}, {@link Type} or {@link Handle}.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstant(final Object value) {
if (value instanceof Integer) {
return addConstantInteger(((Integer) value).intValue());
} else if (value instanceof Byte) {
return addConstantInteger(((Byte) value).intValue());
} else if (value instanceof Character) {
return addConstantInteger(((Character) value).charValue());
} else if (value instanceof Short) {
return addConstantInteger(((Short) value).intValue());
} else if (value instanceof Boolean) {
return addConstantInteger(((Boolean) value).booleanValue() ? 1 : 0);
} else if (value instanceof Float) {
return addConstantFloat(((Float) value).floatValue());
} else if (value instanceof Long) {
return addConstantLong(((Long) value).longValue());
} else if (value instanceof Double) {
return addConstantDouble(((Double) value).doubleValue());
} else if (value instanceof String) {
return addConstantString((String) value);
} else if (value instanceof Type) {
Type type = (Type) value;
int typeSort = type.getSort();
if (typeSort == Type.OBJECT) {
return addConstantClass(type.getInternalName());
} else if (typeSort == Type.METHOD) {
return addConstantMethodType(type.getDescriptor());
} else { // type is a primitive or array type.
return addConstantClass(type.getDescriptor());
}
} else if (value instanceof Handle) {
Handle handle = (Handle) value;
return addConstantMethodHandle(
handle.getTag(),
handle.getOwner(),
handle.getName(),
handle.getDesc(),
handle.isInterface());
} else if (value instanceof ConstantDynamic) {
ConstantDynamic constantDynamic = (ConstantDynamic) value;
return addConstantDynamic(
constantDynamic.getName(),
constantDynamic.getDescriptor(),
constantDynamic.getBootstrapMethod(),
constantDynamic.getBootstrapMethodArgumentsUnsafe());
} else {
throw new IllegalArgumentException("value " + value);
}
}
/**
* Adds a CONSTANT_Class_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param value the internal name of a class.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantClass(final String value) {
return addConstantUtf8Reference(Symbol.CONSTANT_CLASS_TAG, value);
}
/**
* Adds a CONSTANT_Fieldref_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param owner the internal name of a class.
* @param name a field name.
* @param descriptor a field descriptor.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantFieldref(final String owner, final String name, final String descriptor) {
return addConstantMemberReference(Symbol.CONSTANT_FIELDREF_TAG, owner, name, descriptor);
}
/**
* Adds a CONSTANT_Methodref_info or CONSTANT_InterfaceMethodref_info to the constant pool of this
* symbol table. Does nothing if the constant pool already contains a similar item.
*
* @param owner the internal name of a class.
* @param name a method name.
* @param descriptor a method descriptor.
* @param isInterface whether owner is an interface or not.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantMethodref(
final String owner, final String name, final String descriptor, final boolean isInterface) {
int tag = isInterface ? Symbol.CONSTANT_INTERFACE_METHODREF_TAG : Symbol.CONSTANT_METHODREF_TAG;
return addConstantMemberReference(tag, owner, name, descriptor);
}
/**
* Adds a CONSTANT_Fieldref_info, CONSTANT_Methodref_info or CONSTANT_InterfaceMethodref_info to
* the constant pool of this symbol table. Does nothing if the constant pool already contains a
* similar item.
*
* @param tag one of {@link Symbol#CONSTANT_FIELDREF_TAG}, {@link Symbol#CONSTANT_METHODREF_TAG}
* or {@link Symbol#CONSTANT_INTERFACE_METHODREF_TAG}.
* @param owner the internal name of a class.
* @param name a field or method name.
* @param descriptor a field or method descriptor.
* @return a new or already existing Symbol with the given value.
*/
private Entry addConstantMemberReference(
final int tag, final String owner, final String name, final String descriptor) {
int hashCode = hash(tag, owner, name, descriptor);
Entry entry = get(hashCode);
while (entry != null) {
if (entry.tag == tag
&& entry.hashCode == hashCode
&& entry.owner.equals(owner)
&& entry.name.equals(name)
&& entry.value.equals(descriptor)) {
return entry;
}
entry = entry.next;
}
constantPool.put122(
tag, addConstantClass(owner).index, addConstantNameAndType(name, descriptor));
return put(new Entry(constantPoolCount++, tag, owner, name, descriptor, 0, hashCode));
}
/**
* Adds a new CONSTANT_Fieldref_info, CONSTANT_Methodref_info or CONSTANT_InterfaceMethodref_info
* to the constant pool of this symbol table.
*
* @param index the constant pool index of the new Symbol.
* @param tag one of {@link Symbol#CONSTANT_FIELDREF_TAG}, {@link Symbol#CONSTANT_METHODREF_TAG}
* or {@link Symbol#CONSTANT_INTERFACE_METHODREF_TAG}.
* @param owner the internal name of a class.
* @param name a field or method name.
* @param descriptor a field or method descriptor.
*/
private void addConstantMemberReference(
final int index,
final int tag,
final String owner,
final String name,
final String descriptor) {
add(new Entry(index, tag, owner, name, descriptor, 0, hash(tag, owner, name, descriptor)));
}
/**
* Adds a CONSTANT_String_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param value a string.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantString(final String value) {
return addConstantUtf8Reference(Symbol.CONSTANT_STRING_TAG, value);
}
/**
* Adds a CONSTANT_Integer_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param value an int.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantInteger(final int value) {
return addConstantIntegerOrFloat(Symbol.CONSTANT_INTEGER_TAG, value);
}
/**
* Adds a CONSTANT_Float_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param value a float.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantFloat(final float value) {
return addConstantIntegerOrFloat(Symbol.CONSTANT_FLOAT_TAG, Float.floatToRawIntBits(value));
}
/**
* Adds a CONSTANT_Integer_info or CONSTANT_Float_info to the constant pool of this symbol table.
* Does nothing if the constant pool already contains a similar item.
*
* @param tag one of {@link Symbol#CONSTANT_INTEGER_TAG} or {@link Symbol#CONSTANT_FLOAT_TAG}.
* @param value an int or float.
* @return a constant pool constant with the given tag and primitive values.
*/
private Symbol addConstantIntegerOrFloat(final int tag, final int value) {
int hashCode = hash(tag, value);
Entry entry = get(hashCode);
while (entry != null) {
if (entry.tag == tag && entry.hashCode == hashCode && entry.data == value) {
return entry;
}
entry = entry.next;
}
constantPool.putByte(tag).putInt(value);
return put(new Entry(constantPoolCount++, tag, value, hashCode));
}
/**
* Adds a new CONSTANT_Integer_info or CONSTANT_Float_info to the constant pool of this symbol
* table.
*
* @param index the constant pool index of the new Symbol.
* @param tag one of {@link Symbol#CONSTANT_INTEGER_TAG} or {@link Symbol#CONSTANT_FLOAT_TAG}.
* @param value an int or float.
*/
private void addConstantIntegerOrFloat(final int index, final int tag, final int value) {
add(new Entry(index, tag, value, hash(tag, value)));
}
/**
* Adds a CONSTANT_Long_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param value a long.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantLong(final long value) {
return addConstantLongOrDouble(Symbol.CONSTANT_LONG_TAG, value);
}
/**
* Adds a CONSTANT_Double_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param value a double.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantDouble(final double value) {
return addConstantLongOrDouble(Symbol.CONSTANT_DOUBLE_TAG, Double.doubleToRawLongBits(value));
}
/**
* Adds a CONSTANT_Long_info or CONSTANT_Double_info to the constant pool of this symbol table.
* Does nothing if the constant pool already contains a similar item.
*
* @param tag one of {@link Symbol#CONSTANT_LONG_TAG} or {@link Symbol#CONSTANT_DOUBLE_TAG}.
* @param value a long or double.
* @return a constant pool constant with the given tag and primitive values.
*/
private Symbol addConstantLongOrDouble(final int tag, final long value) {
int hashCode = hash(tag, value);
Entry entry = get(hashCode);
while (entry != null) {
if (entry.tag == tag && entry.hashCode == hashCode && entry.data == value) {
return entry;
}
entry = entry.next;
}
int index = constantPoolCount;
constantPool.putByte(tag).putLong(value);
constantPoolCount += 2;
return put(new Entry(index, tag, value, hashCode));
}
/**
* Adds a new CONSTANT_Long_info or CONSTANT_Double_info to the constant pool of this symbol
* table.
*
* @param index the constant pool index of the new Symbol.
* @param tag one of {@link Symbol#CONSTANT_LONG_TAG} or {@link Symbol#CONSTANT_DOUBLE_TAG}.
* @param value a long or double.
*/
private void addConstantLongOrDouble(final int index, final int tag, final long value) {
add(new Entry(index, tag, value, hash(tag, value)));
}
/**
* Adds a CONSTANT_NameAndType_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param name a field or method name.
* @param descriptor a field or method descriptor.
* @return a new or already existing Symbol with the given value.
*/
int addConstantNameAndType(final String name, final String descriptor) {
final int tag = Symbol.CONSTANT_NAME_AND_TYPE_TAG;
int hashCode = hash(tag, name, descriptor);
Entry entry = get(hashCode);
while (entry != null) {
if (entry.tag == tag
&& entry.hashCode == hashCode
&& entry.name.equals(name)
&& entry.value.equals(descriptor)) {
return entry.index;
}
entry = entry.next;
}
constantPool.put122(tag, addConstantUtf8(name), addConstantUtf8(descriptor));
return put(new Entry(constantPoolCount++, tag, name, descriptor, hashCode)).index;
}
/**
* Adds a new CONSTANT_NameAndType_info to the constant pool of this symbol table.
*
* @param index the constant pool index of the new Symbol.
* @param name a field or method name.
* @param descriptor a field or method descriptor.
*/
private void addConstantNameAndType(final int index, final String name, final String descriptor) {
final int tag = Symbol.CONSTANT_NAME_AND_TYPE_TAG;
add(new Entry(index, tag, name, descriptor, hash(tag, name, descriptor)));
}
/**
* Adds a CONSTANT_Utf8_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param value a string.
* @return a new or already existing Symbol with the given value.
*/
int addConstantUtf8(final String value) {
int hashCode = hash(Symbol.CONSTANT_UTF8_TAG, value);
Entry entry = get(hashCode);
while (entry != null) {
if (entry.tag == Symbol.CONSTANT_UTF8_TAG
&& entry.hashCode == hashCode
&& entry.value.equals(value)) {
return entry.index;
}
entry = entry.next;
}
constantPool.putByte(Symbol.CONSTANT_UTF8_TAG).putUTF8(value);
return put(new Entry(constantPoolCount++, Symbol.CONSTANT_UTF8_TAG, value, hashCode)).index;
}
/**
* Adds a new CONSTANT_String_info to the constant pool of this symbol table.
*
* @param index the constant pool index of the new Symbol.
* @param value a string.
*/
private void addConstantUtf8(final int index, final String value) {
add(new Entry(index, Symbol.CONSTANT_UTF8_TAG, value, hash(Symbol.CONSTANT_UTF8_TAG, value)));
}
/**
* Adds a CONSTANT_MethodHandle_info to the constant pool of this symbol table. Does nothing if
* the constant pool already contains a similar item.
*
* @param referenceKind one of {@link Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC}, {@link
* Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC}, {@link Opcodes#H_INVOKEVIRTUAL}, {@link
* Opcodes#H_INVOKESTATIC}, {@link Opcodes#H_INVOKESPECIAL}, {@link
* Opcodes#H_NEWINVOKESPECIAL} or {@link Opcodes#H_INVOKEINTERFACE}.
* @param owner the internal name of a class of interface.
* @param name a field or method name.
* @param descriptor a field or method descriptor.
* @param isInterface whether owner is an interface or not.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantMethodHandle(
final int referenceKind,
final String owner,
final String name,
final String descriptor,
final boolean isInterface) {
final int tag = Symbol.CONSTANT_METHOD_HANDLE_TAG;
// Note that we don't need to include isInterface in the hash computation, because it is
// redundant with owner (we can't have the same owner with different isInterface values).
int hashCode = hash(tag, owner, name, descriptor, referenceKind);
Entry entry = get(hashCode);
while (entry != null) {
if (entry.tag == tag
&& entry.hashCode == hashCode
&& entry.data == referenceKind
&& entry.owner.equals(owner)
&& entry.name.equals(name)
&& entry.value.equals(descriptor)) {
return entry;
}
entry = entry.next;
}
if (referenceKind <= Opcodes.H_PUTSTATIC) {
constantPool.put112(tag, referenceKind, addConstantFieldref(owner, name, descriptor).index);
} else {
constantPool.put112(
tag, referenceKind, addConstantMethodref(owner, name, descriptor, isInterface).index);
}
return put(
new Entry(constantPoolCount++, tag, owner, name, descriptor, referenceKind, hashCode));
}
/**
* Adds a new CONSTANT_MethodHandle_info to the constant pool of this symbol table.
*
* @param index the constant pool index of the new Symbol.
* @param referenceKind one of {@link Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC}, {@link
* Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC}, {@link Opcodes#H_INVOKEVIRTUAL}, {@link
* Opcodes#H_INVOKESTATIC}, {@link Opcodes#H_INVOKESPECIAL}, {@link
* Opcodes#H_NEWINVOKESPECIAL} or {@link Opcodes#H_INVOKEINTERFACE}.
* @param owner the internal name of a class of interface.
* @param name a field or method name.
* @param descriptor a field or method descriptor.
*/
private void addConstantMethodHandle(
final int index,
final int referenceKind,
final String owner,
final String name,
final String descriptor) {
final int tag = Symbol.CONSTANT_METHOD_HANDLE_TAG;
int hashCode = hash(tag, owner, name, descriptor, referenceKind);
add(new Entry(index, tag, owner, name, descriptor, referenceKind, hashCode));
}
/**
* Adds a CONSTANT_MethodType_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param methodDescriptor a method descriptor.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantMethodType(final String methodDescriptor) {
return addConstantUtf8Reference(Symbol.CONSTANT_METHOD_TYPE_TAG, methodDescriptor);
}
/**
* Adds a CONSTANT_Dynamic_info to the constant pool of this symbol table. Also adds the related
* bootstrap method to the BootstrapMethods of this symbol table. Does nothing if the constant
* pool already contains a similar item.
*
* @param name a method name.
* @param descriptor a field descriptor.
* @param bootstrapMethodHandle a bootstrap method handle.
* @param bootstrapMethodArguments the bootstrap method arguments.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantDynamic(
final String name,
final String descriptor,
final Handle bootstrapMethodHandle,
final Object... bootstrapMethodArguments) {
Symbol bootstrapMethod = addBootstrapMethod(bootstrapMethodHandle, bootstrapMethodArguments);
return addConstantDynamicOrInvokeDynamicReference(
Symbol.CONSTANT_DYNAMIC_TAG, name, descriptor, bootstrapMethod.index);
}
/**
* Adds a CONSTANT_InvokeDynamic_info to the constant pool of this symbol table. Also adds the
* related bootstrap method to the BootstrapMethods of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param name a method name.
* @param descriptor a method descriptor.
* @param bootstrapMethodHandle a bootstrap method handle.
* @param bootstrapMethodArguments the bootstrap method arguments.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantInvokeDynamic(
final String name,
final String descriptor,
final Handle bootstrapMethodHandle,
final Object... bootstrapMethodArguments) {
Symbol bootstrapMethod = addBootstrapMethod(bootstrapMethodHandle, bootstrapMethodArguments);
return addConstantDynamicOrInvokeDynamicReference(
Symbol.CONSTANT_INVOKE_DYNAMIC_TAG, name, descriptor, bootstrapMethod.index);
}
/**
* Adds a CONSTANT_Dynamic or a CONSTANT_InvokeDynamic_info to the constant pool of this symbol
* table. Does nothing if the constant pool already contains a similar item.
*
* @param tag one of {@link Symbol#CONSTANT_DYNAMIC_TAG} or {@link
* Symbol#CONSTANT_INVOKE_DYNAMIC_TAG}.
* @param name a method name.
* @param descriptor a field descriptor for CONSTANT_DYNAMIC_TAG) or a method descriptor for
* CONSTANT_INVOKE_DYNAMIC_TAG.
* @param bootstrapMethodIndex the index of a bootstrap method in the BootstrapMethods attribute.
* @return a new or already existing Symbol with the given value.
*/
private Symbol addConstantDynamicOrInvokeDynamicReference(
final int tag, final String name, final String descriptor, final int bootstrapMethodIndex) {
int hashCode = hash(tag, name, descriptor, bootstrapMethodIndex);
Entry entry = get(hashCode);
while (entry != null) {
if (entry.tag == tag
&& entry.hashCode == hashCode
&& entry.data == bootstrapMethodIndex
&& entry.name.equals(name)
&& entry.value.equals(descriptor)) {
return entry;
}
entry = entry.next;
}
constantPool.put122(tag, bootstrapMethodIndex, addConstantNameAndType(name, descriptor));
return put(
new Entry(
constantPoolCount++, tag, null, name, descriptor, bootstrapMethodIndex, hashCode));
}
/**
* Adds a new CONSTANT_Dynamic_info or CONSTANT_InvokeDynamic_info to the constant pool of this
* symbol table.
*
* @param tag one of {@link Symbol#CONSTANT_DYNAMIC_TAG} or {@link
* Symbol#CONSTANT_INVOKE_DYNAMIC_TAG}.
* @param index the constant pool index of the new Symbol.
* @param name a method name.
* @param descriptor a field descriptor for CONSTANT_DYNAMIC_TAG or a method descriptor for
* CONSTANT_INVOKE_DYNAMIC_TAG.
* @param bootstrapMethodIndex the index of a bootstrap method in the BootstrapMethods attribute.
*/
private void addConstantDynamicOrInvokeDynamicReference(
final int tag,
final int index,
final String name,
final String descriptor,
final int bootstrapMethodIndex) {
int hashCode = hash(tag, name, descriptor, bootstrapMethodIndex);
add(new Entry(index, tag, null, name, descriptor, bootstrapMethodIndex, hashCode));
}
/**
* Adds a CONSTANT_Module_info to the constant pool of this symbol table. Does nothing if the
* constant pool already contains a similar item.
*
* @param moduleName a fully qualified name (using dots) of a module.
* @return a new or already existing Symbol with the given value.
*/
Symbol addConstantModule(final String moduleName) {
return addConstantUtf8Reference(Symbol.CONSTANT_MODULE_TAG, moduleName);
}
/**
* Adds a CONSTANT_Package_info to the constant pool of this symbol table. Does nothing if the