1
3 package jminusminus;
4
5 import java.io.DataOutputStream;
6 import java.io.IOException;
7 import static jminusminus.CLConstants.*;
8
9
15
16 abstract class CLCPInfo {
17
18
24
25 public int cpIndex;
26
27
28 public short tag;
29
30
39
40 public void write(CLOutputStream out) throws IOException {
41 out.writeByte(tag);
42 }
43
44
51
52 public void writeToStdOut(PrettyPrinter p) {
53 p.printf("%-10s", cpIndex);
54 }
55
56 }
57
58
61
62 class CLConstantClassInfo extends CLCPInfo {
63
64
65 public int nameIndex;
66
67
73
74 public CLConstantClassInfo(int nameIndex) {
75 super.tag = CONSTANT_Class;
76 this.nameIndex = nameIndex;
77 }
78
79
82
83 public void write(CLOutputStream out) throws IOException {
84 super.write(out);
85 out.writeShort(nameIndex);
86 }
87
88
91
92 public boolean equals(Object obj) {
93 if (obj instanceof CLConstantClassInfo) {
94 CLConstantClassInfo c = (CLConstantClassInfo) obj;
95 if (c.nameIndex == nameIndex) {
96 return true;
97 }
98 }
99 return false;
100 }
101
102
105
106 public void writeToStdOut(PrettyPrinter p) {
107 super.writeToStdOut(p);
108 p.printf("%-20s%s\n", "Class", nameIndex);
109 }
110
111 }
112
113
117
118 abstract class CLConstantMemberRefInfo extends CLCPInfo {
119
120
121 public int classIndex;
122
123
124 public int nameAndTypeIndex;
125
126
136
137 protected CLConstantMemberRefInfo(int classIndex, int nameAndTypeIndex,
138 short tag) {
139 super.tag = tag;
140 this.classIndex = classIndex;
141 this.nameAndTypeIndex = nameAndTypeIndex;
142 }
143
144
147
148 public void write(CLOutputStream out) throws IOException {
149 super.write(out);
150 out.writeShort(classIndex);
151 out.writeShort(nameAndTypeIndex);
152 }
153
154
157
158 public boolean equals(Object obj) {
159 if (obj instanceof CLConstantMemberRefInfo) {
160 CLConstantMemberRefInfo c = (CLConstantMemberRefInfo) obj;
161 if ((c.tag == tag) && (c.classIndex == classIndex)
162 && (c.nameAndTypeIndex == nameAndTypeIndex)) {
163 return true;
164 }
165 }
166 return false;
167 }
168
169 }
170
171
174
175 class CLConstantFieldRefInfo extends CLConstantMemberRefInfo {
176
177
185
186 public CLConstantFieldRefInfo(int classIndex, int nameAndTypeIndex) {
187 super(classIndex, nameAndTypeIndex, CONSTANT_Fieldref);
188 }
189
190
193
194 public void writeToStdOut(PrettyPrinter p) {
195 super.writeToStdOut(p);
196 p.printf("%-20s%-8s%-8s\n", "FieldRef", classIndex, nameAndTypeIndex);
197 }
198
199 }
200
201
204
205 class CLConstantMethodRefInfo extends CLConstantMemberRefInfo {
206
207
215
216 public CLConstantMethodRefInfo(int classIndex, int nameAndTypeIndex) {
217 super(classIndex, nameAndTypeIndex, CONSTANT_Methodref);
218 }
219
220
223
224 public void writeToStdOut(PrettyPrinter p) {
225 super.writeToStdOut(p);
226 p.printf("%-20s%-8s%-8s\n", "MethodRef", classIndex, nameAndTypeIndex);
227 }
228
229 }
230
231
235
236 class CLConstantInterfaceMethodRefInfo extends CLConstantMemberRefInfo {
237
238
246
247 public CLConstantInterfaceMethodRefInfo(int classIndex, int nameAndTypeIndex) {
248 super(classIndex, nameAndTypeIndex, CONSTANT_InterfaceMethodref);
249 }
250
251
254
255 public void writeToStdOut(PrettyPrinter p) {
256 super.writeToStdOut(p);
257 p.printf("%-20s%-8s%-8s\n", "InterfaceMethodRef", classIndex,
258 nameAndTypeIndex);
259 }
260
261 }
262
263
266
267 class CLConstantStringInfo extends CLCPInfo {
268
269
270 public int stringIndex;
271
272
278
279 public CLConstantStringInfo(int stringIndex) {
280 super.tag = CONSTANT_String;
281 this.stringIndex = stringIndex;
282 }
283
284
287
288 public void write(CLOutputStream out) throws IOException {
289 super.write(out);
290 out.writeShort(stringIndex);
291 }
292
293
296
297 public boolean equals(Object obj) {
298 if (obj instanceof CLConstantStringInfo) {
299 CLConstantStringInfo c = (CLConstantStringInfo) obj;
300 if (c.stringIndex == stringIndex) {
301 return true;
302 }
303 }
304 return false;
305 }
306
307
310
311 public void writeToStdOut(PrettyPrinter p) {
312 super.writeToStdOut(p);
313 p.printf("%-20s%s\n", "String", stringIndex);
314 }
315
316 }
317
318
321
322 class CLConstantIntegerInfo extends CLCPInfo {
323
324
325 public int i;
326
327
333
334 public CLConstantIntegerInfo(int i) {
335 super.tag = CONSTANT_Integer;
336 this.i = i;
337 }
338
339
344
345 public short[] bytes() {
346 short[] s = new short[4];
347 short mask = 0xFF;
348 int k = i;
349 for (int j = 0; j < 4; j++) {
350 s[3 - j] = (short) (k & mask);
351 k >>>= 8;
352 }
353 return s;
354 }
355
356
359
360 public void write(CLOutputStream out) throws IOException {
361 super.write(out);
362
363 ((DataOutputStream) out).writeInt(i);
367 }
368
369
372
373 public boolean equals(Object obj) {
374 if (obj instanceof CLConstantIntegerInfo) {
375 CLConstantIntegerInfo c = (CLConstantIntegerInfo) obj;
376 if (c.i == i) {
377 return true;
378 }
379 }
380 return false;
381 }
382
383
386
387 public void writeToStdOut(PrettyPrinter p) {
388 super.writeToStdOut(p);
389 p.printf("%-20s%s\n", "Integer", i);
390 }
391
392 }
393
394
397
398 class CLConstantFloatInfo extends CLCPInfo {
399
400
401 public float f;
402
403
409
410 public CLConstantFloatInfo(float f) {
411 super.tag = CONSTANT_Float;
412 this.f = f;
413 }
414
415
420
421 public short[] bytes() {
422 short[] s = new short[4];
423 short mask = 0xFF;
424 int i = Float.floatToIntBits(f);
425 for (int j = 0; j < 4; j++) {
426 s[3 - j] = (short) (i & mask);
427 i >>>= 8;
428 }
429 return s;
430 }
431
432
435
436 public void write(CLOutputStream out) throws IOException {
437 super.write(out);
438 out.writeFloat(f);
439 }
440
441
444
445 public boolean equals(Object obj) {
446 if (obj instanceof CLConstantFloatInfo) {
447 CLConstantFloatInfo c = (CLConstantFloatInfo) obj;
448 if (c.f == f) {
449 return true;
450 }
451 }
452 return false;
453 }
454
455
458
459 public void writeToStdOut(PrettyPrinter p) {
460 super.writeToStdOut(p);
461 p.printf("%-20s%s\n", "Float", f);
462 }
463
464 }
465
466
469
470 class CLConstantLongInfo extends CLCPInfo {
471
472
473 public long l;
474
475
480
481 private short[] bytes() {
482 short[] s = new short[8];
483 short mask = 0xFF;
484 long k = l;
485 for (int j = 0; j < 8; j++) {
486 s[7 - j] = (short) (k & mask);
487 k >>>= 8;
488 }
489 return s;
490 }
491
492
498
499 public CLConstantLongInfo(long l) {
500 super.tag = CONSTANT_Long;
501 this.l = l;
502 }
503
504
509
510 public short[] lowBytes() {
511 short[] s = bytes();
512 short[] l = new short[4];
513 l[0] = s[4];
514 l[1] = s[5];
515 l[2] = s[6];
516 l[3] = s[7];
517 return l;
518 }
519
520
525
526 public short[] highBytes() {
527 short[] s = bytes();
528 short[] h = new short[4];
529 h[0] = s[0];
530 h[1] = s[1];
531 h[2] = s[2];
532 h[3] = s[3];
533 return h;
534 }
535
536
539
540 public void write(CLOutputStream out) throws IOException {
541 super.write(out);
542 out.writeLong(l);
543 }
544
545
548
549 public boolean equals(Object obj) {
550 if (obj instanceof CLConstantLongInfo) {
551 CLConstantLongInfo c = (CLConstantLongInfo) obj;
552 if (c.l == l) {
553 return true;
554 }
555 }
556 return false;
557 }
558
559
562
563 public void writeToStdOut(PrettyPrinter p) {
564 super.writeToStdOut(p);
565 p.printf("%-20s%s\n", "Long", l);
566 }
567
568 }
569
570
573
574 class CLConstantDoubleInfo extends CLCPInfo {
575
576
577 public double d;
578
579
584
585 private short[] bytes() {
586 short[] s = new short[8];
587 short mask = 0xFF;
588 long l = Double.doubleToLongBits(d);
589 for (int j = 0; j < 8; j++) {
590 s[7 - j] = (short) (l & mask);
591 l >>>= 8;
592 }
593 return s;
594 }
595
596
602
603 public CLConstantDoubleInfo(double d) {
604 super.tag = CONSTANT_Double;
605 this.d = d;
606 }
607
608
613
614 public short[] lowBytes() {
615 short[] s = bytes();
616 short[] l = new short[4];
617 l[0] = s[4];
618 l[1] = s[5];
619 l[2] = s[6];
620 l[3] = s[7];
621 return l;
622 }
623
624
629
630 public short[] highBytes() {
631 short[] s = bytes();
632 short[] h = new short[4];
633 h[0] = s[0];
634 h[1] = s[1];
635 h[2] = s[2];
636 h[3] = s[3];
637 return h;
638 }
639
640
643
644 public void write(CLOutputStream out) throws IOException {
645 super.write(out);
646 out.writeDouble(d);
647 }
648
649
652
653 public boolean equals(Object obj) {
654 if (obj instanceof CLConstantDoubleInfo) {
655 CLConstantDoubleInfo c = (CLConstantDoubleInfo) obj;
656 if (c.d == d) {
657 return true;
658 }
659 }
660 return false;
661 }
662
663
666
667 public void writeToStdOut(PrettyPrinter p) {
668 super.writeToStdOut(p);
669 p.printf("%-20s%s\n", "Double", d);
670 }
671
672 }
673
674
678
679 class CLConstantNameAndTypeInfo extends CLCPInfo {
680
681
682 public int nameIndex;
683
684
685 public int descriptorIndex;
686
687
695
696 public CLConstantNameAndTypeInfo(int nameIndex, int descriptorIndex) {
697 super.tag = CONSTANT_NameAndType;
698 this.nameIndex = nameIndex;
699 this.descriptorIndex = descriptorIndex;
700 }
701
702
705
706 public void write(CLOutputStream out) throws IOException {
707 super.write(out);
708 out.writeShort(nameIndex);
709 out.writeShort(descriptorIndex);
710 }
711
712
715
716 public boolean equals(Object obj) {
717 if (obj instanceof CLConstantNameAndTypeInfo) {
718 CLConstantNameAndTypeInfo c = (CLConstantNameAndTypeInfo) obj;
719 if ((c.nameIndex == nameIndex)
720 && (c.descriptorIndex == descriptorIndex)) {
721 return true;
722 }
723 }
724 return false;
725 }
726
727
730
731 public void writeToStdOut(PrettyPrinter p) {
732 super.writeToStdOut(p);
733 p.printf("%-20s%-8s%-8s\n", "NameAndType", nameIndex, descriptorIndex);
734 }
735
736 }
737
738
741
742 class CLConstantUtf8Info extends CLCPInfo {
743
744
745 public byte[] b;
746
747
753
754 public CLConstantUtf8Info(byte[] b) {
755 super.tag = CONSTANT_Utf8;
756 this.b = b;
757 }
758
759
764
765 public int length() {
766 return b.length;
767 }
768
769
772
773 public void write(CLOutputStream out) throws IOException {
774 super.write(out);
775 out.writeUTF(new String(b));
776 }
777
778
781
782 public boolean equals(Object obj) {
783 if (obj instanceof CLConstantUtf8Info) {
784 CLConstantUtf8Info c = (CLConstantUtf8Info) obj;
785 if ((new String(b)).equals(new String(c.b))) {
786 return true;
787 }
788 }
789 return false;
790 }
791
792
795
796 public void writeToStdOut(PrettyPrinter p) {
797 super.writeToStdOut(p);
798 p.printf("%-20s%s\n", "Utf8", new String(b));
799 }
800
801 }
802