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
|
// $Id$
package ch.epfl.lamp.fjbg;
import java.util.*;
import java.io.*;
/**
* Constant pool, holding constants for a Java class file.
*
* @author Michel Schinz
* @version 2.0
*/
public class JConstantPool {
protected boolean frozen = false;
protected HashMap/*<Entry,Integer>*/ entryToIndex = new HashMap();
protected Entry[] indexToEntry;
protected int currIndex;
public static final short CONSTANT_Utf8 = 1;
public static final short CONSTANT_Integer = 3;
public static final short CONSTANT_Float = 4;
public static final short CONSTANT_Long = 5;
public static final short CONSTANT_Double = 6;
public static final short CONSTANT_Class = 7;
public static final short CONSTANT_String = 8;
public static final short CONSTANT_Fieldref = 9;
public static final short CONSTANT_Methodref = 10;
public static final short CONSTANT_InterfaceMethodref = 11;
public static final short CONSTANT_NameAndType = 12;
protected JConstantPool(FJBGContext context) {
indexToEntry = new Entry[8];
currIndex = 1;
}
protected JConstantPool(FJBGContext context, DataInputStream stream)
throws IOException {
int count = stream.readShort();
indexToEntry = new EntryIndex[count];
currIndex = 1;
while (currIndex < count) {
EntryIndex e;
int tag = stream.readByte();
switch (tag) {
case CONSTANT_Utf8:
e = new Utf8Entry(stream);
break;
case CONSTANT_Integer:
e = new IntegerEntry(stream);
break;
case CONSTANT_Float:
e = new FloatEntry(stream);
break;
case CONSTANT_Long:
e = new LongEntry(stream);
break;
case CONSTANT_Double:
e = new DoubleEntry(stream);
break;
case CONSTANT_Class:
e = new DescriptorEntryIndex(stream);
break;
case CONSTANT_String:
e = new StringEntryIndex(stream);
break;
case CONSTANT_Fieldref:
case CONSTANT_Methodref:
case CONSTANT_InterfaceMethodref:
e = new FieldOrMethodRefEntryIndex(tag, stream);
break;
case CONSTANT_NameAndType:
e = new NameAndTypeEntryIndex(stream);
break;
default:
throw new IllegalArgumentException("unknown entry in pool: " + tag);
}
indexToEntry[currIndex] = e;
currIndex += e.getSize();
}
}
public void freeze() { frozen = true; }
/**
* Returns a string representing the type of an entry
* knowing its tag
* @param tag The tag representing the type of the
* constant pool entry
*/
public String getEntryType(int tag) {
switch (tag) {
case 4 : return "Utf8";
case 5 : return "Integer";
case 6 : return "Float";
case 7 : return "Long";
case 8 : return "Double";
case 9 : return "Class";
case 10 : return "String";
case 11 : return "Fieldref";
case 12 : return "Methodref";
case 13 : return "InterfaceMethodref";
case 14 : return "NameAndType";
default : throw new Error("invalid constant pool tag : " + tag);
}
}
public int addClass(String className) {
return addDescriptor(className.replace('.', '/'));
}
public String lookupClass(int index) {
DescriptorEntry entry = (DescriptorEntry)lookupEntry(index);
return entry.getValue().replace('/', '.');
}
public int addDescriptor(JReferenceType type) {
return addDescriptor(type.getDescriptor());
}
protected int addDescriptor(String name) {
return addEntry(new DescriptorEntryValue(name));
}
public int addClassMethodRef(String className,
String methodName,
String signature) {
return addMethodRef(true, className, methodName, signature);
}
public int addInterfaceMethodRef(String className,
String methodName,
String signature) {
return addMethodRef(false, className, methodName, signature);
}
public int addMethodRef(boolean isClass,
String className,
String methodName,
String signature) {
return addEntry(new FieldOrMethodRefEntryValue(isClass
? CONSTANT_Methodref
: CONSTANT_InterfaceMethodref,
className,
methodName,
signature));
}
public int addFieldRef(String className,
String fieldName,
String signature) {
return addEntry(new FieldOrMethodRefEntryValue(CONSTANT_Fieldref,
className,
fieldName,
signature));
}
public int addInteger(int value) {
return addEntry(new IntegerEntry(value));
}
public int addFloat(float value) {
return addEntry(new FloatEntry(value));
}
public int addLong(long value) {
return addEntry(new LongEntry(value));
}
public int addDouble(double value) {
return addEntry(new DoubleEntry(value));
}
public int addString(String value) {
return addEntry(new StringEntryValue(value));
}
public int addNameAndType(String name, String descriptor) {
return addEntry(new NameAndTypeEntryValue(name, descriptor));
}
public int addUtf8(String value) {
return addEntry(new Utf8Entry(value));
}
public int addUtf8(byte[] value) {
return addEntry(new Utf8Entry(value));
}
public String lookupUtf8(int index) {
Utf8Entry entry = (Utf8Entry)lookupEntry(index);
return entry.getValue();
}
protected int addEntry(EntryValue e) {
assert !frozen;
Integer idx = (Integer)entryToIndex.get(e);
if (idx != null)
return idx.intValue();
e.addChildren();
int index = currIndex;
currIndex += e.getSize();
entryToIndex.put(e, new Integer(index));
if (index >= indexToEntry.length) {
Entry[] newI2E = new Entry[indexToEntry.length * 2];
System.arraycopy(indexToEntry, 0, newI2E, 0, indexToEntry.length);
indexToEntry = newI2E;
}
indexToEntry[index] = e;
return index;
}
public Entry lookupEntry(int index) {
assert index > 0 && index < currIndex
: "invalid index: " + index;
assert indexToEntry[index] != null
: "invalid index (null contents): " + index;
return indexToEntry[index];
}
public void writeTo(DataOutputStream stream) throws IOException {
if (! frozen) freeze();
stream.writeShort(currIndex);
for (int i = 0; i < currIndex; ++i) {
Entry entry = indexToEntry[i];
if (entry != null) {
stream.writeByte(entry.getTag());
entry.writeContentsTo(stream);
}
}
}
/// Classes for the various kinds of entries
//////////////////////////////////////////////////////////////////////
public interface Entry {
public int getTag();
int getSize();
void writeContentsTo(DataOutputStream stream) throws IOException;
}
protected interface EntryValue extends Entry {
abstract void addChildren();
}
protected interface EntryIndex extends Entry {
abstract void fetchChildren();
}
abstract protected class ChildlessEntry implements EntryValue, EntryIndex {
public void addChildren() {}
public void fetchChildren() {}
}
public class IntegerEntry extends ChildlessEntry implements Entry {
private final int value;
public IntegerEntry(int value) { this.value = value; }
public IntegerEntry(DataInputStream stream) throws IOException {
this(stream.readInt());
}
public int hashCode() { return value; }
public boolean equals(Object o) {
return o instanceof IntegerEntry && ((IntegerEntry)o).value == value;
}
public int getTag() { return CONSTANT_Integer; }
public int getValue() { return value; }
public int getSize() { return 1; }
public void writeContentsTo(DataOutputStream stream) throws IOException {
stream.writeInt(value);
}
}
public class FloatEntry extends ChildlessEntry implements Entry {
private final float value;
public FloatEntry(float value) { this.value = value; }
public FloatEntry(DataInputStream stream) throws IOException {
this(stream.readFloat());
}
public int hashCode() { return (int)value; }
public boolean equals(Object o) {
return o instanceof FloatEntry && ((FloatEntry)o).value == value;
}
public int getTag() { return CONSTANT_Float; }
public float getValue() { return value; }
public int getSize() { return 1; }
public void writeContentsTo(DataOutputStream stream) throws IOException {
stream.writeFloat(value);
}
}
public class LongEntry extends ChildlessEntry implements Entry {
private final long value;
public LongEntry(long value) { this.value = value; }
public LongEntry(DataInputStream stream) throws IOException {
this(stream.readLong());
}
public int hashCode() { return (int)value; }
public boolean equals(Object o) {
return o instanceof LongEntry && ((LongEntry)o).value == value;
}
public int getTag() { return CONSTANT_Long; }
public long getValue() { return value; }
public int getSize() { return 2; }
public void writeContentsTo(DataOutputStream stream) throws IOException {
stream.writeLong(value);
}
}
public class DoubleEntry extends ChildlessEntry implements Entry {
private final double value;
public DoubleEntry(double value) { this.value = value; }
public DoubleEntry(DataInputStream stream) throws IOException {
this(stream.readDouble());
}
public int hashCode() { return (int)value; }
public boolean equals(Object o) {
return o instanceof DoubleEntry && ((DoubleEntry)o).value == value;
}
public int getTag() { return CONSTANT_Double; }
public double getValue() { return value; }
public int getSize() { return 2; }
public void writeContentsTo(DataOutputStream stream) throws IOException {
stream.writeDouble(value);
}
}
public class Utf8Entry extends ChildlessEntry implements Entry {
private final String value;
private final byte[] bytes;
public Utf8Entry(String value) {
this.value = value.intern();
this.bytes = null;
}
public Utf8Entry(DataInputStream stream) throws IOException {
this(stream.readUTF());
}
public Utf8Entry(byte[] bytes) {
this.bytes = bytes;
this.value = null;
}
public int hashCode() {
if (bytes != null) return bytes.hashCode();
return value.hashCode();
}
public boolean equals(Object o) {
boolean isEqual = o instanceof Utf8Entry;
if (bytes != null) {
isEqual = isEqual && ((Utf8Entry)o).bytes == bytes;
}
else {
isEqual = isEqual && ((Utf8Entry)o).value == value;
}
return isEqual;
}
public int getTag() { return CONSTANT_Utf8; }
public String getValue() { return value; }
public byte[] getBytes() { return bytes; }
public int getSize() { return 1; }
public void writeContentsTo(DataOutputStream stream) throws IOException {
if (bytes != null) {
stream.writeShort(bytes.length);
stream.write(bytes);
}
else
stream.writeUTF(value);
}
}
abstract public class StringEntry implements Entry {
protected String value;
protected int valueIndex;
public int hashCode() {
assert value != null;
return value.hashCode();
}
public boolean equals(Object o) {
return o instanceof StringEntry && ((StringEntry)o).value == value;
}
public int getTag() { return CONSTANT_String; }
public String getValue() { return value; }
public int getSize() { return 1; }
public void writeContentsTo(DataOutputStream stream) throws IOException {
stream.writeShort(valueIndex);
}
}
public class StringEntryValue extends StringEntry implements EntryValue {
public StringEntryValue(String value) {
this.value = value.intern();
}
public void addChildren() {
valueIndex = addUtf8(value);
}
}
public class StringEntryIndex extends StringEntry implements EntryIndex {
public StringEntryIndex(int valueIndex) {
this.valueIndex = valueIndex;
}
public StringEntryIndex(DataInputStream stream) throws IOException {
this(stream.readShort());
}
public String getValue() {
if (value == null) fetchChildren();
return super.getValue();
}
public void fetchChildren() {
value = lookupUtf8(valueIndex);
}
}
abstract public class DescriptorEntry implements Entry {
protected String name;
protected int nameIndex;
public int hashCode() {
assert name != null;
return name.hashCode();
}
public boolean equals(Object o) {
return o instanceof DescriptorEntry && ((DescriptorEntry)o).name == name;
}
public int getTag() { return CONSTANT_Class; }
public String getValue() { return name; }
public int getSize() { return 1; }
public void writeContentsTo(DataOutputStream stream) throws IOException {
stream.writeShort(nameIndex);
}
}
protected class DescriptorEntryValue
extends DescriptorEntry
implements EntryValue {
public DescriptorEntryValue(String name) { this.name = name.intern(); }
public void addChildren() {
nameIndex = addUtf8(name);
}
}
protected class DescriptorEntryIndex
extends DescriptorEntry
implements EntryIndex {
public DescriptorEntryIndex(int nameIndex) { this.nameIndex = nameIndex; }
public DescriptorEntryIndex(DataInputStream stream) throws IOException {
this(stream.readShort());
}
public String getValue() {
if (name == null) fetchChildren();
return super.getValue();
}
public void fetchChildren() {
name = lookupUtf8(nameIndex);
}
}
abstract public class FieldOrMethodRefEntry implements Entry {
private final int tag;
protected String className, thingName, signature;
protected int classIndex, nameAndTypeIndex;
public FieldOrMethodRefEntry(int tag) {
assert tag == CONSTANT_Fieldref
|| tag == CONSTANT_Methodref
|| tag == CONSTANT_InterfaceMethodref;
this.tag = tag;
}
public int hashCode() {
return tag
+ className.hashCode()
+ thingName.hashCode()
+ signature.hashCode();
}
public boolean equals(Object o) {
return o instanceof FieldOrMethodRefEntry
&& ((FieldOrMethodRefEntry)o).tag == tag
&& ((FieldOrMethodRefEntry)o).className == className
&& ((FieldOrMethodRefEntry)o).thingName == thingName
&& ((FieldOrMethodRefEntry)o).signature == signature;
}
public int getTag() { return tag; }
public String getClassName() { return className; }
public String getFieldOrMethodName() { return thingName; }
public String getSignature() { return signature; }
public int getSize() { return 1; }
public void writeContentsTo(DataOutputStream stream) throws IOException {
stream.writeShort(classIndex);
stream.writeShort(nameAndTypeIndex);
}
}
protected class FieldOrMethodRefEntryValue
extends FieldOrMethodRefEntry
implements EntryValue {
public FieldOrMethodRefEntryValue(int tag,
String className,
String thingName,
String signature) {
super(tag);
this.className = className.intern();
this.thingName = thingName.intern();
this.signature = signature.intern();
}
public void addChildren() {
classIndex = addClass(className);
nameAndTypeIndex = addNameAndType(thingName, signature);
}
}
protected class FieldOrMethodRefEntryIndex
extends FieldOrMethodRefEntry
implements EntryIndex {
public FieldOrMethodRefEntryIndex(int tag,
int classIndex,
int nameAndTypeIndex) {
super(tag);
this.classIndex = classIndex;
this.nameAndTypeIndex = nameAndTypeIndex;
}
public FieldOrMethodRefEntryIndex(int tag, DataInputStream stream)
throws IOException {
this(tag, stream.readShort(), stream.readShort());
}
public String getClassName() {
if (className == null) fetchChildren();
return super.getClassName();
}
public String getFieldOrMethodName() {
if (thingName == null) fetchChildren();
return super.getFieldOrMethodName();
}
public String getSignature() {
if (signature == null) fetchChildren();
return super.getSignature();
}
public void fetchChildren() {
className = lookupClass(classIndex);
NameAndTypeEntry nat = (NameAndTypeEntry)lookupEntry(nameAndTypeIndex);
thingName = nat.getName();
signature = nat.getDescriptor();
}
}
abstract public class NameAndTypeEntry implements Entry {
protected String name, descriptor;
protected int nameIndex, descriptorIndex;
public int hashCode() { return name.hashCode() + descriptor.hashCode(); }
public boolean equals(Object o) {
return o instanceof NameAndTypeEntry
&& ((NameAndTypeEntry)o).name == name
&& ((NameAndTypeEntry)o).descriptor == descriptor;
}
public int getTag() { return CONSTANT_NameAndType; }
public String getName() { return name; }
public String getDescriptor() { return descriptor; }
public int getSize() { return 1; }
public void writeContentsTo(DataOutputStream stream) throws IOException {
stream.writeShort(nameIndex);
stream.writeShort(descriptorIndex);
}
}
protected class NameAndTypeEntryValue
extends NameAndTypeEntry
implements EntryValue {
public NameAndTypeEntryValue(String name, String descriptor) {
this.name = name.intern();
this.descriptor = descriptor.intern();
}
public void addChildren() {
nameIndex = addUtf8(name);
descriptorIndex = addUtf8(descriptor);
}
}
protected class NameAndTypeEntryIndex
extends NameAndTypeEntry
implements EntryIndex {
public NameAndTypeEntryIndex(int nameIndex, int descriptorIndex) {
this.nameIndex = nameIndex;
this.descriptorIndex = descriptorIndex;
}
public NameAndTypeEntryIndex(DataInputStream stream) throws IOException {
this(stream.readShort(), stream.readShort());
}
public String getName() {
if (name == null) fetchChildren();
return super.getName();
}
public String getDescriptor() {
if (descriptor == null) fetchChildren();
return super.getDescriptor();
}
public void fetchChildren() {
name = lookupUtf8(nameIndex);
descriptor = lookupUtf8(descriptorIndex);
}
}
}
|