-
Notifications
You must be signed in to change notification settings - Fork 350
/
Copy pathClassFile.java
2735 lines (2406 loc) · 84.3 KB
/
ClassFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014, United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The Java Pathfinder core (jpf-core) platform is licensed under the
* Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gov.nasa.jpf.jvm;
import gov.nasa.jpf.jvm.JVMByteCodeReader;
import gov.nasa.jpf.vm.ClassParseException;
import gov.nasa.jpf.JPFException;
import gov.nasa.jpf.util.BailOut;
import gov.nasa.jpf.util.BinaryClassSource;
import java.io.File;
/**
* class to read and dissect Java classfile contents (as specified by the Java VM
* spec http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#16628
*/
public class ClassFile extends BinaryClassSource {
public static final int CONSTANT_UTF8 = 1;
public static final int CONSTANT_INTEGER = 3;
public static final int CONSTANT_FLOAT = 4;
public static final int CONSTANT_LONG = 5;
public static final int CONSTANT_DOUBLE = 6;
public static final int CONSTANT_CLASS = 7;
public static final int CONSTANT_STRING = 8;
public static final int FIELD_REF = 9;
public static final int METHOD_REF = 10;
public static final int INTERFACE_METHOD_REF = 11;
public static final int NAME_AND_TYPE = 12;
public static final int METHOD_HANDLE = 15;
public static final int METHOD_TYPE = 16;
public static final int INVOKE_DYNAMIC = 18;
public static final int REF_GETFIELD = 1;
public static final int REF_GETSTATIC = 2;
public static final int REF_PUTFIELD = 3;
public static final int REF_PUTSTATIC = 4;
public static final int REF_INVOKEVIRTUAL = 5;
public static final int REF_INVOKESTATIC = 6;
public static final int REF_INVOKESPECIAL = 7;
public static final int REF_NEW_INVOKESPECIAL = 8;
public static final int REF_INVOKEINTERFACE = 9;
// used to store types in cpValue[]
public static enum CpInfo {
Unused_0, // 0
ConstantUtf8, // 1
Unused_2, // 2
ConstantInteger, // 3
ConstantFloat, // 4
ConstantLong, // 5
ConstantDouble, // 6
ConstantClass, // 7
ConstantString, // 8
FieldRef, // 9
MethodRef, // 10
InterfaceMethodRef, // 11
NameAndType, // 12
Unused_13,
Unused_14,
MethodHandle, // 15
MethodType, // 16
Unused_17,
InvokeDynamic // 18
}
// <2do> this is going away
String requestedTypeName; // the type name that caused this classfile to be loaded
// the const pool
int[] cpPos; // cpPos[i] holds data start index for cp_entry i (0 is unused)
Object[] cpValue; // cpValue[i] hold the String/Integer/Float/Double associated with corresponding cp_entries
//--- ctors
public ClassFile (byte[] data, int offset){
super(data,offset);
}
public ClassFile (byte[] data){
super(data,0);
}
public ClassFile (String typeName, byte[] data){
super(data,0);
this.requestedTypeName = typeName;
}
public ClassFile (String typeName, byte[] data, int offset){
super(data, offset);
this.requestedTypeName = typeName;
}
public ClassFile (File file) throws ClassParseException {
super(file);
}
public ClassFile (String pathName) throws ClassParseException {
super( new File(pathName));
}
/**
* set classfile data. This is mainly provided to allow
* on-the-fly classfile instrumentation with 3rd party libraries
*
* BEWARE - like getData(), this method can cause parsing to fail if the
* provided data does not conform to the VM specs. In particular, this
* method should ONLY be called before executing parse(ClassFileReader) and
* will otherwise throw a JPFException
*/
public void setData(byte[] newData){
if (cpPos != null){
throw new JPFException("concurrent modification of ClassFile data");
}
data = newData;
}
/**
* return the typename this classfile gets loaded for
* <2do> this is going away
*/
public String getRequestedTypeName(){
return requestedTypeName;
}
//--- general attributes
public static final String SYNTHETIC_ATTR = "Synthetic";
public static final String DEPRECATED_ATTR = "Deprecated";
public static final String SIGNATURE_ATTR = "Signature";
public static final String RUNTIME_INVISIBLE_ANNOTATIONS_ATTR = "RuntimeInvisibleAnnotations";
public static final String RUNTIME_VISIBLE_ANNOTATIONS_ATTR = "RuntimeVisibleAnnotations";
public static final String RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR = "RuntimeVisibleTypeAnnotations";
//--- standard field attributes
public static final String CONST_VALUE_ATTR = "ConstantValue";
protected final static String[] stdFieldAttrs = {
CONST_VALUE_ATTR, SYNTHETIC_ATTR, DEPRECATED_ATTR, SIGNATURE_ATTR,
RUNTIME_INVISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR };
//--- standard method attributes
public static final String CODE_ATTR = "Code";
public static final String EXCEPTIONS_ATTR = "Exceptions";
public static final String RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS_ATTR = "RuntimeInvisibleParameterAnnotations";
public static final String RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS_ATTR = "RuntimeVisibleParameterAnnotations";
public static final String ANNOTATIONDEFAULT_ATTR = "AnnotationDefault";
protected final static String[] stdMethodAttrs = {
CODE_ATTR, EXCEPTIONS_ATTR, SYNTHETIC_ATTR, DEPRECATED_ATTR, SIGNATURE_ATTR,
RUNTIME_INVISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_ANNOTATIONS_ATTR,
RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS_ATTR,
RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS_ATTR,
RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR,
ANNOTATIONDEFAULT_ATTR
};
//--- standard code attributes
public static final String LINE_NUMBER_TABLE_ATTR = "LineNumberTable";
public static final String LOCAL_VAR_TABLE_ATTR = "LocalVariableTable";
protected final static String[] stdCodeAttrs = { LINE_NUMBER_TABLE_ATTR, LOCAL_VAR_TABLE_ATTR, RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR };
//--- standard class attributes
public static final String SOURCE_FILE_ATTR = "SourceFile";
public static final String INNER_CLASSES_ATTR = "InnerClasses";
public static final String ENCLOSING_METHOD_ATTR = "EnclosingMethod";
public static final String BOOTSTRAP_METHOD_ATTR = "BootstrapMethods";
protected final static String[] stdClassAttrs = {
SOURCE_FILE_ATTR, DEPRECATED_ATTR, INNER_CLASSES_ATTR, DEPRECATED_ATTR, SIGNATURE_ATTR,
RUNTIME_INVISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR,
ENCLOSING_METHOD_ATTR, BOOTSTRAP_METHOD_ATTR };
protected String internStdAttrName(int cpIdx, String name, String[] stdNames){
for (int i=0; i<stdNames.length; i++){
if (stdNames[i] == name) return name;
}
for (int i=0; i<stdNames.length; i++){
String stdName = stdNames[i];
if (stdName.equals(name)){
cpValue[cpIdx] = stdName;
return stdName;
}
}
return name;
}
//--- constpool access
//--- the primitive info cpValue
public String utf8At(int utf8InfoIdx){
//assert data[cpPos[utf8InfoIdx]] == 1 : "not a utf8_info tag";
return (String) cpValue[utf8InfoIdx];
}
public int intAt(int intInfoIdx){
//assert data[cpPos[intInfoIdx]] == 3 : "not a int_info tag";
return (Integer) cpValue[intInfoIdx];
}
public float floatAt(int floatInfoIdx){
//assert data[cpPos[floatInfoIdx]] == 4 : "not a float_info tag";
return (Float) cpValue[floatInfoIdx];
}
public long longAt(int longInfoIdx){
//assert data[cpPos[longInfoIdx]] == 5 : "not a long_info tag";
return (Long) cpValue[longInfoIdx];
}
public double doubleAt(int doubleInfoIdx){
//assert data[cpPos[doubleInfoIdx]] == 6 : "not a double_info tag";
return (Double) cpValue[doubleInfoIdx];
}
//--- those two are delegated but resolved
public String classNameAt(int classInfoIdx){
//assert data[cpPos[classInfoIdx]] == 7 : "not a Class_info tag";
return (String) cpValue[classInfoIdx];
}
public String stringAt(int stringInfoIdx){
//assert data[cpPos[stringInfoIdx]] == 8 : "not a String_info tag";
return (String) cpValue[stringInfoIdx];
}
//--- composite infos
// the generic ones (if we don't care what kind of reference type this is)
public String refClassNameAt(int cpIdx){
return (String) cpValue[ u2(cpPos[cpIdx]+1)];
}
public String refNameAt(int cpIdx){
return utf8At( u2( cpPos[ u2(cpPos[cpIdx]+3)]+1));
}
public String refDescriptorAt(int cpIdx){
return utf8At( u2( cpPos[ u2(cpPos[cpIdx]+3)]+3));
}
public int mhRefTypeAt (int methodHandleInfoIdx){
return u1(cpPos[methodHandleInfoIdx]+1);
}
public int mhMethodRefIndexAt (int methodHandleInfoIdx){
return u2(cpPos[methodHandleInfoIdx]+2);
}
// those could check ref types
public String fieldClassNameAt(int fieldRefInfoIdx){
//assert data[cpPos[fieldRefInfoIdx]] == 9 : "not a Fieldref_info tag";
return (String) cpValue[ u2(cpPos[fieldRefInfoIdx]+1)];
}
public String fieldNameAt(int fieldRefInfoIdx){
return utf8At( u2( cpPos[ u2(cpPos[fieldRefInfoIdx]+3)]+1));
}
public String fieldDescriptorAt(int fieldRefInfoIdx){
return utf8At( u2( cpPos[ u2(cpPos[fieldRefInfoIdx]+3)]+3));
}
public String methodClassNameAt(int methodRefInfoIdx){
return (String) cpValue[ u2(cpPos[methodRefInfoIdx]+1)];
}
public String methodNameAt(int methodRefInfoIdx){
return utf8At( u2( cpPos[ u2(cpPos[methodRefInfoIdx]+3)]+1));
}
public String methodDescriptorAt(int methodRefInfoIdx){
return utf8At( u2( cpPos[ u2(cpPos[methodRefInfoIdx]+3)]+3));
}
public String methodTypeDescriptorAt (int methodTypeInfoIdx){
return utf8At( u2(cpPos[methodTypeInfoIdx]+1));
}
public String interfaceMethodClassNameAt(int ifcMethodRefInfoIdx){
return (String) cpValue[ u2(cpPos[ifcMethodRefInfoIdx]+1)];
}
public String interfaceMethodNameAt(int ifcMethodRefInfoIdx){
return utf8At( u2( cpPos[ u2(cpPos[ifcMethodRefInfoIdx]+3)]+1));
}
public String interfaceMethodDescriptorAt(int ifcMethodRefInfoIdx){
return utf8At( u2( cpPos[ u2(cpPos[ifcMethodRefInfoIdx]+3)]+3));
}
public int bootstrapMethodIndex (int cpInvokeDynamicIndex){
return u2(cpPos[cpInvokeDynamicIndex]+1);
}
public String samMethodNameAt(int cpInvokeDynamicIndex) {
return utf8At( u2( cpPos[ u2(cpPos[cpInvokeDynamicIndex]+3)]+1));
}
public String callSiteDescriptor(int cpInvokeDynamicIndex) {
return utf8At( u2( cpPos[ u2(cpPos[cpInvokeDynamicIndex]+3)]+3));
}
public String getRefTypeName (int refCode){
switch (refCode){
case REF_GETFIELD: return "getfield";
case REF_GETSTATIC: return "getstatic";
case REF_PUTFIELD: return "putfield";
case REF_PUTSTATIC: return "putstatic";
case REF_INVOKEVIRTUAL: return "invokevirtual";
case REF_INVOKESTATIC: return "invokestatic";
case REF_INVOKESPECIAL: return "invokespecial";
case REF_NEW_INVOKESPECIAL: return "new-invokespecial";
case REF_INVOKEINTERFACE: return "invokeinterface";
default:
return "<unknown>";
}
}
public String getTypeName (int typeCode){
switch(typeCode){
case 4: return "boolean";
case 5: return "char";
case 6: return "float";
case 7: return "double";
case 8: return "byte";
case 9: return "short";
case 10: return "int";
case 11: return "long";
default:
return "<unknown>";
}
}
@Override
public int getPos(){
return pos;
}
public int getPc(){
return pc;
}
//--- traverse/analyze the const pool (this is rather exotic)
public int getNumberOfCpEntries(){
return cpValue.length;
}
public Object getCpValue (int i){
return cpValue[i];
}
public int getCpTag (int i){
return data[cpPos[i]];
}
/**
* the result can be used as input for u2(dataIndex)
*
* NOTE - this returns -1 for the dreaded unused extra entries associated
* with ConstantDouble and ConstantLong
*/
public int getDataPosOfCpEntry (int i){
return cpPos[i];
}
//--- standard attributes
public Object getConstValueAttribute(int dataPos){
int cpIdx = u2(dataPos);
Object v = cpValue[cpIdx];
return v;
}
public String getSourceFileAttribute(int dataPos){
// SourceFile_attribute { u2 attr_name_idx; u4 attr_length; u2 sourcefile_idx<utf8>; }
int cpIdx = u2(dataPos + 6);
Object v = cpValue[cpIdx];
return (String)v;
}
//--- low level readers
public final int u1(int dataIdx){
return data[dataIdx] & 0xff;
}
public final int u2(int dataIdx){
return ((data[dataIdx]&0xff) << 8) | (data[dataIdx+1]&0xff);
}
public final int i1(int dataIdx) {
return data[dataIdx++];
}
public final int i2(int dataIdx) {
int idx = dataIdx;
return (data[idx++] << 8) | (data[idx]&0xff);
}
public final int readU2(){
int idx = pos;
pos += 2;
return ((data[idx++]&0xff) << 8) | (data[idx]&0xff);
}
public final int readI2() {
int idx = pos;
pos += 2;
return (data[idx++] << 8) | (data[idx]&0xff);
}
public final int readI4(){
int idx = pos;
pos += 4;
byte[] data = this.data;
return (data[idx++] <<24) | ((data[idx++]&0xff) << 16) | ((data[idx++]&0xff) << 8) | (data[idx]&0xff);
}
//--- reader notifications
private void setClass(ClassFileReader reader, String clsName, String superClsName, int flags, int cpCount) throws ClassParseException {
int p = pos;
reader.setClass( this, clsName, superClsName, flags, cpCount);
pos = p;
}
private void setInterfaceCount(ClassFileReader reader, int ifcCount){
int p = pos;
reader.setInterfaceCount( this, ifcCount);
pos = p;
}
private void setInterface(ClassFileReader reader, int ifcIndex, String ifcName){
int p = pos;
reader.setInterface( this, ifcIndex, ifcName);
pos = p;
}
private void setInterfacesDone(ClassFileReader reader){
int p = pos;
reader.setInterfacesDone( this);
pos = p;
}
private void setFieldCount(ClassFileReader reader, int fieldCount){
int p = pos;
reader.setFieldCount( this, fieldCount);
pos = p;
}
private void setField(ClassFileReader reader, int fieldIndex, int accessFlags, String name, String descriptor){
int p = pos;
reader.setField( this, fieldIndex, accessFlags, name, descriptor);
pos = p;
}
private void setFieldAttributeCount(ClassFileReader reader, int fieldIndex, int attrCount){
int p = pos;
reader.setFieldAttributeCount( this, fieldIndex, attrCount);
pos = p;
}
private void setFieldAttribute(ClassFileReader reader, int fieldIndex, int attrIndex, String name, int attrLength){
int p = pos + attrLength;
reader.setFieldAttribute( this, fieldIndex, attrIndex, name, attrLength);
pos = p;
}
private void setFieldAttributesDone(ClassFileReader reader, int fieldIndex){
int p = pos;
reader.setFieldAttributesDone( this, fieldIndex);
pos = p;
}
private void setFieldDone(ClassFileReader reader, int fieldIndex){
int p = pos;
reader.setFieldDone( this, fieldIndex);
pos = p;
}
private void setFieldsDone(ClassFileReader reader){
int p = pos;
reader.setFieldsDone( this);
pos = p;
}
private void setConstantValue(ClassFileReader reader, Object tag, Object value){
int p = pos;
reader.setConstantValue( this, tag, value);
pos = p;
}
private void setMethodCount(ClassFileReader reader, int methodCount){
int p = pos;
reader.setMethodCount( this, methodCount);
pos = p;
}
private void setMethod(ClassFileReader reader, int methodIndex, int accessFlags, String name, String descriptor){
int p = pos;
reader.setMethod( this, methodIndex, accessFlags, name, descriptor);
pos = p;
}
private void setMethodAttributeCount(ClassFileReader reader, int methodIndex, int attrCount){
int p = pos;
reader.setMethodAttributeCount( this, methodIndex, attrCount);
pos = p;
}
private void setMethodAttribute(ClassFileReader reader, int methodIndex, int attrIndex, String name, int attrLength){
int p = pos + attrLength;
reader.setMethodAttribute( this, methodIndex, attrIndex, name, attrLength);
pos = p;
}
private void setMethodAttributesDone(ClassFileReader reader, int methodIndex){
int p = pos;
reader.setMethodAttributesDone( this, methodIndex);
pos = p;
}
private void setMethodDone(ClassFileReader reader, int methodIndex){
int p = pos;
reader.setMethodDone( this, methodIndex);
pos = p;
}
private void setMethodsDone(ClassFileReader reader){
int p = pos;
reader.setMethodsDone( this);
pos = p;
}
private void setExceptionCount(ClassFileReader reader, Object tag, int exceptionCount){
int p = pos;
reader.setExceptionCount( this, tag, exceptionCount);
pos = p;
}
private void setExceptionsDone(ClassFileReader reader, Object tag){
int p = pos;
reader.setExceptionsDone( this, tag);
pos = p;
}
private void setException(ClassFileReader reader, Object tag, int exceptionIndex, String exceptionType){
int p = pos;
reader.setException( this, tag, exceptionIndex, exceptionType);
pos = p;
}
private void setCode(ClassFileReader reader, Object tag, int maxStack, int maxLocals, int codeLength){
int p = pos + codeLength;
reader.setCode( this, tag, maxStack, maxLocals, codeLength);
pos = p;
}
private void setExceptionTableCount(ClassFileReader reader, Object tag, int exceptionTableCount){
int p = pos;
reader.setExceptionHandlerTableCount( this, tag, exceptionTableCount);
pos = p;
}
private void setExceptionTableEntry(ClassFileReader reader, Object tag, int exceptionIndex,
int startPc, int endPc, int handlerPc, String catchType){
int p = pos;
reader.setExceptionHandler( this, tag, exceptionIndex, startPc, endPc, handlerPc, catchType);
pos = p;
}
private void setExceptionTableDone(ClassFileReader reader, Object tag){
int p = pos;
reader.setExceptionHandlerTableDone( this, tag);
pos = p;
}
private void setCodeAttributeCount(ClassFileReader reader, Object tag, int attrCount){
int p = pos;
reader.setCodeAttributeCount( this, tag, attrCount);
pos = p;
}
private void setCodeAttribute(ClassFileReader reader, Object tag, int attrIndex, String name, int attrLength){
int p = pos + attrLength;
reader.setCodeAttribute( this, tag, attrIndex, name, attrLength);
pos = p;
}
private void setCodeAttributesDone(ClassFileReader reader, Object tag){
int p = pos;
reader.setCodeAttributesDone( this, tag);
pos = p;
}
private void setLineNumberTableCount(ClassFileReader reader, Object tag, int lineNumberCount){
int p = pos;
reader.setLineNumberTableCount( this, tag, lineNumberCount);
pos = p;
}
private void setLineNumber(ClassFileReader reader, Object tag, int lineIndex, int lineNumber, int startPc){
int p = pos;
reader.setLineNumber( this, tag, lineIndex, lineNumber, startPc);
pos = p;
}
private void setLineNumberTableDone(ClassFileReader reader, Object tag){
int p = pos;
reader.setLineNumberTableDone( this, tag);
pos = p;
}
private void setLocalVarTableCount(ClassFileReader reader, Object tag, int localVarCount){
int p = pos;
reader.setLocalVarTableCount( this, tag, localVarCount);
pos = p;
}
private void setLocalVar(ClassFileReader reader, Object tag, int localVarIndex, String varName, String descriptor,
int scopeStartPc, int scopeEndPc, int slotIndex){
int p = pos;
reader.setLocalVar( this, tag, localVarIndex, varName, descriptor, scopeStartPc, scopeEndPc, slotIndex);
pos = p;
}
private void setLocalVarTableDone(ClassFileReader reader, Object tag){
int p = pos;
reader.setLocalVarTableDone( this, tag);
pos = p;
}
private void setClassAttributeCount(ClassFileReader reader, int attrCount){
int p = pos;
reader.setClassAttributeCount( this, attrCount);
pos = p;
}
private void setClassAttribute(ClassFileReader reader, int attrIndex, String name, int attrLength){
int p = pos + attrLength;
reader.setClassAttribute( this, attrIndex, name, attrLength);
pos = p;
}
private void setClassAttributesDone(ClassFileReader reader){
int p = pos;
reader.setClassAttributesDone(this);
pos = p;
}
private void setSourceFile(ClassFileReader reader, Object tag, String pathName){
int p = pos;
reader.setSourceFile( this, tag, pathName);
pos = p;
}
private void setBootstrapMethodCount (ClassFileReader reader, Object tag, int bootstrapMethodCount){
int p = pos;
reader.setBootstrapMethodCount( this, tag, bootstrapMethodCount);
pos = p;
}
private void setBootstrapMethod (ClassFileReader reader, Object tag, int idx,
int refKind, String cls, String mth, String descriptor, int[] cpArgs){
int p = pos;
reader.setBootstrapMethod( this, tag, idx, refKind, cls, mth, descriptor, cpArgs);
pos = p;
}
private void setBootstrapMethodsDone (ClassFileReader reader, Object tag){
int p = pos;
reader.setBootstrapMethodsDone( this, tag);
pos = p;
}
private void setInnerClassCount(ClassFileReader reader, Object tag, int innerClsCount){
int p = pos;
reader.setInnerClassCount( this, tag, innerClsCount);
pos = p;
}
private void setInnerClass(ClassFileReader reader, Object tag, int innerClsIndex, String outerName, String innerName,
String innerSimpleName, int accessFlags){
int p = pos;
reader.setInnerClass( this, tag, innerClsIndex, outerName, innerName, innerSimpleName, accessFlags);
pos = p;
}
private void setEnclosingMethod(ClassFileReader reader, Object tag, String enclosingClass, String enclosedMethod, String descriptor){
int p = pos;
reader.setEnclosingMethod( this, tag, enclosingClass, enclosedMethod, descriptor);
pos = p;
}
private void setInnerClassesDone(ClassFileReader reader, Object tag){
int p = pos;
reader.setInnerClassesDone(this, tag);
pos = p;
}
private void setAnnotationCount(ClassFileReader reader, Object tag, int annotationCount){
int p = pos;
reader.setAnnotationCount( this, tag, annotationCount);
pos = p;
}
private void setAnnotation(ClassFileReader reader, Object tag, int annotationIndex, String annotationType){
int p = pos;
reader.setAnnotation( this, tag, annotationIndex, annotationType);
pos = p;
}
private void setAnnotationsDone(ClassFileReader reader, Object tag){
int p = pos;
reader.setAnnotationsDone(this, tag);
pos = p;
}
private void setTypeAnnotationCount(ClassFileReader reader, Object tag, int annotationCount){
int p = pos;
reader.setTypeAnnotationCount( this, tag, annotationCount);
pos = p;
}
private void setTypeAnnotationsDone(ClassFileReader reader, Object tag){
int p = pos;
reader.setTypeAnnotationsDone(this, tag);
pos = p;
}
private void setAnnotationValueCount(ClassFileReader reader, Object tag, int annotationIndex, int nValuePairs){
int p = pos;
reader.setAnnotationValueCount( this, tag, annotationIndex, nValuePairs);
pos = p;
}
private void setPrimitiveAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
String elementName, int arrayIndex, Object val){
int p = pos;
reader.setPrimitiveAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
pos = p;
}
private void setStringAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
String elementName, int arrayIndex, String s){
int p = pos;
reader.setStringAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, s);
pos = p;
}
private void setClassAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
String elementName, int arrayIndex, String typeName){
int p = pos;
reader.setClassAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, typeName);
pos = p;
}
private void setEnumAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
String elementName, int arrayIndex, String enumType, String enumValue){
int p = pos;
reader.setEnumAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, enumType, enumValue);
pos = p;
}
private void setAnnotationValueElementCount(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
String elementName, int elementCount){
int p = pos;
reader.setAnnotationValueElementCount(this, tag, annotationIndex, valueIndex, elementName, elementCount);
pos = p;
}
private void setAnnotationValueElementsDone(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
String elementName){
int p = pos;
reader.setAnnotationValueElementsDone(this, tag, annotationIndex, valueIndex, elementName);
pos = p;
}
public void setAnnotationValuesDone(ClassFileReader reader, Object tag, int annotationIndex){
int p = pos;
reader.setAnnotationValuesDone(this, tag, annotationIndex);
pos = p;
}
private void setParameterCount(ClassFileReader reader, Object tag, int parameterCount){
int p = pos;
reader.setParameterCount(this, tag, parameterCount);
pos = p;
}
private void setParameterAnnotationCount(ClassFileReader reader, Object tag, int paramIndex, int annotationCount){
int p = pos;
reader.setParameterAnnotationCount(this, tag, paramIndex, annotationCount);
pos = p;
}
private void setParameterAnnotation(ClassFileReader reader, Object tag, int annotationIndex, String annotationType){
int p = pos;
reader.setParameterAnnotation( this, tag, annotationIndex, annotationType);
pos = p;
}
private void setParameterAnnotationsDone(ClassFileReader reader, Object tag, int paramIndex){
int p = pos;
reader.setParameterAnnotationsDone(this, tag, paramIndex);
pos = p;
}
private void setParametersDone(ClassFileReader reader, Object tag){
int p = pos;
reader.setParametersDone(this, tag);
pos = p;
}
public void setSignature(ClassFileReader reader, Object tag, String signature){
int p = pos;
reader.setSignature(this, tag, signature);
pos = p;
}
//--- parsing
/**
* this is the main parsing routine that uses the ClassFileReader interface
* to tell clients about the classfile contents
*
* ClassFile structure: http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#74353
* u4 magic; // 0xcafebabe
* u2 minor_version;
* u2 major_version;
*
* u2 constant_pool_count;
* cp_entry constant_pool[constant_pool_count-1];
* u2 access_flags;
*
* u2 this_class;
* u2 super_class;
*
* u2 interfaces_count;
* u2 interfaces[interfaces_count];
*
* u2 fields_count;
* field_info fields[fields_count];
*
* u2 methods_count;
* method_info methods[methods_count];
*
* u2 attributes_count;
* attribute_info attributes[attributes_count];
*/
public void parse( ClassFileReader reader) throws ClassParseException {
int cpIdx;
try {
// yeah, cafebabe
int magic = readI4();
if (magic != 0xCAFEBABE) {
error("wrong magic: " + Integer.toHexString(magic));
}
// we don't do much with the version numbers yet
int minor = readU2();
int major = readU2();
// get the const pool
int cpCount = readU2();
cpPos = new int[cpCount];
cpValue = new Object[cpCount];
parseCp(cpCount);
// the class essentials
int accessFlags = readU2();
cpIdx = readU2();
String clsName = (String) cpValue[cpIdx];
cpIdx = readU2();
String superClsName = (String) cpValue[cpIdx];
setClass(reader, clsName, superClsName, accessFlags, cpCount);
// interfaces
int ifcCount = readU2();
parseInterfaces(reader, ifcCount);
// fields
int fieldCount = readU2();
parseFields(reader, fieldCount);
// methods
int methodCount = readU2();
parseMethods(reader, methodCount);
// class attributes
int classAttrCount = readU2();
parseClassAttributes(reader, classAttrCount);
} catch (BailOut x){
// nothing, just a control exception to shortcut the classfile parsing
}
}
//--- constpool parsing
public static String readModifiedUTF8String( byte[] data, int pos, int len) throws ClassParseException {
int n = 0; // the number of chars in buf
char[] buf = new char[len]; // it can't be more, but it can be less chars
// \u0001 - \u007f : single byte chars: 0xxxxxxx
// \u0000 and \u0080 - \u07ff : double byte chars: 110xxxxx, 10xxxxxx
// \u0800 - \uffff : tripple byte chars: 1110xxxx, 10xxxxxx, 10xxxxxx
int max = pos+len;
for (int i=pos; i<max; i++){
int c = data[i] & 0xff;
if ((c & 0x80) == 0){ // single byte char 0xxxxxxx
buf[n++] = (char)c;
} else {
if ((c & 0x40) != 0){ // 11xxxxxx
// for the sake of efficiency, we don't check for the trailing zero bit in the marker,
// we just mask it out
if ((c & 0x20) == 0) { // 110xxxxx - double byte char
buf[n++] = (char) (((c & 0x1f) << 6) | (data[++i] & 0x3f));
} else { // 1110xxxx - tripple byte char
buf[n++] = (char) (((c & 0x0f) << 12) | ((data[++i] & 0x3f) << 6) | (data[++i] & 0x3f));
}
} else {
throw new ClassParseException("malformed modified UTF-8 input: ");
}
}
}
return new String(buf, 0, n);
}
// the protected methods are called automatically, the public parse..Attr() methods
// are called optionally from the corresponding ClassFileReader.set..Attribute() method.
// Note that these calls have to provide the ClassFileReader as an argument because
// we might actually switch to another reader (e.g. MethodInfos for parseCodeAttr)
protected void parseCp(int cpCount) throws ClassParseException {
int j = pos;
byte[] data = this.data;
int[] dataIdx = this.cpPos;
Object[] values = this.cpValue;
//--- first pass: store data index values and convert non-delegating constant values
// cp_entry[0] is traditionally unused
for (int i=1; i<cpCount; i++) {
switch (data[j]){
case 0:
error("illegal constpool tag");
case CONSTANT_UTF8: // utf8_info { u1 tag; u2 length; u1 bytes[length]; }
dataIdx[i] = j++;
int len = ((data[j++]&0xff) <<8) | (data[j++]&0xff);
String s = readModifiedUTF8String( data, j, len);
values[i] = s;
j += len;
break;
case 2:
error("illegal constpool tag");
case CONSTANT_INTEGER: // Integer_info { u1 tag; u4 bytes; }
dataIdx[i] = j++;
int iVal = (data[j++]&0xff)<<24 | (data[j++]&0xff)<<16 | (data[j++]&0xff)<<8 | (data[j++]&0xff);
values[i] = new Integer(iVal);
break;
case CONSTANT_FLOAT: // Float_info { u1 tag; u4 bytes; }
dataIdx[i] = j++;
int iBits = (data[j++]&0xff)<<24 | (data[j++]&0xff)<<16 | (data[j++]&0xff)<<8 | (data[j++]&0xff);
float fVal = Float.intBitsToFloat(iBits);
values[i] = new Float(fVal);
break;
case CONSTANT_LONG: // Long_info { u1 tag; u4 high_bytes; u4 low_bytes; }
dataIdx[i] = j++;
long lVal = (data[j++]&0xffL)<<56 | (data[j++]&0xffL)<<48 | (data[j++]&0xffL)<<40 | (data[j++]&0xffL)<<32
| (data[j++]&0xffL)<<24 | (data[j++]&0xffL)<<16 | (data[j++]&0xffL)<<8 | (data[j++]&0xffL);
values[i] = new Long(lVal);
dataIdx[++i] = -1; // 8 byte cpValue occupy 2 index slots
break;
case CONSTANT_DOUBLE: // Double_info { u1 tag; u4 high_bytes; u4 low_bytes; }
dataIdx[i] = j++;
long lBits = (data[j++]&0xffL)<<56 | (data[j++]&0xffL)<<48 | (data[j++]&0xffL)<<40 | (data[j++]&0xffL)<<32
| (data[j++]&0xffL)<<24 | (data[j++]&0xffL)<<16 | (data[j++]&0xffL)<<8 | (data[j++]&0xffL);
double dVal = Double.longBitsToDouble(lBits);
values[i] = new Double(dVal);
dataIdx[++i] = -1; // 8 byte cpValue occupy 2 index slots
break;
case CONSTANT_CLASS: // Class_info { u1 tag; u2 name_index<utf8>; }
dataIdx[i] = j;
values[i] = CpInfo.ConstantClass;
j += 3;
break;
case CONSTANT_STRING: // String_info { u1 tag; u2 string_index<utf8>; }
dataIdx[i] = j;
values[i] = CpInfo.ConstantString;
j += 3;