Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ConstantPool.java
41161 views
1
/*
2
* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package com.sun.tools.classfile;
27
28
import java.io.DataOutputStream;
29
import java.io.IOException;
30
import java.io.OutputStream;
31
import java.util.Iterator;
32
33
/**
34
* See JVMS, section 4.5.
35
*
36
* <p><b>This is NOT part of any supported API.
37
* If you write code that depends on this, you do so at your own risk.
38
* This code and its internal interfaces are subject to change or
39
* deletion without notice.</b>
40
*/
41
public class ConstantPool {
42
43
public static class InvalidIndex extends ConstantPoolException {
44
private static final long serialVersionUID = -4350294289300939730L;
45
InvalidIndex(int index) {
46
super(index);
47
}
48
49
@Override
50
public String getMessage() {
51
// i18n
52
return "invalid index #" + index;
53
}
54
}
55
56
public static class UnexpectedEntry extends ConstantPoolException {
57
private static final long serialVersionUID = 6986335935377933211L;
58
UnexpectedEntry(int index, int expected_tag, int found_tag) {
59
super(index);
60
this.expected_tag = expected_tag;
61
this.found_tag = found_tag;
62
}
63
64
@Override
65
public String getMessage() {
66
// i18n?
67
return "unexpected entry at #" + index + " -- expected tag " + expected_tag + ", found " + found_tag;
68
}
69
70
public final int expected_tag;
71
public final int found_tag;
72
}
73
74
public static class InvalidEntry extends ConstantPoolException {
75
private static final long serialVersionUID = 1000087545585204447L;
76
InvalidEntry(int index, int tag) {
77
super(index);
78
this.tag = tag;
79
}
80
81
@Override
82
public String getMessage() {
83
// i18n?
84
return "unexpected tag at #" + index + ": " + tag;
85
}
86
87
public final int tag;
88
}
89
90
public static class EntryNotFound extends ConstantPoolException {
91
private static final long serialVersionUID = 2885537606468581850L;
92
EntryNotFound(Object value) {
93
super(-1);
94
this.value = value;
95
}
96
97
@Override
98
public String getMessage() {
99
// i18n?
100
return "value not found: " + value;
101
}
102
103
public final Object value;
104
}
105
106
public static final int CONSTANT_Utf8 = 1;
107
public static final int CONSTANT_Integer = 3;
108
public static final int CONSTANT_Float = 4;
109
public static final int CONSTANT_Long = 5;
110
public static final int CONSTANT_Double = 6;
111
public static final int CONSTANT_Class = 7;
112
public static final int CONSTANT_String = 8;
113
public static final int CONSTANT_Fieldref = 9;
114
public static final int CONSTANT_Methodref = 10;
115
public static final int CONSTANT_InterfaceMethodref = 11;
116
public static final int CONSTANT_NameAndType = 12;
117
public static final int CONSTANT_MethodHandle = 15;
118
public static final int CONSTANT_MethodType = 16;
119
public static final int CONSTANT_Dynamic = 17;
120
public static final int CONSTANT_InvokeDynamic = 18;
121
public static final int CONSTANT_Module = 19;
122
public static final int CONSTANT_Package = 20;
123
124
public static enum RefKind {
125
REF_getField(1),
126
REF_getStatic(2),
127
REF_putField(3),
128
REF_putStatic(4),
129
REF_invokeVirtual(5),
130
REF_invokeStatic(6),
131
REF_invokeSpecial(7),
132
REF_newInvokeSpecial(8),
133
REF_invokeInterface(9);
134
135
public final int tag;
136
137
RefKind(int tag) {
138
this.tag = tag;
139
}
140
141
static RefKind getRefkind(int tag) {
142
switch(tag) {
143
case 1:
144
return REF_getField;
145
case 2:
146
return REF_getStatic;
147
case 3:
148
return REF_putField;
149
case 4:
150
return REF_putStatic;
151
case 5:
152
return REF_invokeVirtual;
153
case 6:
154
return REF_invokeStatic;
155
case 7:
156
return REF_invokeSpecial;
157
case 8:
158
return REF_newInvokeSpecial;
159
case 9:
160
return REF_invokeInterface;
161
default:
162
return null;
163
}
164
}
165
}
166
167
ConstantPool(ClassReader cr) throws IOException, InvalidEntry {
168
int count = cr.readUnsignedShort();
169
pool = new CPInfo[count];
170
for (int i = 1; i < count; i++) {
171
int tag = cr.readUnsignedByte();
172
switch (tag) {
173
case CONSTANT_Class:
174
pool[i] = new CONSTANT_Class_info(this, cr);
175
break;
176
177
case CONSTANT_Double:
178
pool[i] = new CONSTANT_Double_info(cr);
179
i++;
180
break;
181
182
case CONSTANT_Fieldref:
183
pool[i] = new CONSTANT_Fieldref_info(this, cr);
184
break;
185
186
case CONSTANT_Float:
187
pool[i] = new CONSTANT_Float_info(cr);
188
break;
189
190
case CONSTANT_Integer:
191
pool[i] = new CONSTANT_Integer_info(cr);
192
break;
193
194
case CONSTANT_InterfaceMethodref:
195
pool[i] = new CONSTANT_InterfaceMethodref_info(this, cr);
196
break;
197
198
case CONSTANT_InvokeDynamic:
199
pool[i] = new CONSTANT_InvokeDynamic_info(this, cr);
200
break;
201
202
case CONSTANT_Dynamic:
203
pool[i] = new CONSTANT_Dynamic_info(this, cr);
204
break;
205
206
case CONSTANT_Long:
207
pool[i] = new CONSTANT_Long_info(cr);
208
i++;
209
break;
210
211
case CONSTANT_MethodHandle:
212
pool[i] = new CONSTANT_MethodHandle_info(this, cr);
213
break;
214
215
case CONSTANT_MethodType:
216
pool[i] = new CONSTANT_MethodType_info(this, cr);
217
break;
218
219
case CONSTANT_Methodref:
220
pool[i] = new CONSTANT_Methodref_info(this, cr);
221
break;
222
223
case CONSTANT_Module:
224
pool[i] = new CONSTANT_Module_info(this, cr);
225
break;
226
227
case CONSTANT_NameAndType:
228
pool[i] = new CONSTANT_NameAndType_info(this, cr);
229
break;
230
231
case CONSTANT_Package:
232
pool[i] = new CONSTANT_Package_info(this, cr);
233
break;
234
235
case CONSTANT_String:
236
pool[i] = new CONSTANT_String_info(this, cr);
237
break;
238
239
case CONSTANT_Utf8:
240
pool[i] = new CONSTANT_Utf8_info(cr);
241
break;
242
243
default:
244
throw new InvalidEntry(i, tag);
245
}
246
}
247
}
248
249
public ConstantPool(CPInfo[] pool) {
250
this.pool = pool;
251
}
252
253
public int size() {
254
return pool.length;
255
}
256
257
public int byteLength() {
258
int length = 2;
259
for (int i = 1; i < size(); ) {
260
CPInfo cpInfo = pool[i];
261
length += cpInfo.byteLength();
262
i += cpInfo.size();
263
}
264
return length;
265
}
266
267
public CPInfo get(int index) throws InvalidIndex {
268
if (index <= 0 || index >= pool.length)
269
throw new InvalidIndex(index);
270
CPInfo info = pool[index];
271
if (info == null) {
272
// this occurs for indices referencing the "second half" of an
273
// 8 byte constant, such as CONSTANT_Double or CONSTANT_Long
274
throw new InvalidIndex(index);
275
}
276
return pool[index];
277
}
278
279
private CPInfo get(int index, int expected_type) throws InvalidIndex, UnexpectedEntry {
280
CPInfo info = get(index);
281
if (info.getTag() != expected_type)
282
throw new UnexpectedEntry(index, expected_type, info.getTag());
283
return info;
284
}
285
286
public CONSTANT_Utf8_info getUTF8Info(int index) throws InvalidIndex, UnexpectedEntry {
287
return ((CONSTANT_Utf8_info) get(index, CONSTANT_Utf8));
288
}
289
290
public CONSTANT_Class_info getClassInfo(int index) throws InvalidIndex, UnexpectedEntry {
291
return ((CONSTANT_Class_info) get(index, CONSTANT_Class));
292
}
293
294
public CONSTANT_Module_info getModuleInfo(int index) throws InvalidIndex, UnexpectedEntry {
295
return ((CONSTANT_Module_info) get(index, CONSTANT_Module));
296
}
297
298
public CONSTANT_NameAndType_info getNameAndTypeInfo(int index) throws InvalidIndex, UnexpectedEntry {
299
return ((CONSTANT_NameAndType_info) get(index, CONSTANT_NameAndType));
300
}
301
302
public CONSTANT_Package_info getPackageInfo(int index) throws InvalidIndex, UnexpectedEntry {
303
return ((CONSTANT_Package_info) get(index, CONSTANT_Package));
304
}
305
306
public String getUTF8Value(int index) throws InvalidIndex, UnexpectedEntry {
307
return getUTF8Info(index).value;
308
}
309
310
public int getUTF8Index(String value) throws EntryNotFound {
311
for (int i = 1; i < pool.length; i++) {
312
CPInfo info = pool[i];
313
if (info instanceof CONSTANT_Utf8_info &&
314
((CONSTANT_Utf8_info) info).value.equals(value))
315
return i;
316
}
317
throw new EntryNotFound(value);
318
}
319
320
public Iterable<CPInfo> entries() {
321
return () -> new Iterator<CPInfo>() {
322
323
public boolean hasNext() {
324
return next < pool.length;
325
}
326
327
public CPInfo next() {
328
current = pool[next];
329
switch (current.getTag()) {
330
case CONSTANT_Double:
331
case CONSTANT_Long:
332
next += 2;
333
break;
334
default:
335
next += 1;
336
}
337
return current;
338
}
339
340
public void remove() {
341
throw new UnsupportedOperationException();
342
}
343
344
private CPInfo current;
345
private int next = 1;
346
347
};
348
}
349
350
private CPInfo[] pool;
351
352
public interface Visitor<R,P> {
353
R visitClass(CONSTANT_Class_info info, P p);
354
R visitDouble(CONSTANT_Double_info info, P p);
355
R visitFieldref(CONSTANT_Fieldref_info info, P p);
356
R visitFloat(CONSTANT_Float_info info, P p);
357
R visitInteger(CONSTANT_Integer_info info, P p);
358
R visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, P p);
359
R visitInvokeDynamic(CONSTANT_InvokeDynamic_info info, P p);
360
R visitDynamicConstant(CONSTANT_Dynamic_info info, P p);
361
R visitLong(CONSTANT_Long_info info, P p);
362
R visitMethodref(CONSTANT_Methodref_info info, P p);
363
R visitMethodHandle(CONSTANT_MethodHandle_info info, P p);
364
R visitMethodType(CONSTANT_MethodType_info info, P p);
365
R visitModule(CONSTANT_Module_info info, P p);
366
R visitNameAndType(CONSTANT_NameAndType_info info, P p);
367
R visitPackage(CONSTANT_Package_info info, P p);
368
R visitString(CONSTANT_String_info info, P p);
369
R visitUtf8(CONSTANT_Utf8_info info, P p);
370
}
371
372
public static abstract class CPInfo {
373
CPInfo() {
374
this.cp = null;
375
}
376
377
CPInfo(ConstantPool cp) {
378
this.cp = cp;
379
}
380
381
public abstract int getTag();
382
383
/** The number of slots in the constant pool used by this entry.
384
* 2 for CONSTANT_Double and CONSTANT_Long; 1 for everything else. */
385
public int size() {
386
return 1;
387
}
388
389
public abstract int byteLength();
390
391
public abstract <R,D> R accept(Visitor<R,D> visitor, D data);
392
393
protected final ConstantPool cp;
394
}
395
396
public static abstract class CPRefInfo extends CPInfo {
397
protected CPRefInfo(ConstantPool cp, ClassReader cr, int tag) throws IOException {
398
super(cp);
399
this.tag = tag;
400
class_index = cr.readUnsignedShort();
401
name_and_type_index = cr.readUnsignedShort();
402
}
403
404
protected CPRefInfo(ConstantPool cp, int tag, int class_index, int name_and_type_index) {
405
super(cp);
406
this.tag = tag;
407
this.class_index = class_index;
408
this.name_and_type_index = name_and_type_index;
409
}
410
411
public int getTag() {
412
return tag;
413
}
414
415
public int byteLength() {
416
return 5;
417
}
418
419
public CONSTANT_Class_info getClassInfo() throws ConstantPoolException {
420
return cp.getClassInfo(class_index);
421
}
422
423
public String getClassName() throws ConstantPoolException {
424
return cp.getClassInfo(class_index).getName();
425
}
426
427
public CONSTANT_NameAndType_info getNameAndTypeInfo() throws ConstantPoolException {
428
return cp.getNameAndTypeInfo(name_and_type_index);
429
}
430
431
public final int tag;
432
public final int class_index;
433
public final int name_and_type_index;
434
}
435
436
public static class CONSTANT_Class_info extends CPInfo {
437
CONSTANT_Class_info(ConstantPool cp, ClassReader cr) throws IOException {
438
super(cp);
439
name_index = cr.readUnsignedShort();
440
}
441
442
public CONSTANT_Class_info(ConstantPool cp, int name_index) {
443
super(cp);
444
this.name_index = name_index;
445
}
446
447
public int getTag() {
448
return CONSTANT_Class;
449
}
450
451
public int byteLength() {
452
return 3;
453
}
454
455
/**
456
* Get the raw value of the class referenced by this constant pool entry.
457
* This will either be the name of the class, in internal form, or a
458
* descriptor for an array class.
459
* @return the raw value of the class
460
*/
461
public String getName() throws ConstantPoolException {
462
return cp.getUTF8Value(name_index);
463
}
464
465
/**
466
* If this constant pool entry identifies either a class or interface type,
467
* or a possibly multi-dimensional array of a class of interface type,
468
* return the name of the class or interface in internal form. Otherwise,
469
* (i.e. if this is a possibly multi-dimensional array of a primitive type),
470
* return null.
471
* @return the base class or interface name
472
*/
473
public String getBaseName() throws ConstantPoolException {
474
String name = getName();
475
if (name.startsWith("[")) {
476
int index = name.indexOf("[L");
477
if (index == -1)
478
return null;
479
return name.substring(index + 2, name.length() - 1);
480
} else
481
return name;
482
}
483
484
public int getDimensionCount() throws ConstantPoolException {
485
String name = getName();
486
int count = 0;
487
while (name.charAt(count) == '[')
488
count++;
489
return count;
490
}
491
492
@Override
493
public String toString() {
494
return "CONSTANT_Class_info[name_index: " + name_index + "]";
495
}
496
497
public <R, D> R accept(Visitor<R, D> visitor, D data) {
498
return visitor.visitClass(this, data);
499
}
500
501
public final int name_index;
502
}
503
504
public static class CONSTANT_Double_info extends CPInfo {
505
CONSTANT_Double_info(ClassReader cr) throws IOException {
506
value = cr.readDouble();
507
}
508
509
public CONSTANT_Double_info(double value) {
510
this.value = value;
511
}
512
513
public int getTag() {
514
return CONSTANT_Double;
515
}
516
517
public int byteLength() {
518
return 9;
519
}
520
521
@Override
522
public int size() {
523
return 2;
524
}
525
526
@Override
527
public String toString() {
528
return "CONSTANT_Double_info[value: " + value + "]";
529
}
530
531
public <R, D> R accept(Visitor<R, D> visitor, D data) {
532
return visitor.visitDouble(this, data);
533
}
534
535
public final double value;
536
}
537
538
public static class CONSTANT_Fieldref_info extends CPRefInfo {
539
CONSTANT_Fieldref_info(ConstantPool cp, ClassReader cr) throws IOException {
540
super(cp, cr, CONSTANT_Fieldref);
541
}
542
543
public CONSTANT_Fieldref_info(ConstantPool cp, int class_index, int name_and_type_index) {
544
super(cp, CONSTANT_Fieldref, class_index, name_and_type_index);
545
}
546
547
@Override
548
public String toString() {
549
return "CONSTANT_Fieldref_info[class_index: " + class_index + ", name_and_type_index: " + name_and_type_index + "]";
550
}
551
552
public <R, D> R accept(Visitor<R, D> visitor, D data) {
553
return visitor.visitFieldref(this, data);
554
}
555
}
556
557
public static class CONSTANT_Float_info extends CPInfo {
558
CONSTANT_Float_info(ClassReader cr) throws IOException {
559
value = cr.readFloat();
560
}
561
562
public CONSTANT_Float_info(float value) {
563
this.value = value;
564
}
565
566
public int getTag() {
567
return CONSTANT_Float;
568
}
569
570
public int byteLength() {
571
return 5;
572
}
573
574
@Override
575
public String toString() {
576
return "CONSTANT_Float_info[value: " + value + "]";
577
}
578
579
public <R, D> R accept(Visitor<R, D> visitor, D data) {
580
return visitor.visitFloat(this, data);
581
}
582
583
public final float value;
584
}
585
586
public static class CONSTANT_Integer_info extends CPInfo {
587
CONSTANT_Integer_info(ClassReader cr) throws IOException {
588
value = cr.readInt();
589
}
590
591
public CONSTANT_Integer_info(int value) {
592
this.value = value;
593
}
594
595
public int getTag() {
596
return CONSTANT_Integer;
597
}
598
599
public int byteLength() {
600
return 5;
601
}
602
603
@Override
604
public String toString() {
605
return "CONSTANT_Integer_info[value: " + value + "]";
606
}
607
608
public <R, D> R accept(Visitor<R, D> visitor, D data) {
609
return visitor.visitInteger(this, data);
610
}
611
612
public final int value;
613
}
614
615
public static class CONSTANT_InterfaceMethodref_info extends CPRefInfo {
616
CONSTANT_InterfaceMethodref_info(ConstantPool cp, ClassReader cr) throws IOException {
617
super(cp, cr, CONSTANT_InterfaceMethodref);
618
}
619
620
public CONSTANT_InterfaceMethodref_info(ConstantPool cp, int class_index, int name_and_type_index) {
621
super(cp, CONSTANT_InterfaceMethodref, class_index, name_and_type_index);
622
}
623
624
@Override
625
public String toString() {
626
return "CONSTANT_InterfaceMethodref_info[class_index: " + class_index + ", name_and_type_index: " + name_and_type_index + "]";
627
}
628
629
public <R, D> R accept(Visitor<R, D> visitor, D data) {
630
return visitor.visitInterfaceMethodref(this, data);
631
}
632
}
633
634
public static class CONSTANT_InvokeDynamic_info extends CPInfo {
635
CONSTANT_InvokeDynamic_info(ConstantPool cp, ClassReader cr) throws IOException {
636
super(cp);
637
bootstrap_method_attr_index = cr.readUnsignedShort();
638
name_and_type_index = cr.readUnsignedShort();
639
}
640
641
public CONSTANT_InvokeDynamic_info(ConstantPool cp, int bootstrap_method_index, int name_and_type_index) {
642
super(cp);
643
this.bootstrap_method_attr_index = bootstrap_method_index;
644
this.name_and_type_index = name_and_type_index;
645
}
646
647
public int getTag() {
648
return CONSTANT_InvokeDynamic;
649
}
650
651
public int byteLength() {
652
return 5;
653
}
654
655
@Override
656
public String toString() {
657
return "CONSTANT_InvokeDynamic_info[bootstrap_method_index: " + bootstrap_method_attr_index + ", name_and_type_index: " + name_and_type_index + "]";
658
}
659
660
public <R, D> R accept(Visitor<R, D> visitor, D data) {
661
return visitor.visitInvokeDynamic(this, data);
662
}
663
664
public CONSTANT_NameAndType_info getNameAndTypeInfo() throws ConstantPoolException {
665
return cp.getNameAndTypeInfo(name_and_type_index);
666
}
667
668
public final int bootstrap_method_attr_index;
669
public final int name_and_type_index;
670
}
671
672
public static class CONSTANT_Long_info extends CPInfo {
673
CONSTANT_Long_info(ClassReader cr) throws IOException {
674
value = cr.readLong();
675
}
676
677
public CONSTANT_Long_info(long value) {
678
this.value = value;
679
}
680
681
public int getTag() {
682
return CONSTANT_Long;
683
}
684
685
@Override
686
public int size() {
687
return 2;
688
}
689
690
public int byteLength() {
691
return 9;
692
}
693
694
@Override
695
public String toString() {
696
return "CONSTANT_Long_info[value: " + value + "]";
697
}
698
699
public <R, D> R accept(Visitor<R, D> visitor, D data) {
700
return visitor.visitLong(this, data);
701
}
702
703
public final long value;
704
}
705
706
public static class CONSTANT_MethodHandle_info extends CPInfo {
707
CONSTANT_MethodHandle_info(ConstantPool cp, ClassReader cr) throws IOException {
708
super(cp);
709
reference_kind = RefKind.getRefkind(cr.readUnsignedByte());
710
reference_index = cr.readUnsignedShort();
711
}
712
713
public CONSTANT_MethodHandle_info(ConstantPool cp, RefKind ref_kind, int member_index) {
714
super(cp);
715
this.reference_kind = ref_kind;
716
this.reference_index = member_index;
717
}
718
719
public int getTag() {
720
return CONSTANT_MethodHandle;
721
}
722
723
public int byteLength() {
724
return 4;
725
}
726
727
@Override
728
public String toString() {
729
return "CONSTANT_MethodHandle_info[ref_kind: " + reference_kind + ", member_index: " + reference_index + "]";
730
}
731
732
public <R, D> R accept(Visitor<R, D> visitor, D data) {
733
return visitor.visitMethodHandle(this, data);
734
}
735
736
public CPRefInfo getCPRefInfo() throws ConstantPoolException {
737
int expected = CONSTANT_Methodref;
738
int actual = cp.get(reference_index).getTag();
739
// allow these tag types also:
740
switch (actual) {
741
case CONSTANT_Fieldref:
742
case CONSTANT_InterfaceMethodref:
743
expected = actual;
744
}
745
return (CPRefInfo) cp.get(reference_index, expected);
746
}
747
748
public final RefKind reference_kind;
749
public final int reference_index;
750
}
751
752
public static class CONSTANT_MethodType_info extends CPInfo {
753
CONSTANT_MethodType_info(ConstantPool cp, ClassReader cr) throws IOException {
754
super(cp);
755
descriptor_index = cr.readUnsignedShort();
756
}
757
758
public CONSTANT_MethodType_info(ConstantPool cp, int signature_index) {
759
super(cp);
760
this.descriptor_index = signature_index;
761
}
762
763
public int getTag() {
764
return CONSTANT_MethodType;
765
}
766
767
public int byteLength() {
768
return 3;
769
}
770
771
@Override
772
public String toString() {
773
return "CONSTANT_MethodType_info[signature_index: " + descriptor_index + "]";
774
}
775
776
public <R, D> R accept(Visitor<R, D> visitor, D data) {
777
return visitor.visitMethodType(this, data);
778
}
779
780
public String getType() throws ConstantPoolException {
781
return cp.getUTF8Value(descriptor_index);
782
}
783
784
public final int descriptor_index;
785
}
786
787
public static class CONSTANT_Methodref_info extends CPRefInfo {
788
CONSTANT_Methodref_info(ConstantPool cp, ClassReader cr) throws IOException {
789
super(cp, cr, CONSTANT_Methodref);
790
}
791
792
public CONSTANT_Methodref_info(ConstantPool cp, int class_index, int name_and_type_index) {
793
super(cp, CONSTANT_Methodref, class_index, name_and_type_index);
794
}
795
796
@Override
797
public String toString() {
798
return "CONSTANT_Methodref_info[class_index: " + class_index + ", name_and_type_index: " + name_and_type_index + "]";
799
}
800
801
public <R, D> R accept(Visitor<R, D> visitor, D data) {
802
return visitor.visitMethodref(this, data);
803
}
804
}
805
806
public static class CONSTANT_Module_info extends CPInfo {
807
CONSTANT_Module_info(ConstantPool cp, ClassReader cr) throws IOException {
808
super(cp);
809
name_index = cr.readUnsignedShort();
810
}
811
812
public CONSTANT_Module_info(ConstantPool cp, int name_index) {
813
super(cp);
814
this.name_index = name_index;
815
}
816
817
public int getTag() {
818
return CONSTANT_Module;
819
}
820
821
public int byteLength() {
822
return 3;
823
}
824
825
/**
826
* Get the raw value of the module name referenced by this constant pool entry.
827
* This will be the name of the module.
828
* @return the raw value of the module name
829
*/
830
public String getName() throws ConstantPoolException {
831
return cp.getUTF8Value(name_index);
832
}
833
834
@Override
835
public String toString() {
836
return "CONSTANT_Module_info[name_index: " + name_index + "]";
837
}
838
839
public <R, D> R accept(Visitor<R, D> visitor, D data) {
840
return visitor.visitModule(this, data);
841
}
842
843
public final int name_index;
844
}
845
846
public static class CONSTANT_NameAndType_info extends CPInfo {
847
CONSTANT_NameAndType_info(ConstantPool cp, ClassReader cr) throws IOException {
848
super(cp);
849
name_index = cr.readUnsignedShort();
850
type_index = cr.readUnsignedShort();
851
}
852
853
public CONSTANT_NameAndType_info(ConstantPool cp, int name_index, int type_index) {
854
super(cp);
855
this.name_index = name_index;
856
this.type_index = type_index;
857
}
858
859
public int getTag() {
860
return CONSTANT_NameAndType;
861
}
862
863
public int byteLength() {
864
return 5;
865
}
866
867
public String getName() throws ConstantPoolException {
868
return cp.getUTF8Value(name_index);
869
}
870
871
public String getType() throws ConstantPoolException {
872
return cp.getUTF8Value(type_index);
873
}
874
875
public <R, D> R accept(Visitor<R, D> visitor, D data) {
876
return visitor.visitNameAndType(this, data);
877
}
878
879
@Override
880
public String toString() {
881
return "CONSTANT_NameAndType_info[name_index: " + name_index + ", type_index: " + type_index + "]";
882
}
883
884
public final int name_index;
885
public final int type_index;
886
}
887
888
public static class CONSTANT_Dynamic_info extends CPInfo {
889
CONSTANT_Dynamic_info(ConstantPool cp, ClassReader cr) throws IOException {
890
super(cp);
891
bootstrap_method_attr_index = cr.readUnsignedShort();
892
name_and_type_index = cr.readUnsignedShort();
893
}
894
895
public CONSTANT_Dynamic_info(ConstantPool cp, int bootstrap_method_index, int name_and_type_index) {
896
super(cp);
897
this.bootstrap_method_attr_index = bootstrap_method_index;
898
this.name_and_type_index = name_and_type_index;
899
}
900
901
public int getTag() {
902
return CONSTANT_Dynamic;
903
}
904
905
public int byteLength() {
906
return 5;
907
}
908
909
@Override
910
public String toString() {
911
return "CONSTANT_Dynamic_info[bootstrap_method_index: " + bootstrap_method_attr_index + ", name_and_type_index: " + name_and_type_index + "]";
912
}
913
914
public <R, D> R accept(Visitor<R, D> visitor, D data) {
915
return visitor.visitDynamicConstant(this, data);
916
}
917
918
public CONSTANT_NameAndType_info getNameAndTypeInfo() throws ConstantPoolException {
919
return cp.getNameAndTypeInfo(name_and_type_index);
920
}
921
922
public final int bootstrap_method_attr_index;
923
public final int name_and_type_index;
924
}
925
926
public static class CONSTANT_Package_info extends CPInfo {
927
CONSTANT_Package_info(ConstantPool cp, ClassReader cr) throws IOException {
928
super(cp);
929
name_index = cr.readUnsignedShort();
930
}
931
932
public CONSTANT_Package_info(ConstantPool cp, int name_index) {
933
super(cp);
934
this.name_index = name_index;
935
}
936
937
public int getTag() {
938
return CONSTANT_Package;
939
}
940
941
public int byteLength() {
942
return 3;
943
}
944
945
/**
946
* Get the raw value of the package name referenced by this constant pool entry.
947
* This will be the name of the package, in internal form.
948
* @return the raw value of the module name
949
*/
950
public String getName() throws ConstantPoolException {
951
return cp.getUTF8Value(name_index);
952
}
953
954
@Override
955
public String toString() {
956
return "CONSTANT_Package_info[name_index: " + name_index + "]";
957
}
958
959
public <R, D> R accept(Visitor<R, D> visitor, D data) {
960
return visitor.visitPackage(this, data);
961
}
962
963
public final int name_index;
964
}
965
966
public static class CONSTANT_String_info extends CPInfo {
967
CONSTANT_String_info(ConstantPool cp, ClassReader cr) throws IOException {
968
super(cp);
969
string_index = cr.readUnsignedShort();
970
}
971
972
public CONSTANT_String_info(ConstantPool cp, int string_index) {
973
super(cp);
974
this.string_index = string_index;
975
}
976
977
public int getTag() {
978
return CONSTANT_String;
979
}
980
981
public int byteLength() {
982
return 3;
983
}
984
985
public String getString() throws ConstantPoolException {
986
return cp.getUTF8Value(string_index);
987
}
988
989
public <R, D> R accept(Visitor<R, D> visitor, D data) {
990
return visitor.visitString(this, data);
991
}
992
993
@Override
994
public String toString() {
995
return "CONSTANT_String_info[class_index: " + string_index + "]";
996
}
997
998
public final int string_index;
999
}
1000
1001
public static class CONSTANT_Utf8_info extends CPInfo {
1002
CONSTANT_Utf8_info(ClassReader cr) throws IOException {
1003
value = cr.readUTF();
1004
}
1005
1006
public CONSTANT_Utf8_info(String value) {
1007
this.value = value;
1008
}
1009
1010
public int getTag() {
1011
return CONSTANT_Utf8;
1012
}
1013
1014
public int byteLength() {
1015
class SizeOutputStream extends OutputStream {
1016
@Override
1017
public void write(int b) {
1018
size++;
1019
}
1020
int size;
1021
}
1022
SizeOutputStream sizeOut = new SizeOutputStream();
1023
DataOutputStream out = new DataOutputStream(sizeOut);
1024
try { out.writeUTF(value); } catch (IOException ignore) { }
1025
return 1 + sizeOut.size;
1026
}
1027
1028
@Override
1029
public String toString() {
1030
if (value.length() < 32 && isPrintableAscii(value))
1031
return "CONSTANT_Utf8_info[value: \"" + value + "\"]";
1032
else
1033
return "CONSTANT_Utf8_info[value: (" + value.length() + " chars)]";
1034
}
1035
1036
static boolean isPrintableAscii(String s) {
1037
for (int i = 0; i < s.length(); i++) {
1038
char c = s.charAt(i);
1039
if (c < 32 || c >= 127)
1040
return false;
1041
}
1042
return true;
1043
}
1044
1045
public <R, D> R accept(Visitor<R, D> visitor, D data) {
1046
return visitor.visitUtf8(this, data);
1047
}
1048
1049
public final String value;
1050
}
1051
1052
}
1053
1054