2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.jvm;
21 import gov.nasa.jpf.jvm.JVMByteCodeReader;
22 import gov.nasa.jpf.vm.ClassParseException;
23 import gov.nasa.jpf.JPFException;
24 import gov.nasa.jpf.util.BailOut;
25 import gov.nasa.jpf.util.BinaryClassSource;
30 * class to read and dissect Java classfile contents (as specified by the Java VM
31 * spec http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#16628
33 public class ClassFile extends BinaryClassSource {
35 public static final int CONSTANT_UTF8 = 1;
36 public static final int CONSTANT_INTEGER = 3;
37 public static final int CONSTANT_FLOAT = 4;
38 public static final int CONSTANT_LONG = 5;
39 public static final int CONSTANT_DOUBLE = 6;
40 public static final int CONSTANT_CLASS = 7;
41 public static final int CONSTANT_STRING = 8;
42 public static final int FIELD_REF = 9;
43 public static final int METHOD_REF = 10;
44 public static final int INTERFACE_METHOD_REF = 11;
45 public static final int NAME_AND_TYPE = 12;
46 public static final int METHOD_HANDLE = 15;
47 public static final int METHOD_TYPE = 16;
48 public static final int INVOKE_DYNAMIC = 18;
50 public static final int REF_GETFIELD = 1;
51 public static final int REF_GETSTATIC = 2;
52 public static final int REF_PUTFIELD = 3;
53 public static final int REF_PUTSTATIC = 4;
54 public static final int REF_INVOKEVIRTUAL = 5;
55 public static final int REF_INVOKESTATIC = 6;
56 public static final int REF_INVOKESPECIAL = 7;
57 public static final int REF_NEW_INVOKESPECIAL = 8;
58 public static final int REF_INVOKEINTERFACE = 9;
60 // used to store types in cpValue[]
61 public static enum CpInfo {
73 InterfaceMethodRef, // 11
83 // <2do> this is going away
84 String requestedTypeName; // the type name that caused this classfile to be loaded
87 int[] cpPos; // cpPos[i] holds data start index for cp_entry i (0 is unused)
88 Object[] cpValue; // cpValue[i] hold the String/Integer/Float/Double associated with corresponding cp_entries
91 public ClassFile (byte[] data, int offset){
95 public ClassFile (byte[] data){
99 public ClassFile (String typeName, byte[] data){
102 this.requestedTypeName = typeName;
105 public ClassFile (String typeName, byte[] data, int offset){
108 this.requestedTypeName = typeName;
111 public ClassFile (File file) throws ClassParseException {
115 public ClassFile (String pathName) throws ClassParseException {
116 super( new File(pathName));
123 * set classfile data. This is mainly provided to allow
124 * on-the-fly classfile instrumentation with 3rd party libraries
126 * BEWARE - like getData(), this method can cause parsing to fail if the
127 * provided data does not conform to the VM specs. In particular, this
128 * method should ONLY be called before executing parse(ClassFileReader) and
129 * will otherwise throw a JPFException
131 public void setData(byte[] newData){
133 throw new JPFException("concurrent modification of ClassFile data");
140 * return the typename this classfile gets loaded for
141 * <2do> this is going away
143 public String getRequestedTypeName(){
144 return requestedTypeName;
148 //--- general attributes
149 public static final String SYNTHETIC_ATTR = "Synthetic";
150 public static final String DEPRECATED_ATTR = "Deprecated";
151 public static final String SIGNATURE_ATTR = "Signature";
152 public static final String RUNTIME_INVISIBLE_ANNOTATIONS_ATTR = "RuntimeInvisibleAnnotations";
153 public static final String RUNTIME_VISIBLE_ANNOTATIONS_ATTR = "RuntimeVisibleAnnotations";
154 public static final String RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR = "RuntimeVisibleTypeAnnotations";
156 //--- standard field attributes
157 public static final String CONST_VALUE_ATTR = "ConstantValue";
159 protected final static String[] stdFieldAttrs = {
160 CONST_VALUE_ATTR, SYNTHETIC_ATTR, DEPRECATED_ATTR, SIGNATURE_ATTR,
161 RUNTIME_INVISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR };
164 //--- standard method attributes
165 public static final String CODE_ATTR = "Code";
166 public static final String EXCEPTIONS_ATTR = "Exceptions";
167 public static final String RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS_ATTR = "RuntimeInvisibleParameterAnnotations";
168 public static final String RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS_ATTR = "RuntimeVisibleParameterAnnotations";
169 public static final String ANNOTATIONDEFAULT_ATTR = "AnnotationDefault";
171 protected final static String[] stdMethodAttrs = {
172 CODE_ATTR, EXCEPTIONS_ATTR, SYNTHETIC_ATTR, DEPRECATED_ATTR, SIGNATURE_ATTR,
173 RUNTIME_INVISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_ANNOTATIONS_ATTR,
174 RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS_ATTR,
175 RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS_ATTR,
176 RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR,
177 ANNOTATIONDEFAULT_ATTR
181 //--- standard code attributes
182 public static final String LINE_NUMBER_TABLE_ATTR = "LineNumberTable";
183 public static final String LOCAL_VAR_TABLE_ATTR = "LocalVariableTable";
185 protected final static String[] stdCodeAttrs = { LINE_NUMBER_TABLE_ATTR, LOCAL_VAR_TABLE_ATTR, RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR };
188 //--- standard class attributes
189 public static final String SOURCE_FILE_ATTR = "SourceFile";
190 public static final String INNER_CLASSES_ATTR = "InnerClasses";
191 public static final String ENCLOSING_METHOD_ATTR = "EnclosingMethod";
192 public static final String BOOTSTRAP_METHOD_ATTR = "BootstrapMethods";
194 protected final static String[] stdClassAttrs = {
195 SOURCE_FILE_ATTR, DEPRECATED_ATTR, INNER_CLASSES_ATTR, DEPRECATED_ATTR, SIGNATURE_ATTR,
196 RUNTIME_INVISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR,
197 ENCLOSING_METHOD_ATTR, BOOTSTRAP_METHOD_ATTR };
200 protected String internStdAttrName(int cpIdx, String name, String[] stdNames){
201 for (int i=0; i<stdNames.length; i++){
202 if (stdNames[i] == name) return name;
204 for (int i=0; i<stdNames.length; i++){
205 String stdName = stdNames[i];
206 if (stdName.equals(name)){
207 cpValue[cpIdx] = stdName;
215 //--- constpool access
217 //--- the primitive info cpValue
218 public String utf8At(int utf8InfoIdx){
219 //assert data[cpPos[utf8InfoIdx]] == 1 : "not a utf8_info tag";
220 return (String) cpValue[utf8InfoIdx];
223 public int intAt(int intInfoIdx){
224 //assert data[cpPos[intInfoIdx]] == 3 : "not a int_info tag";
225 return (Integer) cpValue[intInfoIdx];
228 public float floatAt(int floatInfoIdx){
229 //assert data[cpPos[floatInfoIdx]] == 4 : "not a float_info tag";
230 return (Float) cpValue[floatInfoIdx];
233 public long longAt(int longInfoIdx){
234 //assert data[cpPos[longInfoIdx]] == 5 : "not a long_info tag";
235 return (Long) cpValue[longInfoIdx];
238 public double doubleAt(int doubleInfoIdx){
239 //assert data[cpPos[doubleInfoIdx]] == 6 : "not a double_info tag";
240 return (Double) cpValue[doubleInfoIdx];
243 //--- those two are delegated but resolved
244 public String classNameAt(int classInfoIdx){
245 //assert data[cpPos[classInfoIdx]] == 7 : "not a Class_info tag";
246 return (String) cpValue[classInfoIdx];
249 public String stringAt(int stringInfoIdx){
250 //assert data[cpPos[stringInfoIdx]] == 8 : "not a String_info tag";
251 return (String) cpValue[stringInfoIdx];
254 //--- composite infos
256 // the generic ones (if we don't care what kind of reference type this is)
257 public String refClassNameAt(int cpIdx){
258 return (String) cpValue[ u2(cpPos[cpIdx]+1)];
260 public String refNameAt(int cpIdx){
261 return utf8At( u2( cpPos[ u2(cpPos[cpIdx]+3)]+1));
263 public String refDescriptorAt(int cpIdx){
264 return utf8At( u2( cpPos[ u2(cpPos[cpIdx]+3)]+3));
267 public int mhRefTypeAt (int methodHandleInfoIdx){
268 return u1(cpPos[methodHandleInfoIdx]+1);
270 public int mhMethodRefIndexAt (int methodHandleInfoIdx){
271 return u2(cpPos[methodHandleInfoIdx]+2);
274 // those could check ref types
275 public String fieldClassNameAt(int fieldRefInfoIdx){
276 //assert data[cpPos[fieldRefInfoIdx]] == 9 : "not a Fieldref_info tag";
277 return (String) cpValue[ u2(cpPos[fieldRefInfoIdx]+1)];
279 public String fieldNameAt(int fieldRefInfoIdx){
280 return utf8At( u2( cpPos[ u2(cpPos[fieldRefInfoIdx]+3)]+1));
282 public String fieldDescriptorAt(int fieldRefInfoIdx){
283 return utf8At( u2( cpPos[ u2(cpPos[fieldRefInfoIdx]+3)]+3));
286 public String methodClassNameAt(int methodRefInfoIdx){
287 return (String) cpValue[ u2(cpPos[methodRefInfoIdx]+1)];
289 public String methodNameAt(int methodRefInfoIdx){
290 return utf8At( u2( cpPos[ u2(cpPos[methodRefInfoIdx]+3)]+1));
292 public String methodDescriptorAt(int methodRefInfoIdx){
293 return utf8At( u2( cpPos[ u2(cpPos[methodRefInfoIdx]+3)]+3));
296 public String methodTypeDescriptorAt (int methodTypeInfoIdx){
297 return utf8At( u2(cpPos[methodTypeInfoIdx]+1));
300 public String interfaceMethodClassNameAt(int ifcMethodRefInfoIdx){
301 return (String) cpValue[ u2(cpPos[ifcMethodRefInfoIdx]+1)];
303 public String interfaceMethodNameAt(int ifcMethodRefInfoIdx){
304 return utf8At( u2( cpPos[ u2(cpPos[ifcMethodRefInfoIdx]+3)]+1));
306 public String interfaceMethodDescriptorAt(int ifcMethodRefInfoIdx){
307 return utf8At( u2( cpPos[ u2(cpPos[ifcMethodRefInfoIdx]+3)]+3));
310 public int bootstrapMethodIndex (int cpInvokeDynamicIndex){
311 return u2(cpPos[cpInvokeDynamicIndex]+1);
313 public String samMethodNameAt(int cpInvokeDynamicIndex) {
314 return utf8At( u2( cpPos[ u2(cpPos[cpInvokeDynamicIndex]+3)]+1));
316 public String callSiteDescriptor(int cpInvokeDynamicIndex) {
317 return utf8At( u2( cpPos[ u2(cpPos[cpInvokeDynamicIndex]+3)]+3));
320 public String getRefTypeName (int refCode){
322 case REF_GETFIELD: return "getfield";
323 case REF_GETSTATIC: return "getstatic";
324 case REF_PUTFIELD: return "putfield";
325 case REF_PUTSTATIC: return "putstatic";
326 case REF_INVOKEVIRTUAL: return "invokevirtual";
327 case REF_INVOKESTATIC: return "invokestatic";
328 case REF_INVOKESPECIAL: return "invokespecial";
329 case REF_NEW_INVOKESPECIAL: return "new-invokespecial";
330 case REF_INVOKEINTERFACE: return "invokeinterface";
336 public String getTypeName (int typeCode){
338 case 4: return "boolean";
339 case 5: return "char";
340 case 6: return "float";
341 case 7: return "double";
342 case 8: return "byte";
343 case 9: return "short";
344 case 10: return "int";
345 case 11: return "long";
360 //--- traverse/analyze the const pool (this is rather exotic)
362 public int getNumberOfCpEntries(){
363 return cpValue.length;
366 public Object getCpValue (int i){
370 public int getCpTag (int i){
371 return data[cpPos[i]];
375 * the result can be used as input for u2(dataIndex)
377 * NOTE - this returns -1 for the dreaded unused extra entries associated
378 * with ConstantDouble and ConstantLong
380 public int getDataPosOfCpEntry (int i){
384 //--- standard attributes
386 public Object getConstValueAttribute(int dataPos){
387 int cpIdx = u2(dataPos);
388 Object v = cpValue[cpIdx];
392 public String getSourceFileAttribute(int dataPos){
393 // SourceFile_attribute { u2 attr_name_idx; u4 attr_length; u2 sourcefile_idx<utf8>; }
395 int cpIdx = u2(dataPos + 6);
396 Object v = cpValue[cpIdx];
401 //--- low level readers
403 public final int u1(int dataIdx){
404 return data[dataIdx] & 0xff;
407 public final int u2(int dataIdx){
408 return ((data[dataIdx]&0xff) << 8) | (data[dataIdx+1]&0xff);
411 public final int i1(int dataIdx) {
412 return data[dataIdx++];
415 public final int i2(int dataIdx) {
417 return (data[idx++] << 8) | (data[idx]&0xff);
420 public final int readU2(){
423 return ((data[idx++]&0xff) << 8) | (data[idx]&0xff);
426 public final int readI2() {
429 return (data[idx++] << 8) | (data[idx]&0xff);
432 public final int readI4(){
435 byte[] data = this.data;
437 return (data[idx++] <<24) | ((data[idx++]&0xff) << 16) | ((data[idx++]&0xff) << 8) | (data[idx]&0xff);
441 //--- reader notifications
442 private void setClass(ClassFileReader reader, String clsName, String superClsName, int flags, int cpCount) throws ClassParseException {
444 reader.setClass( this, clsName, superClsName, flags, cpCount);
448 private void setInterfaceCount(ClassFileReader reader, int ifcCount){
450 reader.setInterfaceCount( this, ifcCount);
453 private void setInterface(ClassFileReader reader, int ifcIndex, String ifcName){
455 reader.setInterface( this, ifcIndex, ifcName);
458 private void setInterfacesDone(ClassFileReader reader){
460 reader.setInterfacesDone( this);
465 private void setFieldCount(ClassFileReader reader, int fieldCount){
467 reader.setFieldCount( this, fieldCount);
471 private void setField(ClassFileReader reader, int fieldIndex, int accessFlags, String name, String descriptor){
473 reader.setField( this, fieldIndex, accessFlags, name, descriptor);
476 private void setFieldAttributeCount(ClassFileReader reader, int fieldIndex, int attrCount){
478 reader.setFieldAttributeCount( this, fieldIndex, attrCount);
481 private void setFieldAttribute(ClassFileReader reader, int fieldIndex, int attrIndex, String name, int attrLength){
482 int p = pos + attrLength;
483 reader.setFieldAttribute( this, fieldIndex, attrIndex, name, attrLength);
486 private void setFieldAttributesDone(ClassFileReader reader, int fieldIndex){
488 reader.setFieldAttributesDone( this, fieldIndex);
491 private void setFieldDone(ClassFileReader reader, int fieldIndex){
493 reader.setFieldDone( this, fieldIndex);
496 private void setFieldsDone(ClassFileReader reader){
498 reader.setFieldsDone( this);
501 private void setConstantValue(ClassFileReader reader, Object tag, Object value){
503 reader.setConstantValue( this, tag, value);
507 private void setMethodCount(ClassFileReader reader, int methodCount){
509 reader.setMethodCount( this, methodCount);
512 private void setMethod(ClassFileReader reader, int methodIndex, int accessFlags, String name, String descriptor){
514 reader.setMethod( this, methodIndex, accessFlags, name, descriptor);
517 private void setMethodAttributeCount(ClassFileReader reader, int methodIndex, int attrCount){
519 reader.setMethodAttributeCount( this, methodIndex, attrCount);
522 private void setMethodAttribute(ClassFileReader reader, int methodIndex, int attrIndex, String name, int attrLength){
523 int p = pos + attrLength;
524 reader.setMethodAttribute( this, methodIndex, attrIndex, name, attrLength);
527 private void setMethodAttributesDone(ClassFileReader reader, int methodIndex){
529 reader.setMethodAttributesDone( this, methodIndex);
532 private void setMethodDone(ClassFileReader reader, int methodIndex){
534 reader.setMethodDone( this, methodIndex);
537 private void setMethodsDone(ClassFileReader reader){
539 reader.setMethodsDone( this);
542 private void setExceptionCount(ClassFileReader reader, Object tag, int exceptionCount){
544 reader.setExceptionCount( this, tag, exceptionCount);
547 private void setExceptionsDone(ClassFileReader reader, Object tag){
549 reader.setExceptionsDone( this, tag);
552 private void setException(ClassFileReader reader, Object tag, int exceptionIndex, String exceptionType){
554 reader.setException( this, tag, exceptionIndex, exceptionType);
557 private void setCode(ClassFileReader reader, Object tag, int maxStack, int maxLocals, int codeLength){
558 int p = pos + codeLength;
559 reader.setCode( this, tag, maxStack, maxLocals, codeLength);
562 private void setExceptionTableCount(ClassFileReader reader, Object tag, int exceptionTableCount){
564 reader.setExceptionHandlerTableCount( this, tag, exceptionTableCount);
567 private void setExceptionTableEntry(ClassFileReader reader, Object tag, int exceptionIndex,
568 int startPc, int endPc, int handlerPc, String catchType){
570 reader.setExceptionHandler( this, tag, exceptionIndex, startPc, endPc, handlerPc, catchType);
573 private void setExceptionTableDone(ClassFileReader reader, Object tag){
575 reader.setExceptionHandlerTableDone( this, tag);
579 private void setCodeAttributeCount(ClassFileReader reader, Object tag, int attrCount){
581 reader.setCodeAttributeCount( this, tag, attrCount);
584 private void setCodeAttribute(ClassFileReader reader, Object tag, int attrIndex, String name, int attrLength){
585 int p = pos + attrLength;
586 reader.setCodeAttribute( this, tag, attrIndex, name, attrLength);
589 private void setCodeAttributesDone(ClassFileReader reader, Object tag){
591 reader.setCodeAttributesDone( this, tag);
595 private void setLineNumberTableCount(ClassFileReader reader, Object tag, int lineNumberCount){
597 reader.setLineNumberTableCount( this, tag, lineNumberCount);
600 private void setLineNumber(ClassFileReader reader, Object tag, int lineIndex, int lineNumber, int startPc){
602 reader.setLineNumber( this, tag, lineIndex, lineNumber, startPc);
605 private void setLineNumberTableDone(ClassFileReader reader, Object tag){
607 reader.setLineNumberTableDone( this, tag);
611 private void setLocalVarTableCount(ClassFileReader reader, Object tag, int localVarCount){
613 reader.setLocalVarTableCount( this, tag, localVarCount);
616 private void setLocalVar(ClassFileReader reader, Object tag, int localVarIndex, String varName, String descriptor,
617 int scopeStartPc, int scopeEndPc, int slotIndex){
619 reader.setLocalVar( this, tag, localVarIndex, varName, descriptor, scopeStartPc, scopeEndPc, slotIndex);
622 private void setLocalVarTableDone(ClassFileReader reader, Object tag){
624 reader.setLocalVarTableDone( this, tag);
629 private void setClassAttributeCount(ClassFileReader reader, int attrCount){
631 reader.setClassAttributeCount( this, attrCount);
634 private void setClassAttribute(ClassFileReader reader, int attrIndex, String name, int attrLength){
635 int p = pos + attrLength;
636 reader.setClassAttribute( this, attrIndex, name, attrLength);
639 private void setClassAttributesDone(ClassFileReader reader){
641 reader.setClassAttributesDone(this);
645 private void setSourceFile(ClassFileReader reader, Object tag, String pathName){
647 reader.setSourceFile( this, tag, pathName);
651 private void setBootstrapMethodCount (ClassFileReader reader, Object tag, int bootstrapMethodCount){
653 reader.setBootstrapMethodCount( this, tag, bootstrapMethodCount);
656 private void setBootstrapMethod (ClassFileReader reader, Object tag, int idx,
657 int refKind, String cls, String mth, String descriptor, int[] cpArgs){
659 reader.setBootstrapMethod( this, tag, idx, refKind, cls, mth, descriptor, cpArgs);
662 private void setBootstrapMethodsDone (ClassFileReader reader, Object tag){
664 reader.setBootstrapMethodsDone( this, tag);
668 private void setInnerClassCount(ClassFileReader reader, Object tag, int innerClsCount){
670 reader.setInnerClassCount( this, tag, innerClsCount);
673 private void setInnerClass(ClassFileReader reader, Object tag, int innerClsIndex, String outerName, String innerName,
674 String innerSimpleName, int accessFlags){
676 reader.setInnerClass( this, tag, innerClsIndex, outerName, innerName, innerSimpleName, accessFlags);
679 private void setEnclosingMethod(ClassFileReader reader, Object tag, String enclosingClass, String enclosedMethod, String descriptor){
681 reader.setEnclosingMethod( this, tag, enclosingClass, enclosedMethod, descriptor);
684 private void setInnerClassesDone(ClassFileReader reader, Object tag){
686 reader.setInnerClassesDone(this, tag);
690 private void setAnnotationCount(ClassFileReader reader, Object tag, int annotationCount){
692 reader.setAnnotationCount( this, tag, annotationCount);
695 private void setAnnotation(ClassFileReader reader, Object tag, int annotationIndex, String annotationType){
697 reader.setAnnotation( this, tag, annotationIndex, annotationType);
700 private void setAnnotationsDone(ClassFileReader reader, Object tag){
702 reader.setAnnotationsDone(this, tag);
706 private void setTypeAnnotationCount(ClassFileReader reader, Object tag, int annotationCount){
708 reader.setTypeAnnotationCount( this, tag, annotationCount);
711 private void setTypeAnnotationsDone(ClassFileReader reader, Object tag){
713 reader.setTypeAnnotationsDone(this, tag);
718 private void setAnnotationValueCount(ClassFileReader reader, Object tag, int annotationIndex, int nValuePairs){
720 reader.setAnnotationValueCount( this, tag, annotationIndex, nValuePairs);
723 private void setPrimitiveAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
724 String elementName, int arrayIndex, Object val){
726 reader.setPrimitiveAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
729 private void setStringAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
730 String elementName, int arrayIndex, String s){
732 reader.setStringAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, s);
735 private void setClassAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
736 String elementName, int arrayIndex, String typeName){
738 reader.setClassAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, typeName);
741 private void setEnumAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
742 String elementName, int arrayIndex, String enumType, String enumValue){
744 reader.setEnumAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, enumType, enumValue);
748 private void setAnnotationValueElementCount(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
749 String elementName, int elementCount){
751 reader.setAnnotationValueElementCount(this, tag, annotationIndex, valueIndex, elementName, elementCount);
754 private void setAnnotationValueElementsDone(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
757 reader.setAnnotationValueElementsDone(this, tag, annotationIndex, valueIndex, elementName);
761 public void setAnnotationValuesDone(ClassFileReader reader, Object tag, int annotationIndex){
763 reader.setAnnotationValuesDone(this, tag, annotationIndex);
767 private void setParameterCount(ClassFileReader reader, Object tag, int parameterCount){
769 reader.setParameterCount(this, tag, parameterCount);
772 private void setParameterAnnotationCount(ClassFileReader reader, Object tag, int paramIndex, int annotationCount){
774 reader.setParameterAnnotationCount(this, tag, paramIndex, annotationCount);
777 private void setParameterAnnotation(ClassFileReader reader, Object tag, int annotationIndex, String annotationType){
779 reader.setParameterAnnotation( this, tag, annotationIndex, annotationType);
782 private void setParameterAnnotationsDone(ClassFileReader reader, Object tag, int paramIndex){
784 reader.setParameterAnnotationsDone(this, tag, paramIndex);
787 private void setParametersDone(ClassFileReader reader, Object tag){
789 reader.setParametersDone(this, tag);
793 public void setSignature(ClassFileReader reader, Object tag, String signature){
795 reader.setSignature(this, tag, signature);
802 * this is the main parsing routine that uses the ClassFileReader interface
803 * to tell clients about the classfile contents
805 * ClassFile structure: http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#74353
806 * u4 magic; // 0xcafebabe
810 * u2 constant_pool_count;
811 * cp_entry constant_pool[constant_pool_count-1];
817 * u2 interfaces_count;
818 * u2 interfaces[interfaces_count];
821 * field_info fields[fields_count];
824 * method_info methods[methods_count];
826 * u2 attributes_count;
827 * attribute_info attributes[attributes_count];
829 public void parse( ClassFileReader reader) throws ClassParseException {
834 int magic = readI4();
835 if (magic != 0xCAFEBABE) {
836 error("wrong magic: " + Integer.toHexString(magic));
839 // we don't do much with the version numbers yet
840 int minor = readU2();
841 int major = readU2();
843 // get the const pool
844 int cpCount = readU2();
845 cpPos = new int[cpCount];
846 cpValue = new Object[cpCount];
849 // the class essentials
850 int accessFlags = readU2();
853 String clsName = (String) cpValue[cpIdx];
856 String superClsName = (String) cpValue[cpIdx];
859 setClass(reader, clsName, superClsName, accessFlags, cpCount);
862 int ifcCount = readU2();
863 parseInterfaces(reader, ifcCount);
866 int fieldCount = readU2();
867 parseFields(reader, fieldCount);
870 int methodCount = readU2();
871 parseMethods(reader, methodCount);
874 int classAttrCount = readU2();
875 parseClassAttributes(reader, classAttrCount);
878 // nothing, just a control exception to shortcut the classfile parsing
883 //--- constpool parsing
885 public static String readModifiedUTF8String( byte[] data, int pos, int len) throws ClassParseException {
887 int n = 0; // the number of chars in buf
888 char[] buf = new char[len]; // it can't be more, but it can be less chars
890 // \u0001 - \u007f : single byte chars: 0xxxxxxx
891 // \u0000 and \u0080 - \u07ff : double byte chars: 110xxxxx, 10xxxxxx
892 // \u0800 - \uffff : tripple byte chars: 1110xxxx, 10xxxxxx, 10xxxxxx
895 for (int i=pos; i<max; i++){
896 int c = data[i] & 0xff;
897 if ((c & 0x80) == 0){ // single byte char 0xxxxxxx
901 if ((c & 0x40) != 0){ // 11xxxxxx
903 // for the sake of efficiency, we don't check for the trailing zero bit in the marker,
904 // we just mask it out
905 if ((c & 0x20) == 0) { // 110xxxxx - double byte char
906 buf[n++] = (char) (((c & 0x1f) << 6) | (data[++i] & 0x3f));
908 } else { // 1110xxxx - tripple byte char
909 buf[n++] = (char) (((c & 0x0f) << 12) | ((data[++i] & 0x3f) << 6) | (data[++i] & 0x3f));
913 throw new ClassParseException("malformed modified UTF-8 input: ");
918 return new String(buf, 0, n);
922 // the protected methods are called automatically, the public parse..Attr() methods
923 // are called optionally from the corresponding ClassFileReader.set..Attribute() method.
924 // Note that these calls have to provide the ClassFileReader as an argument because
925 // we might actually switch to another reader (e.g. MethodInfos for parseCodeAttr)
927 protected void parseCp(int cpCount) throws ClassParseException {
930 byte[] data = this.data;
931 int[] dataIdx = this.cpPos;
932 Object[] values = this.cpValue;
934 //--- first pass: store data index values and convert non-delegating constant values
935 // cp_entry[0] is traditionally unused
936 for (int i=1; i<cpCount; i++) {
939 error("illegal constpool tag");
941 case CONSTANT_UTF8: // utf8_info { u1 tag; u2 length; u1 bytes[length]; }
943 int len = ((data[j++]&0xff) <<8) | (data[j++]&0xff);
945 String s = readModifiedUTF8String( data, j, len);
952 error("illegal constpool tag");
954 case CONSTANT_INTEGER: // Integer_info { u1 tag; u4 bytes; }
957 int iVal = (data[j++]&0xff)<<24 | (data[j++]&0xff)<<16 | (data[j++]&0xff)<<8 | (data[j++]&0xff);
958 values[i] = new Integer(iVal);
961 case CONSTANT_FLOAT: // Float_info { u1 tag; u4 bytes; }
964 int iBits = (data[j++]&0xff)<<24 | (data[j++]&0xff)<<16 | (data[j++]&0xff)<<8 | (data[j++]&0xff);
965 float fVal = Float.intBitsToFloat(iBits);
966 values[i] = new Float(fVal);
969 case CONSTANT_LONG: // Long_info { u1 tag; u4 high_bytes; u4 low_bytes; }
971 long lVal = (data[j++]&0xffL)<<56 | (data[j++]&0xffL)<<48 | (data[j++]&0xffL)<<40 | (data[j++]&0xffL)<<32
972 | (data[j++]&0xffL)<<24 | (data[j++]&0xffL)<<16 | (data[j++]&0xffL)<<8 | (data[j++]&0xffL);
973 values[i] = new Long(lVal);
975 dataIdx[++i] = -1; // 8 byte cpValue occupy 2 index slots
978 case CONSTANT_DOUBLE: // Double_info { u1 tag; u4 high_bytes; u4 low_bytes; }
981 long lBits = (data[j++]&0xffL)<<56 | (data[j++]&0xffL)<<48 | (data[j++]&0xffL)<<40 | (data[j++]&0xffL)<<32
982 | (data[j++]&0xffL)<<24 | (data[j++]&0xffL)<<16 | (data[j++]&0xffL)<<8 | (data[j++]&0xffL);
983 double dVal = Double.longBitsToDouble(lBits);
984 values[i] = new Double(dVal);
986 dataIdx[++i] = -1; // 8 byte cpValue occupy 2 index slots
989 case CONSTANT_CLASS: // Class_info { u1 tag; u2 name_index<utf8>; }
991 values[i] = CpInfo.ConstantClass;
996 case CONSTANT_STRING: // String_info { u1 tag; u2 string_index<utf8>; }
998 values[i] = CpInfo.ConstantString;
1003 case FIELD_REF: // Fieldref_info { u1 tag; u2 class_index; u2 name_and_type_index; }
1005 values[i] = CpInfo.FieldRef;
1009 case METHOD_REF: // Methodref_info { u1 tag; u2 class_index; u2 name_and_type_index; }
1011 values[i] = CpInfo.MethodRef;
1015 case INTERFACE_METHOD_REF: // InterfaceMethodref_info { u1 tag; u2 class_index; u2 name_and_type_index; }
1017 values[i] = CpInfo.InterfaceMethodRef;
1021 case NAME_AND_TYPE: // NameAndType_info { u1 tag; u2 name_index<utf8>; u2 descriptor_index<utf8>; }
1023 values[i] = CpInfo.NameAndType;
1028 //--- the Java 8 ones
1030 case METHOD_HANDLE: // MethodHandle_info { u1 tag; u1 reference_kind; u2 reference_index<mthref>; }
1032 values[i] = CpInfo.MethodHandle;
1036 case METHOD_TYPE: // MethodType_info { u1 tag; u2 descriptor_index<utf8>; }
1038 values[i] = CpInfo.MethodType;
1042 case INVOKE_DYNAMIC: // InvokeDynamic_info { u1 tag; u2 bootstrap_method_attr_index; u2 name_and_type_index; }
1044 values[i] = CpInfo.InvokeDynamic;
1049 error("illegal constpool tag: " + data[j]);
1055 //--- second pass: store values of delegating constant values
1056 for (int i=1; i<cpCount; i++){
1057 Object v = cpValue[i];
1059 // we store string and class constants as their utf8 string values
1060 if (v == CpInfo.ConstantClass || v == CpInfo.ConstantString){
1061 cpValue[i] = cpValue[u2(cpPos[i]+1)];
1066 protected void parseInterfaces(ClassFileReader reader, int ifcCount){
1068 setInterfaceCount(reader, ifcCount);
1070 for (int i=0; i<ifcCount; i++){
1071 int cpIdx = readU2();
1072 setInterface(reader, i, classNameAt(cpIdx));
1075 setInterfacesDone(reader);
1079 protected void parseFields(ClassFileReader reader, int fieldCount) {
1081 setFieldCount(reader, fieldCount);
1083 for (int i=0; i<fieldCount; i++){
1084 int accessFlags = readU2();
1086 int cpIdx = readU2();
1087 String name = utf8At(cpIdx);
1090 String descriptor = utf8At(cpIdx);
1092 setField(reader, i, accessFlags, name, descriptor);
1094 int attrCount = readU2();
1095 parseFieldAttributes(reader, i, attrCount);
1097 setFieldDone(reader, i);
1100 setFieldsDone(reader);
1103 protected void parseFieldAttributes(ClassFileReader reader, int fieldIdx, int attrCount){
1104 setFieldAttributeCount(reader, fieldIdx, attrCount);
1106 for (int i=0; i<attrCount; i++){
1107 int cpIdx = readU2();
1108 String name = utf8At(cpIdx);
1110 name = internStdAttrName(cpIdx, name, stdFieldAttrs);
1112 int attrLength = readI4(); // actually U4, but we don't support 2GB attributes
1113 setFieldAttribute(reader, fieldIdx, i, name, attrLength);
1116 setFieldAttributesDone(reader, fieldIdx);
1120 * optionally called by reader to obtain a ConstantValue field attribute
1122 * ConstantValue {u2 attrName<utf8>; u4 attrLength; u2 constIndex<class|string|int|float|long|double> }
1124 * pos is at constIndex
1126 public void parseConstValueAttr(ClassFileReader reader, Object tag){
1127 int cpIdx = readU2();
1128 setConstantValue(reader, tag, cpValue[cpIdx]);
1133 protected void parseMethods(ClassFileReader reader, int methodCount) {
1135 setMethodCount(reader, methodCount);
1137 for (int i=0; i<methodCount; i++){
1138 int accessFlags = readU2();
1140 int cpIdx = readU2();
1141 String name = utf8At(cpIdx);
1144 String descriptor = utf8At(cpIdx);
1146 setMethod(reader, i, accessFlags, name, descriptor);
1148 int attrCount = readU2();
1149 parseMethodAttributes(reader, i, attrCount);
1151 setMethodDone(reader, i);
1154 setMethodsDone(reader);
1157 protected void parseMethodAttributes(ClassFileReader reader, int methodIdx, int attrCount){
1158 setMethodAttributeCount(reader, methodIdx, attrCount);
1160 for (int i=0; i<attrCount; i++){
1161 int cpIdx = readU2();
1162 String name = utf8At(cpIdx);
1164 name = internStdAttrName(cpIdx, name, stdMethodAttrs);
1166 int attrLength = readI4(); // actually U4, but we don't support 2GB attributes
1167 setMethodAttribute(reader, methodIdx, i, name, attrLength);
1170 setMethodAttributesDone(reader, methodIdx);
1173 public void parseExceptionAttr (ClassFileReader reader, Object tag){
1174 int exceptionCount = readU2();
1175 setExceptionCount(reader, tag, exceptionCount);
1177 for (int i=0; i<exceptionCount; i++){
1178 int cpIdx = readU2();
1179 String exceptionType = classNameAt(cpIdx);
1180 setException(reader, tag, i, exceptionType);
1183 setExceptionsDone(reader, tag);
1187 * (optionally) called by reader from within the setMethodAttribute() notification
1188 * This means we have recursive notification since this is a variable length
1189 * attribute that has variable length attributes
1191 * Code_attribute { u2 attr_name_index<utf8>; u4 attr_length;
1192 * u2 max_stack; u2 max_locals;
1193 * u4 code_length; u1 code[code_length];
1194 * u2 exception_table_length;
1195 * { u2 start_pc; u2 end_pc; u2 handler_pc; u2 catch_type<class_entry>;
1196 * } exception_table[exception_table_length];
1197 * u2 attributes_count;
1198 * attribute_info attributes[attributes_count]; }
1200 * pos is at max_stack
1202 public void parseCodeAttr (ClassFileReader reader, Object tag){
1203 int maxStack = readU2();
1204 int maxLocals = readU2();
1205 int codeLength = readI4(); // no code length > 2GB supported
1206 int codeStartPos = pos;
1208 setCode(reader, tag, maxStack, maxLocals, codeLength);
1210 int exceptionCount = readU2();
1211 setExceptionTableCount(reader, tag, exceptionCount);
1213 for (int i = 0; i < exceptionCount; i++) {
1214 int startPc = readU2();
1215 int endPc = readU2();
1216 int handlerPc = readU2();
1218 int cpIdx = readU2();
1219 String catchType = (String) cpValue[cpIdx]; // a Constant_class
1221 setExceptionTableEntry(reader, tag, i, startPc, endPc, handlerPc, catchType);
1223 setExceptionTableDone(reader, tag);
1225 int attrCount = readU2();
1226 parseCodeAttrAttributes(reader, tag, attrCount);
1230 protected void parseCodeAttrAttributes(ClassFileReader reader, Object tag, int attrCount){
1232 setCodeAttributeCount(reader, tag, attrCount);
1234 for (int i=0; i<attrCount; i++){
1235 int cpIdx = readU2();
1236 String name = utf8At(cpIdx);
1238 name = internStdAttrName(cpIdx, name, stdCodeAttrs);
1240 int attrLength = readI4(); // actually U4, but we don't support 2GB attributes
1241 setCodeAttribute(reader, tag, i, name, attrLength);
1244 setCodeAttributesDone(reader, tag);
1248 * optionally called from ClassFileReader.setCodeAttribute() to parse LineNumberTables
1249 * LineNumberTable { u2 attrName; u4 attrLength;
1251 * { u2 startPc; u2 lineNumber; } [lineCount] };
1252 * pos is at lineCount
1254 public void parseLineNumberTableAttr(ClassFileReader reader, Object tag){
1255 int lineCount = readU2();
1256 setLineNumberTableCount(reader, tag, lineCount);
1258 for (int i=0; i<lineCount; i++){
1259 int startPc = readU2();
1260 int lineNumber = readU2();
1261 setLineNumber(reader, tag, i, lineNumber, startPc);
1264 setLineNumberTableDone(reader, tag);
1269 * optionally called from ClassFileReader.setCodeAttribute() to parse LocalVarTables
1270 * LocalVarTableTable { u2 attrName; u4 attrLength;
1272 * { u2 startPc; u2 lineNumber; } [lineCount] };
1273 * pos is at localVarCount
1275 public void parseLocalVarTableAttr(ClassFileReader reader, Object tag){
1276 int localVarCount = readU2();
1277 setLocalVarTableCount(reader, tag, localVarCount);
1279 for (int i=0; i<localVarCount; i++){
1280 int startPc = readU2();
1281 int length = readU2();
1282 int cpIdx = readU2();
1283 String varName = (String) cpValue[cpIdx];
1285 String descriptor = (String) cpValue[cpIdx];
1286 int slotIndex = readU2();
1288 setLocalVar(reader, tag, i, varName, descriptor, startPc, startPc+length-1, slotIndex );
1291 setLocalVarTableDone(reader, tag);
1295 protected void parseClassAttributes(ClassFileReader reader, int attrCount){
1297 setClassAttributeCount(reader, attrCount);
1299 for (int i=0; i<attrCount; i++){
1300 int cpIdx = readU2();
1301 String name = utf8At(cpIdx);
1303 name = internStdAttrName(cpIdx, name, stdClassAttrs);
1305 int attrLength = readI4(); // actually U4, but we don't support 2GB attributes
1306 setClassAttribute(reader, i, name, attrLength);
1309 setClassAttributesDone(reader);
1314 * (optionally) called by ClassFileReader from within setClassAttribute() notification
1316 * InnerClass { u2 nameIdx<utf8>; u4 length; u2 sourceFile<utf8>; }
1318 public void parseSourceFileAttr(ClassFileReader reader, Object tag){
1319 int cpIdx = readU2();
1320 String pathName = utf8At(cpIdx);
1321 setSourceFile(reader, tag, pathName);
1325 * (optionally) called by ClassFileReader from within setClassAttribute() notification
1331 * { u2 innerCls<cls>;
1333 * u2 innerName<utf8>;
1334 * u2 innerAccessFlags;
1335 * } classes[classCount] }
1338 * pos is at classCount
1340 public void parseInnerClassesAttr(ClassFileReader reader, Object tag){
1341 int innerClsCount = readU2();
1342 setInnerClassCount(reader, tag, innerClsCount);
1344 for (int i = 0; i < innerClsCount; i++) {
1345 int cpIdx = readU2();
1346 String innerClsName = (cpIdx != 0) ? (String) cpValue[cpIdx] : null;
1348 String outerClsName = (cpIdx != 0) ? (String) cpValue[cpIdx] : null;
1350 String innerSimpleName = (cpIdx != 0) ? (String) cpValue[cpIdx] : null;
1351 int accessFlags = readU2();
1353 setInnerClass(reader, tag, i, outerClsName, innerClsName, innerSimpleName, accessFlags);
1356 setInnerClassesDone(reader, tag);
1360 * EnclosingMethod_attribute {
1361 * u2 attribute_name_index;
1362 * u4 attribute_length;
1363 * u2 class_index -> Class_info { u1 tag; u2 name_index->utf8 }
1364 * u2 method_index -> NameAndType_info { u1 tag; u2 name_index->utf8; u2 descriptor_index->utf8 }
1367 public void parseEnclosingMethodAttr(ClassFileReader reader, Object tag){
1368 String enclosedMethod = null;
1369 String descriptor = null;
1371 int cpIdx = readU2(); // start of Class_info
1372 String enclosingClass = nameAt(cpIdx);
1374 cpIdx = readU2(); // start of NameAndType_info
1376 // check if this is inside a method - we also get EnclosingMethod_infos for
1377 // classes that are not immediately enclosed
1379 enclosedMethod = nameAt(cpIdx);
1380 descriptor = descriptorAt(cpIdx);
1383 setEnclosingMethod(reader, tag, enclosingClass, enclosedMethod, descriptor);
1387 * BootstrapMethods_attribute {
1388 * u2 attribute_name_index;
1389 * u4 attribute_length;
1390 * u2 num_bootstrap_methods;
1391 * { u2 bootstrap_method_ref; -> MethodHandle
1392 * u2 num_bootstrap_arguments;
1393 * u2 bootstrap_arguments[num_bootstrap_arguments];
1394 * } bootstrap_methods[num_bootstrap_methods];
1397 * pos is at num_bootstrap_methods
1399 public void parseBootstrapMethodAttr (ClassFileReader reader, Object tag){
1400 int nBootstrapMethods = readU2();
1402 setBootstrapMethodCount(reader, tag, nBootstrapMethods);
1404 for (int i=0; i<nBootstrapMethods; i++){
1405 int cpMhIdx = readU2();
1406 int nArgs = readU2();
1407 int[] bmArgs = new int[nArgs];
1408 for (int j=0; j<nArgs; j++){
1409 bmArgs[j] = readU2();
1412 // kind of this method handle
1413 int refKind = mhRefTypeAt(cpMhIdx);
1415 // CONSTANT_Methodref_info structure
1416 int mrefIdx = mhMethodRefIndexAt(cpMhIdx);
1418 String clsName = methodClassNameAt(mrefIdx);
1419 String mthName = methodNameAt(mrefIdx);
1420 String descriptor = methodDescriptorAt(mrefIdx);
1422 setBootstrapMethod(reader, tag, i, refKind, clsName, mthName, descriptor, bmArgs);
1425 setBootstrapMethodsDone( reader, tag);
1428 String nameAt(int nameTypeInfoIdx) {
1429 return utf8At(u2(cpPos[nameTypeInfoIdx] + 1));
1432 String descriptorAt (int nameTypeInfoIdx){
1433 return utf8At( u2( cpPos[nameTypeInfoIdx]+3));
1436 // those are as per http://java.sun.com/docs/books/jvms/second_edition/ClassFileFormat-Java5.pdf
1442 * u2 const_value_index;
1443 * { u2 type_name_index; u2 const_name_index; } enum_const_value;
1444 * u2 class_info_index;
1445 * annotation annotation_value;
1446 * { u2 num_values; element_value values[num_values]; } array_value;
1449 * valid tags are primitve type codes B,C,D,F,I,J,S,Z
1450 * plus: 's'=String, 'e'=enum, 'c'=class, '@'=annotation, '['=array
1452 void parseAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex, String elementName, int arrayIndex){
1456 int t = readUByte();
1459 // booleans have to be treated differently since there is no CONSTANT_Boolean, i.e. values are
1460 // stored as CONSTANT_Integer in the constpool, i.e. the cpValue doesn't have the right type
1462 val = cpValue[cpIdx];
1463 val = Boolean.valueOf((Integer)val == 1);
1464 setPrimitiveAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
1469 val = cpValue[cpIdx];
1470 val = Byte.valueOf(((Integer)val).byteValue());
1471 setPrimitiveAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
1476 val = cpValue[cpIdx];
1477 val = Character.valueOf((char)((Integer)val).shortValue());
1478 setPrimitiveAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
1483 val = cpValue[cpIdx];
1484 val = Short.valueOf(((Integer)val).shortValue());
1485 setPrimitiveAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
1493 val = cpValue[cpIdx];
1494 setPrimitiveAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
1499 String s = (String) cpValue[cpIdx];
1500 setStringAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, s);
1505 String enumTypeName = (String)cpValue[cpIdx];
1507 String enumConstName = (String)cpValue[cpIdx];
1508 setEnumAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, enumTypeName, enumConstName);
1513 String className = (String)cpValue[cpIdx];
1514 setClassAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, className);
1518 parseAnnotation(reader, tag, 0, false); // getting recursive here
1522 int arrayLen = readU2();
1523 setAnnotationValueElementCount(reader, tag, annotationIndex, valueIndex, elementName, arrayLen);
1524 for (int i=0; i<arrayLen; i++){
1525 parseAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, i);
1527 setAnnotationValueElementsDone(reader, tag, annotationIndex, valueIndex, elementName);
1535 * u2 num_element_value_pairs;
1537 * u2 element_name_index;
1538 * element_value value;
1539 * } element_value_pairs[num_element_value_pairs]
1542 void parseAnnotation (ClassFileReader reader, Object tag, int annotationIndex, boolean isParameterAnnotation){
1543 int cpIdx = readU2();
1544 String annotationType = (String)cpValue[cpIdx];
1546 if (isParameterAnnotation){
1547 setParameterAnnotation(reader, tag, annotationIndex, annotationType);
1549 setAnnotation(reader, tag, annotationIndex, annotationType);
1552 parseAnnotationValues(reader, tag, annotationIndex);
1555 void parseAnnotationValues (ClassFileReader reader, Object tag, int annotationIndex){
1556 int nValuePairs = readU2();
1557 setAnnotationValueCount(reader, tag, annotationIndex, nValuePairs);
1559 for (int i=0; i<nValuePairs; i++){
1560 int cpIdx = readU2();
1561 String elementName = (String)cpValue[cpIdx];
1562 parseAnnotationValue(reader, tag, annotationIndex, i, elementName, -1);
1565 setAnnotationValuesDone(reader, tag, annotationIndex);
1569 * class, field, method annotation attributes (only one per target)
1571 * Runtime[In]VisibleAnnotations_attribute {
1572 * u2 attribute_name_index;
1573 * u4 attribute_length;
1574 * u2 num_annotations; << pos
1575 * annotation annotations[num_annotations];
1578 public void parseAnnotationsAttr (ClassFileReader reader, Object tag){
1579 int numAnnotations = readU2();
1580 setAnnotationCount(reader, tag, numAnnotations);
1582 for (int i=0; i<numAnnotations; i++){
1583 parseAnnotation(reader, tag, i, false);
1586 setAnnotationsDone(reader, tag);
1590 // JSR 308 type annotation target types
1591 public static final int CLASS_TYPE_PARAMETER = 0x00;
1592 public static final int METHOD_TYPE_PARAMETER = 0x01;
1593 public static final int CLASS_EXTENDS = 0x10;
1594 public static final int CLASS_TYPE_PARAMETER_BOUND = 0x11;
1595 public static final int METHOD_TYPE_PARAMETER_BOUND = 0x12;
1596 public static final int FIELD = 0x13;
1597 public static final int METHOD_RETURN = 0x14;
1598 public static final int METHOD_RECEIVER = 0x15;
1599 public static final int METHOD_FORMAL_PARAMETER = 0x16;
1600 public static final int THROWS = 0x17;
1601 public static final int LOCAL_VARIABLE = 0x40;
1602 public static final int RESOURCE_VARIABLE = 0x41;
1603 public static final int EXCEPTION_PARAMETER = 0x42;
1604 public static final int INSTANCEOF = 0x43;
1605 public static final int NEW = 0x44;
1606 public static final int CONSTRUCTOR_REFERENCE = 0x45;
1607 public static final int METHOD_REFERENCE = 0x46;
1608 public static final int CAST = 0x47;
1609 public static final int CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT = 0x48;
1610 public static final int METHOD_INVOCATION_TYPE_ARGUMENT = 0x49;
1611 public static final int CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT = 0x4a;
1612 public static final int METHOD_REFERENCE_TYPE_ARGUMENT = 0x4b;
1614 public static String getTargetTypeName (int targetType){
1615 switch (targetType){
1616 case CLASS_TYPE_PARAMETER: return "class type parameter";
1617 case METHOD_TYPE_PARAMETER: return "method type parameter";
1618 case CLASS_EXTENDS: return "super class";
1619 case CLASS_TYPE_PARAMETER_BOUND: return "class type parameter bound";
1620 case METHOD_TYPE_PARAMETER_BOUND: return "method type parameter bound";
1621 case FIELD: return "field";
1622 case METHOD_RETURN: return "method return";
1623 case METHOD_RECEIVER: return "method receiver";
1624 case METHOD_FORMAL_PARAMETER: return "method formal parameter";
1625 case THROWS: return "throws";
1626 case LOCAL_VARIABLE: return "local variable";
1627 case RESOURCE_VARIABLE: return "resource variable";
1628 case EXCEPTION_PARAMETER: return "exception parameter";
1629 case INSTANCEOF: return "instanceof";
1630 case NEW: return "new";
1631 case CONSTRUCTOR_REFERENCE: return "ctor reference";
1632 case METHOD_REFERENCE: return "method reference";
1633 case CAST: return "case";
1634 case METHOD_INVOCATION_TYPE_ARGUMENT: return "method invocation type argument";
1635 case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT: return "ctor reference type argument";
1636 case METHOD_REFERENCE_TYPE_ARGUMENT: return "method reference type argument";
1638 return "<unknown target type 0x" + Integer.toHexString(targetType);
1642 public static String getTypePathEncoding (short[] typePath){
1643 if (typePath == null){
1647 StringBuffer sb = new StringBuffer();
1648 for (int i=0; i<typePath.length;i++){
1649 int e = typePath[i];
1651 sb.append( Integer.toString((e>>8) & 0xff));
1652 sb.append( Integer.toString(e & 0xff));
1656 return sb.toString();
1659 public static String getScopeEncoding (long[] scopeEntries){
1660 StringBuffer sb = new StringBuffer();
1661 for (int i=0; i<scopeEntries.length;i++){
1662 long e = scopeEntries[i];
1663 int slotIndex = (int)(e & 0xffff);
1664 int length = (int)((e >> 16) & 0xffff);
1665 int startPc = (int)((e >> 32) & 0xffff);
1672 sb.append( Integer.toString(startPc));
1674 sb.append( Integer.toString(startPc + length-1));
1676 sb.append(slotIndex);
1679 return sb.toString();
1682 // JSR 308 type annotation, which adds 3 fields to the old annotation structure
1684 // type_annotation {
1685 // u1 target_type; // targeted program element (sec 3.2)
1686 // union { // ?? this is probably packed - variable size unions make no sense
1687 // type_parameter_target;
1688 // supertype_target;
1689 // type_parameter_bound_target;
1691 // method_formal_parameter_target;
1696 // type_argument_target;
1697 // } target_info; // targeted program element (sec 3.3)
1699 // type_path target_path; // encoding of annotation position in compound type (array, generic, etc., sec 3.4)
1701 // // standard annotation fields
1702 // u2 type_index; // the annotation type
1703 // u2 num_element_value_pairs;
1705 // u2 element_name_index;
1706 // element_value value;
1707 // } element_value_pairs[num_element_value_pairs];
1710 // struct type_path {
1712 // type_path_entry path[path_length];
1715 // struct type_path_entry {
1716 // u1 type_path_kind;
1717 // // 0: deeper in array type
1718 // // 1: deeper in nested type
1719 // // 2: bound of wildcard typearg
1720 // // 3: type argument of parameterized type
1721 // u1 type_argument_index;
1722 // // 0, if type_path_kind == 0,1,2
1723 // // 0-based index of type arg in parameterized type if type_path_kind i== 3
1726 int getTargetInfoSize (int targetType){
1727 int len = 3; // max static length are xx_TYPE_ARGUMENTs
1728 if (targetType == LOCAL_VARIABLE || targetType == RESOURCE_VARIABLE){
1729 len = Math.max( len, u2(pos) * 6); // three u2 values per entry
1735 int getTypePathSize (short[] typePath){
1736 int typePathSize = 1;
1737 if (typePath != null) {
1738 typePathSize += typePath.length * 2;
1740 return typePathSize;
1744 short[] readTypePath (){
1745 short[] typePath = null;
1747 int pathLength = readUByte();
1748 if (pathLength > 0){
1749 typePath = new short[pathLength];
1750 for (int i=0; i<pathLength; i++){
1751 int pathKind = (short)readUByte();
1752 int argIdx = (short)readUByte();
1753 typePath[i]= (short)((pathKind << 8) | argIdx);
1760 String readAnnotationType (){
1761 int cpIdx = readU2();
1762 String annotationType = (String)cpValue[cpIdx];
1763 return annotationType;
1766 void setTypeAnnotation (ClassFileReader reader, Object tag, int annotationIndex) {
1767 int targetType = readUByte();
1769 switch (targetType){
1770 case CLASS_TYPE_PARAMETER:
1771 case METHOD_TYPE_PARAMETER: {
1772 // type_parameter_target { u1 type_parameter_index; }
1773 int typeParamIdx = readUByte();
1774 reader.setTypeParameterAnnotation( this, tag, annotationIndex, targetType, typeParamIdx, readTypePath(), readAnnotationType());
1777 case CLASS_EXTENDS: {
1778 // supertype_target { u2 supertype_index; }
1779 int superTypeIdx = readU2();
1780 reader.setSuperTypeAnnotation( this, tag, annotationIndex, targetType, superTypeIdx, readTypePath(), readAnnotationType());
1783 case CLASS_TYPE_PARAMETER_BOUND:
1784 case METHOD_TYPE_PARAMETER_BOUND: {
1785 // type_parameter_bound_target { u1 type_parameter_index; u1 bound_index; }
1786 int typeParamIdx = readUByte();
1787 int boundIdx = readUByte();
1788 reader.setTypeParameterBoundAnnotation(this, tag, annotationIndex, targetType, typeParamIdx, boundIdx, readTypePath(), readAnnotationType());
1792 case METHOD_RECEIVER:
1795 reader.setTypeAnnotation( this, tag, annotationIndex, targetType, readTypePath(), readAnnotationType());
1798 case METHOD_FORMAL_PARAMETER: {
1799 // method_formal_parameter_target { u1 method_formal_parameter_index; }
1800 int formalParamIdx = readUByte();
1801 reader.setFormalParameterAnnotation( this, tag, annotationIndex, targetType, formalParamIdx, readTypePath(), readAnnotationType());
1805 // throws_target { u2 throws_type_index; }
1806 int throwsTypeIdx = readU2();
1807 reader.setThrowsAnnotation( this, tag, annotationIndex, targetType, throwsTypeIdx, readTypePath(), readAnnotationType());
1810 case LOCAL_VARIABLE:
1811 case RESOURCE_VARIABLE: {
1812 // this can't just refer to a LocalVarInfo since those depend on debug compile options
1814 // localvar_target {
1815 // u2 table_length; // number of entries, not bytes
1818 // u2 length; // bytecode offset length
1819 // u2 index; // local var idx
1820 // } table[table_length];
1822 int tableLength = readU2();
1823 long[] scopeEntries = new long[tableLength];
1824 for (int i=0; i<tableLength; i++){
1825 int startPc = readU2();
1826 int length = readU2();
1827 int slotIdx = readU2();
1828 scopeEntries[i] = ((long)startPc << 32) | ((long)length << 16) | slotIdx;
1830 reader.setVariableAnnotation( this, tag, annotationIndex, targetType, scopeEntries, readTypePath(), readAnnotationType());
1833 case EXCEPTION_PARAMETER: {
1834 // catch_target { u2 exception_table_index; }
1835 int exceptionIdx = readU2();
1836 reader.setExceptionParameterAnnotation( this, tag, annotationIndex, targetType, exceptionIdx, readTypePath(), readAnnotationType());
1840 case METHOD_REFERENCE:
1841 case CONSTRUCTOR_REFERENCE:
1843 // offset_target { u2 offset; } // insn offset within bytecode
1844 int offset = readU2();
1845 reader.setBytecodeAnnotation(this, tag, annotationIndex, targetType, offset, readTypePath(), readAnnotationType());
1849 case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
1850 case METHOD_INVOCATION_TYPE_ARGUMENT:
1851 case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
1852 case METHOD_REFERENCE_TYPE_ARGUMENT: {
1853 // type_argument_target {
1855 // u1 type_argument_index;
1857 int offset = readU2();
1858 int typeArgIdx = readUByte();
1859 reader.setBytecodeTypeParameterAnnotation(this, tag, annotationIndex, targetType, offset, typeArgIdx, readTypePath(), readAnnotationType());
1864 // <2do - report this to the reader
1865 throw new RuntimeException("unknown type annotation target: 0x" + Integer.toHexString(targetType));
1870 void parseTypeAnnotation (ClassFileReader reader, Object tag, int annotationIndex) {
1872 // this does the respective setXTypeAnnotation() reader callback
1873 //dumpData(pos, 16);
1874 setTypeAnnotation(reader, tag, annotationIndex);
1876 // now set the annotation value pairs
1877 parseAnnotationValues( reader, tag, annotationIndex);
1881 * Runtime[In]VisibleTypeAnnotations_attribute {
1882 * u2 attribute_name_index;
1883 * u4 attribute_length;
1884 * u2 num_annotations;
1885 * type_annotation annotations[num_annotations];
1888 public void parseTypeAnnotationsAttr (ClassFileReader reader, Object tag) {
1889 int numAnnotations = readU2();
1890 setTypeAnnotationCount(reader, tag, numAnnotations);
1892 for (int i=0; i<numAnnotations; i++){
1893 parseTypeAnnotation(reader, tag, i);
1896 setTypeAnnotationsDone(reader, tag);
1900 * RuntimeInvisibleParameterAnnotations_attribute {
1901 * u2 attribute_name_index;
1902 * u4 attribute_length;
1903 * u1 num_parameters; << pos
1905 * u2 num_annotations;
1906 * annotation annotations[num_annotations];
1907 * } parameter_annotations[num_parameters];
1910 public void parseParameterAnnotationsAttr(ClassFileReader reader, Object tag){
1911 int numParameters = readUByte();
1912 setParameterCount(reader, tag, numParameters);
1913 for (int i=0; i<numParameters; i++){
1914 int numAnnotations = readU2();
1916 setParameterAnnotationCount(reader, tag, i, numAnnotations);
1917 for (int j=0; j<numAnnotations; j++){
1918 parseAnnotation(reader, tag, j, true);
1920 setParameterAnnotationsDone(reader, tag, i);
1922 setParametersDone(reader, tag);
1926 * Signature_attribute {
1927 * u2 attribute_name_index;
1929 * u2 signature-index << pos
1932 public void parseSignatureAttr(ClassFileReader reader, Object tag){
1933 int cpIdx = readU2();
1934 setSignature(reader, tag, utf8At(cpIdx));
1939 * AnnotationDefault_attribute {
1940 * u2 attribute_name_index;
1941 * u4 attribute_length;
1942 * element_value default_value; << pos
1945 public void parseAnnotationDefaultAttr(ClassFileReader reader, Object tag){
1946 parseAnnotationValue(reader, tag, -1, -1, null, -1);
1950 // EnclosingMethod_attribute {
1951 // u2 attribute_name_index;
1952 // u4 attribute_length;
1957 // LocalVariableTypeTable_attribute { // Code attr
1958 // u2 attribute_name_index;
1959 // u4 attribute_length;
1960 // u2 local_variable_type_table_length;
1965 // u2 signature_index;
1967 // } local_variable_type_table[local_variable_type_table_length];
1973 public void parseBytecode(JVMByteCodeReader reader, Object tag, int codeLength){
1980 boolean isWide = false; // modifier for Xload,Xstore,ret and iinc
1983 int endPos = pos+codeLength;
1987 while (pos < endPos){
1988 pc = pos - startPos;
1990 int opcode = readUByte();
1995 case 1: // aconst_null
1996 reader.aconst_null();
1998 case 2: // iconst_m1
2022 case 10: // lconst_1
2025 case 11: // fconst_0
2028 case 12: // fconst_1
2031 case 13: // fconst_2
2034 case 14: // dconst_0
2037 case 15: // dconst_1
2041 constVal = readByte();
2042 reader.bipush(constVal);
2045 constVal = readI2();
2046 reader.sipush(constVal);
2049 cpIdx = readUByte();
2054 reader.ldc_w_(cpIdx);
2058 reader.ldc2_w(cpIdx);
2061 localVarIndex = isWide ? readU2() : readUByte();
2062 reader.iload(localVarIndex);
2065 localVarIndex = isWide ? readU2() : readUByte();
2066 reader.lload(localVarIndex);
2069 localVarIndex = isWide ? readU2() : readUByte();
2070 reader.fload(localVarIndex);
2073 localVarIndex = isWide ? readU2() : readUByte();
2074 reader.dload(localVarIndex);
2077 localVarIndex = isWide ? readU2() : readUByte();
2078 reader.aload(localVarIndex);
2165 localVarIndex = isWide ? readU2() : readUByte();
2166 reader.istore(localVarIndex);
2169 localVarIndex = isWide ? readU2() : readUByte();
2170 reader.lstore(localVarIndex);
2173 localVarIndex = isWide ? readU2() : readUByte();
2174 reader.fstore(localVarIndex);
2177 localVarIndex = isWide ? readU2() : readUByte();
2178 reader.dstore(localVarIndex);
2181 localVarIndex = isWide ? readU2() : readUByte();
2182 reader.astore(localVarIndex);
2184 case 59: // istore_0
2187 case 60: // istore_1
2190 case 61: // istore_2
2193 case 62: // istore_3
2196 case 63: // lstore_0
2199 case 64: // lstore_1
2202 case 65: // lstore_2
2205 case 66: // lstore_3
2208 case 67: // fstore_0
2211 case 68: // fstore_1
2214 case 69: // fstore_2
2217 case 70: // fstore_3
2232 case 75: // astore_0
2235 case 76: // astore_1
2238 case 77: // astore_2
2241 case 78: // astore_3
2405 localVarIndex = readU2();
2406 constVal = readI2();
2408 localVarIndex = readUByte();
2409 constVal = readByte();
2411 reader.iinc(localVarIndex, constVal);
2475 reader.ifeq(offset);
2479 reader.ifne(offset);
2483 reader.iflt(offset);
2487 reader.ifge(offset);
2491 reader.ifgt(offset);
2495 reader.ifle(offset);
2497 case 159: // if_icmpeq
2499 reader.if_icmpeq(offset);
2501 case 160: // if_icmpne
2503 reader.if_icmpne(offset);
2505 case 161: // if_icmplt
2507 reader.if_icmplt(offset);
2509 case 162: // if_icmpge
2511 reader.if_icmpge(offset);
2513 case 163: // if_icmpgt
2515 reader.if_icmpgt(offset);
2517 case 164: // if_icmple
2519 reader.if_icmple(offset);
2521 case 165: // if_acmpeq
2523 reader.if_acmpeq(offset);
2525 case 166: // if_acmpne
2527 reader.if_acmpne(offset);
2531 reader.goto_(offset);
2538 localVarIndex = isWide ? readU2() : readUByte();
2539 reader.ret(localVarIndex);
2541 case 170: // tableswitch
2542 pos = (((pc+4)>>2)<<2)+startPos; // skip over padding
2544 defaultOffset = readI4();
2546 int high = readI4();
2548 int len = high-low+1;
2549 nextPos = pos + len*4;
2550 reader.tableswitch(defaultOffset, low, high);
2553 case 171: // lookupswitch
2554 pos = (((pc+4)>>2)<<2)+startPos; // skip over padding
2556 defaultOffset = readI4();
2557 int nPairs = readI4();
2559 nextPos = pos + (nPairs*8);
2560 reader.lookupswitch(defaultOffset, nPairs);
2563 case 172: // ireturn
2566 case 173: // lreturn
2569 case 174: // freturn
2572 case 175: // dreturn
2575 case 176: // areturn
2581 case 178: // getstatic
2582 cpIdx = readU2(); // CP index of fieldRef
2583 reader.getstatic(cpIdx);
2585 case 179: // putstatic
2586 cpIdx = readU2(); // CP index of fieldRef
2587 reader.putstatic(cpIdx);
2589 case 180: // getfield
2590 cpIdx = readU2(); // CP index of fieldRef
2591 reader.getfield(cpIdx);
2593 case 181: // putfield
2594 cpIdx = readU2(); // CP index of fieldRef
2595 reader.putfield(cpIdx);
2597 case 182: // invokevirtual
2598 cpIdx = readU2(); // CP index of methodRef
2599 reader.invokevirtual(cpIdx);
2601 case 183: // invokespecial
2602 cpIdx = readU2(); // CP index of methodRef
2603 reader.invokespecial(cpIdx);
2605 case 184: // invokestatic
2606 cpIdx = readU2(); // CP index of methodRef
2607 reader.invokestatic(cpIdx);
2609 case 185: // invokeinterface
2610 cpIdx = readU2(); // CP index of methodRef
2611 int count = readUByte();
2612 int zero = readUByte(); // must be 0
2613 reader.invokeinterface(cpIdx, count, zero);
2615 case 186: // invokedynamic
2616 cpIdx = readU2(); // CP index of bootstrap method
2619 reader.invokedynamic(cpIdx);
2625 case 188: // newarray
2626 int aType = readUByte();
2627 reader.newarray(aType);
2629 case 189: // anewarray
2630 cpIdx = readU2(); // CP index of component type
2631 reader.anewarray(cpIdx);
2633 case 190: // arraylength
2634 reader.arraylength();
2639 case 192: // checkcast
2640 cpIdx = readU2(); // cast type cp index
2641 reader.checkcast(cpIdx);
2643 case 193: // instanceof
2644 cpIdx = readU2(); // check type cp index
2645 reader.instanceof_(cpIdx);
2647 case 194: // monitorenter
2648 reader.monitorenter();
2650 case 195: // monitorexit
2651 reader.monitorexit();
2655 // affects immediate operand width if next bytecode is:
2656 // iload,fload,aload,lload,dload,
2657 // istore,fstore,astore,lstore,dstore
2661 case 197: // multianewarray
2663 int dimensions = readUByte();
2664 reader.multianewarray(cpIdx, dimensions);
2668 reader.ifnull(offset);
2670 case 199: // ifnonnull
2672 reader.ifnonnull(offset);
2676 reader.goto_w(offset);
2680 reader.jsr_w(offset);
2685 reader.unknown(opcode);
2688 isWide = false; // reset wide modifier
2693 //--- those can only be called from within a JVMByteCodeReader.tableswitch() notification
2694 public void parseTableSwitchEntries(JVMByteCodeReader reader, int low, int high){
2695 for (int val=low; val<=high; val++){
2696 int offset = readI4();
2697 reader.tableswitchEntry(val, offset);
2700 public int getTableSwitchOffset(int low, int high, int defaultOffset, int val){
2701 if (val < low || val > high){
2702 return defaultOffset;
2705 int n = Math.abs(val - low);
2707 int pcOffset = readI4();
2712 //--- those can only be called from within a JVMByteCodeReader.lookupswitch() notification
2713 public void parseLookupSwitchEntries(JVMByteCodeReader reader, int nEntries){
2714 for (int i=0; i<nEntries; i++){
2715 int value = readI4();
2716 int offset = readI4();
2717 reader.lookupswitchEntry(i, value, offset);
2720 public int getLookupSwitchOffset(int nEntries, int defaultOffset, int val){
2721 for (int i=0; i<nEntries; i++){
2722 int match = readI4();
2725 } else if (val == match) {
2726 int offset = readI4();
2732 return defaultOffset;