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;
23 import gov.nasa.jpf.JPFException;
24 import gov.nasa.jpf.util.BailOut;
25 import gov.nasa.jpf.util.BinaryClassSource;
26 import gov.nasa.jpf.vm.ClassParseException;
29 * class to read and dissect Java classfile contents (as specified by the Java VM
30 * spec http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#16628
32 public class ClassFile extends BinaryClassSource {
34 public static final int CONSTANT_UTF8 = 1;
35 public static final int CONSTANT_INTEGER = 3;
36 public static final int CONSTANT_FLOAT = 4;
37 public static final int CONSTANT_LONG = 5;
38 public static final int CONSTANT_DOUBLE = 6;
39 public static final int CONSTANT_CLASS = 7;
40 public static final int CONSTANT_STRING = 8;
41 public static final int FIELD_REF = 9;
42 public static final int METHOD_REF = 10;
43 public static final int INTERFACE_METHOD_REF = 11;
44 public static final int NAME_AND_TYPE = 12;
45 public static final int METHOD_HANDLE = 15;
46 public static final int METHOD_TYPE = 16;
47 public static final int INVOKE_DYNAMIC = 18;
49 public static final int REF_GETFIELD = 1;
50 public static final int REF_GETSTATIC = 2;
51 public static final int REF_PUTFIELD = 3;
52 public static final int REF_PUTSTATIC = 4;
53 public static final int REF_INVOKEVIRTUAL = 5;
54 public static final int REF_INVOKESTATIC = 6;
55 public static final int REF_INVOKESPECIAL = 7;
56 public static final int REF_NEW_INVOKESPECIAL = 8;
57 public static final int REF_INVOKEINTERFACE = 9;
59 // used to store types in cpValue[]
60 public static enum CpInfo {
72 InterfaceMethodRef, // 11
82 // <2do> this is going away
83 String requestedTypeName; // the type name that caused this classfile to be loaded
86 int[] cpPos; // cpPos[i] holds data start index for cp_entry i (0 is unused)
87 Object[] cpValue; // cpValue[i] hold the String/Integer/Float/Double associated with corresponding cp_entries
90 public ClassFile (byte[] data, int offset){
94 public ClassFile (byte[] data){
98 public ClassFile (String typeName, byte[] data){
101 this.requestedTypeName = typeName;
104 public ClassFile (String typeName, byte[] data, int offset){
107 this.requestedTypeName = typeName;
110 public ClassFile (File file) throws ClassParseException {
114 public ClassFile (String pathName) throws ClassParseException {
115 super( new File(pathName));
122 * set classfile data. This is mainly provided to allow
123 * on-the-fly classfile instrumentation with 3rd party libraries
125 * BEWARE - like getData(), this method can cause parsing to fail if the
126 * provided data does not conform to the VM specs. In particular, this
127 * method should ONLY be called before executing parse(ClassFileReader) and
128 * will otherwise throw a JPFException
130 public void setData(byte[] newData){
132 throw new JPFException("concurrent modification of ClassFile data");
139 * return the typename this classfile gets loaded for
140 * <2do> this is going away
142 public String getRequestedTypeName(){
143 return requestedTypeName;
147 //--- general attributes
148 public static final String SYNTHETIC_ATTR = "Synthetic";
149 public static final String DEPRECATED_ATTR = "Deprecated";
150 public static final String SIGNATURE_ATTR = "Signature";
151 public static final String RUNTIME_INVISIBLE_ANNOTATIONS_ATTR = "RuntimeInvisibleAnnotations";
152 public static final String RUNTIME_VISIBLE_ANNOTATIONS_ATTR = "RuntimeVisibleAnnotations";
153 public static final String RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR = "RuntimeVisibleTypeAnnotations";
155 //--- standard field attributes
156 public static final String CONST_VALUE_ATTR = "ConstantValue";
158 protected final static String[] stdFieldAttrs = {
159 CONST_VALUE_ATTR, SYNTHETIC_ATTR, DEPRECATED_ATTR, SIGNATURE_ATTR,
160 RUNTIME_INVISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR };
163 //--- standard method attributes
164 public static final String CODE_ATTR = "Code";
165 public static final String EXCEPTIONS_ATTR = "Exceptions";
166 public static final String RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS_ATTR = "RuntimeInvisibleParameterAnnotations";
167 public static final String RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS_ATTR = "RuntimeVisibleParameterAnnotations";
168 public static final String ANNOTATIONDEFAULT_ATTR = "AnnotationDefault";
170 protected final static String[] stdMethodAttrs = {
171 CODE_ATTR, EXCEPTIONS_ATTR, SYNTHETIC_ATTR, DEPRECATED_ATTR, SIGNATURE_ATTR,
172 RUNTIME_INVISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_ANNOTATIONS_ATTR,
173 RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS_ATTR,
174 RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS_ATTR,
175 RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR,
176 ANNOTATIONDEFAULT_ATTR
180 //--- standard code attributes
181 public static final String LINE_NUMBER_TABLE_ATTR = "LineNumberTable";
182 public static final String LOCAL_VAR_TABLE_ATTR = "LocalVariableTable";
184 protected final static String[] stdCodeAttrs = { LINE_NUMBER_TABLE_ATTR, LOCAL_VAR_TABLE_ATTR, RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR };
187 //--- standard class attributes
188 public static final String SOURCE_FILE_ATTR = "SourceFile";
189 public static final String INNER_CLASSES_ATTR = "InnerClasses";
190 public static final String ENCLOSING_METHOD_ATTR = "EnclosingMethod";
191 public static final String BOOTSTRAP_METHOD_ATTR = "BootstrapMethods";
193 protected final static String[] stdClassAttrs = {
194 SOURCE_FILE_ATTR, DEPRECATED_ATTR, INNER_CLASSES_ATTR, DEPRECATED_ATTR, SIGNATURE_ATTR,
195 RUNTIME_INVISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_ANNOTATIONS_ATTR, RUNTIME_VISIBLE_TYPE_ANNOTATIONS_ATTR,
196 ENCLOSING_METHOD_ATTR, BOOTSTRAP_METHOD_ATTR };
199 protected String internStdAttrName(int cpIdx, String name, String[] stdNames){
200 for (int i=0; i<stdNames.length; i++){
201 if (stdNames[i] == name) return name;
203 for (int i=0; i<stdNames.length; i++){
204 String stdName = stdNames[i];
205 if (stdName.equals(name)){
206 cpValue[cpIdx] = stdName;
214 //--- constpool access
216 //--- the primitive info cpValue
217 public String utf8At(int utf8InfoIdx){
218 //assert data[cpPos[utf8InfoIdx]] == 1 : "not a utf8_info tag";
219 return (String) cpValue[utf8InfoIdx];
222 public int intAt(int intInfoIdx){
223 //assert data[cpPos[intInfoIdx]] == 3 : "not a int_info tag";
224 return (Integer) cpValue[intInfoIdx];
227 public float floatAt(int floatInfoIdx){
228 //assert data[cpPos[floatInfoIdx]] == 4 : "not a float_info tag";
229 return (Float) cpValue[floatInfoIdx];
232 public long longAt(int longInfoIdx){
233 //assert data[cpPos[longInfoIdx]] == 5 : "not a long_info tag";
234 return (Long) cpValue[longInfoIdx];
237 public double doubleAt(int doubleInfoIdx){
238 //assert data[cpPos[doubleInfoIdx]] == 6 : "not a double_info tag";
239 return (Double) cpValue[doubleInfoIdx];
242 //--- those two are delegated but resolved
243 public String classNameAt(int classInfoIdx){
244 //assert data[cpPos[classInfoIdx]] == 7 : "not a Class_info tag";
245 return (String) cpValue[classInfoIdx];
248 public String stringAt(int stringInfoIdx){
249 //assert data[cpPos[stringInfoIdx]] == 8 : "not a String_info tag";
250 return (String) cpValue[stringInfoIdx];
253 //--- composite infos
255 // the generic ones (if we don't care what kind of reference type this is)
256 public String refClassNameAt(int cpIdx){
257 return (String) cpValue[ u2(cpPos[cpIdx]+1)];
259 public String refNameAt(int cpIdx){
260 return utf8At( u2( cpPos[ u2(cpPos[cpIdx]+3)]+1));
262 public String refDescriptorAt(int cpIdx){
263 return utf8At( u2( cpPos[ u2(cpPos[cpIdx]+3)]+3));
266 public int mhRefTypeAt (int methodHandleInfoIdx){
267 return u1(cpPos[methodHandleInfoIdx]+1);
269 public int mhMethodRefIndexAt (int methodHandleInfoIdx){
270 return u2(cpPos[methodHandleInfoIdx]+2);
273 // those could check ref types
274 public String fieldClassNameAt(int fieldRefInfoIdx){
275 //assert data[cpPos[fieldRefInfoIdx]] == 9 : "not a Fieldref_info tag";
276 return (String) cpValue[ u2(cpPos[fieldRefInfoIdx]+1)];
278 public String fieldNameAt(int fieldRefInfoIdx){
279 return utf8At( u2( cpPos[ u2(cpPos[fieldRefInfoIdx]+3)]+1));
281 public String fieldDescriptorAt(int fieldRefInfoIdx){
282 return utf8At( u2( cpPos[ u2(cpPos[fieldRefInfoIdx]+3)]+3));
285 public String methodClassNameAt(int methodRefInfoIdx){
286 return (String) cpValue[ u2(cpPos[methodRefInfoIdx]+1)];
288 public String methodNameAt(int methodRefInfoIdx){
289 return utf8At( u2( cpPos[ u2(cpPos[methodRefInfoIdx]+3)]+1));
291 public String methodDescriptorAt(int methodRefInfoIdx){
292 return utf8At( u2( cpPos[ u2(cpPos[methodRefInfoIdx]+3)]+3));
295 public String methodTypeDescriptorAt (int methodTypeInfoIdx){
296 return utf8At( u2(cpPos[methodTypeInfoIdx]+1));
299 public String interfaceMethodClassNameAt(int ifcMethodRefInfoIdx){
300 return (String) cpValue[ u2(cpPos[ifcMethodRefInfoIdx]+1)];
302 public String interfaceMethodNameAt(int ifcMethodRefInfoIdx){
303 return utf8At( u2( cpPos[ u2(cpPos[ifcMethodRefInfoIdx]+3)]+1));
305 public String interfaceMethodDescriptorAt(int ifcMethodRefInfoIdx){
306 return utf8At( u2( cpPos[ u2(cpPos[ifcMethodRefInfoIdx]+3)]+3));
309 public int bootstrapMethodIndex (int cpInvokeDynamicIndex){
310 return u2(cpPos[cpInvokeDynamicIndex]+1);
312 public String samMethodNameAt(int cpInvokeDynamicIndex) {
313 return utf8At( u2( cpPos[ u2(cpPos[cpInvokeDynamicIndex]+3)]+1));
315 public String callSiteDescriptor(int cpInvokeDynamicIndex) {
316 return utf8At( u2( cpPos[ u2(cpPos[cpInvokeDynamicIndex]+3)]+3));
319 public String getRefTypeName (int refCode){
321 case REF_GETFIELD: return "getfield";
322 case REF_GETSTATIC: return "getstatic";
323 case REF_PUTFIELD: return "putfield";
324 case REF_PUTSTATIC: return "putstatic";
325 case REF_INVOKEVIRTUAL: return "invokevirtual";
326 case REF_INVOKESTATIC: return "invokestatic";
327 case REF_INVOKESPECIAL: return "invokespecial";
328 case REF_NEW_INVOKESPECIAL: return "new-invokespecial";
329 case REF_INVOKEINTERFACE: return "invokeinterface";
335 public String getTypeName (int typeCode){
337 case 4: return "boolean";
338 case 5: return "char";
339 case 6: return "float";
340 case 7: return "double";
341 case 8: return "byte";
342 case 9: return "short";
343 case 10: return "int";
344 case 11: return "long";
359 //--- traverse/analyze the const pool (this is rather exotic)
361 public int getNumberOfCpEntries(){
362 return cpValue.length;
365 public Object getCpValue (int i){
369 public int getCpTag (int i){
370 return data[cpPos[i]];
374 * the result can be used as input for u2(dataIndex)
376 * NOTE - this returns -1 for the dreaded unused extra entries associated
377 * with ConstantDouble and ConstantLong
379 public int getDataPosOfCpEntry (int i){
383 //--- standard attributes
385 public Object getConstValueAttribute(int dataPos){
386 int cpIdx = u2(dataPos);
387 Object v = cpValue[cpIdx];
391 public String getSourceFileAttribute(int dataPos){
392 // SourceFile_attribute { u2 attr_name_idx; u4 attr_length; u2 sourcefile_idx<utf8>; }
394 int cpIdx = u2(dataPos + 6);
395 Object v = cpValue[cpIdx];
400 //--- low level readers
402 public final int u1(int dataIdx){
403 return data[dataIdx] & 0xff;
406 public final int u2(int dataIdx){
407 return ((data[dataIdx]&0xff) << 8) | (data[dataIdx+1]&0xff);
410 public final int i1(int dataIdx) {
411 return data[dataIdx++];
414 public final int i2(int dataIdx) {
416 return (data[idx++] << 8) | (data[idx]&0xff);
419 public final int readU2(){
422 return ((data[idx++]&0xff) << 8) | (data[idx]&0xff);
425 public final int readI2() {
428 return (data[idx++] << 8) | (data[idx]&0xff);
431 public final int readI4(){
434 byte[] data = this.data;
436 return (data[idx++] <<24) | ((data[idx++]&0xff) << 16) | ((data[idx++]&0xff) << 8) | (data[idx]&0xff);
440 //--- reader notifications
441 private void setClass(ClassFileReader reader, String clsName, String superClsName, int flags, int cpCount) throws ClassParseException {
443 reader.setClass( this, clsName, superClsName, flags, cpCount);
447 private void setInterfaceCount(ClassFileReader reader, int ifcCount){
449 reader.setInterfaceCount( this, ifcCount);
452 private void setInterface(ClassFileReader reader, int ifcIndex, String ifcName){
454 reader.setInterface( this, ifcIndex, ifcName);
457 private void setInterfacesDone(ClassFileReader reader){
459 reader.setInterfacesDone( this);
464 private void setFieldCount(ClassFileReader reader, int fieldCount){
466 reader.setFieldCount( this, fieldCount);
470 private void setField(ClassFileReader reader, int fieldIndex, int accessFlags, String name, String descriptor){
472 reader.setField( this, fieldIndex, accessFlags, name, descriptor);
475 private void setFieldAttributeCount(ClassFileReader reader, int fieldIndex, int attrCount){
477 reader.setFieldAttributeCount( this, fieldIndex, attrCount);
480 private void setFieldAttribute(ClassFileReader reader, int fieldIndex, int attrIndex, String name, int attrLength){
481 int p = pos + attrLength;
482 reader.setFieldAttribute( this, fieldIndex, attrIndex, name, attrLength);
485 private void setFieldAttributesDone(ClassFileReader reader, int fieldIndex){
487 reader.setFieldAttributesDone( this, fieldIndex);
490 private void setFieldDone(ClassFileReader reader, int fieldIndex){
492 reader.setFieldDone( this, fieldIndex);
495 private void setFieldsDone(ClassFileReader reader){
497 reader.setFieldsDone( this);
500 private void setConstantValue(ClassFileReader reader, Object tag, Object value){
502 reader.setConstantValue( this, tag, value);
506 private void setMethodCount(ClassFileReader reader, int methodCount){
508 reader.setMethodCount( this, methodCount);
511 private void setMethod(ClassFileReader reader, int methodIndex, int accessFlags, String name, String descriptor){
513 reader.setMethod( this, methodIndex, accessFlags, name, descriptor);
516 private void setMethodAttributeCount(ClassFileReader reader, int methodIndex, int attrCount){
518 reader.setMethodAttributeCount( this, methodIndex, attrCount);
521 private void setMethodAttribute(ClassFileReader reader, int methodIndex, int attrIndex, String name, int attrLength){
522 int p = pos + attrLength;
523 reader.setMethodAttribute( this, methodIndex, attrIndex, name, attrLength);
526 private void setMethodAttributesDone(ClassFileReader reader, int methodIndex){
528 reader.setMethodAttributesDone( this, methodIndex);
531 private void setMethodDone(ClassFileReader reader, int methodIndex){
533 reader.setMethodDone( this, methodIndex);
536 private void setMethodsDone(ClassFileReader reader){
538 reader.setMethodsDone( this);
541 private void setExceptionCount(ClassFileReader reader, Object tag, int exceptionCount){
543 reader.setExceptionCount( this, tag, exceptionCount);
546 private void setExceptionsDone(ClassFileReader reader, Object tag){
548 reader.setExceptionsDone( this, tag);
551 private void setException(ClassFileReader reader, Object tag, int exceptionIndex, String exceptionType){
553 reader.setException( this, tag, exceptionIndex, exceptionType);
556 private void setCode(ClassFileReader reader, Object tag, int maxStack, int maxLocals, int codeLength){
557 int p = pos + codeLength;
558 reader.setCode( this, tag, maxStack, maxLocals, codeLength);
561 private void setExceptionTableCount(ClassFileReader reader, Object tag, int exceptionTableCount){
563 reader.setExceptionHandlerTableCount( this, tag, exceptionTableCount);
566 private void setExceptionTableEntry(ClassFileReader reader, Object tag, int exceptionIndex,
567 int startPc, int endPc, int handlerPc, String catchType){
569 reader.setExceptionHandler( this, tag, exceptionIndex, startPc, endPc, handlerPc, catchType);
572 private void setExceptionTableDone(ClassFileReader reader, Object tag){
574 reader.setExceptionHandlerTableDone( this, tag);
578 private void setCodeAttributeCount(ClassFileReader reader, Object tag, int attrCount){
580 reader.setCodeAttributeCount( this, tag, attrCount);
583 private void setCodeAttribute(ClassFileReader reader, Object tag, int attrIndex, String name, int attrLength){
584 int p = pos + attrLength;
585 reader.setCodeAttribute( this, tag, attrIndex, name, attrLength);
588 private void setCodeAttributesDone(ClassFileReader reader, Object tag){
590 reader.setCodeAttributesDone( this, tag);
594 private void setLineNumberTableCount(ClassFileReader reader, Object tag, int lineNumberCount){
596 reader.setLineNumberTableCount( this, tag, lineNumberCount);
599 private void setLineNumber(ClassFileReader reader, Object tag, int lineIndex, int lineNumber, int startPc){
601 reader.setLineNumber( this, tag, lineIndex, lineNumber, startPc);
604 private void setLineNumberTableDone(ClassFileReader reader, Object tag){
606 reader.setLineNumberTableDone( this, tag);
610 private void setLocalVarTableCount(ClassFileReader reader, Object tag, int localVarCount){
612 reader.setLocalVarTableCount( this, tag, localVarCount);
615 private void setLocalVar(ClassFileReader reader, Object tag, int localVarIndex, String varName, String descriptor,
616 int scopeStartPc, int scopeEndPc, int slotIndex){
618 reader.setLocalVar( this, tag, localVarIndex, varName, descriptor, scopeStartPc, scopeEndPc, slotIndex);
621 private void setLocalVarTableDone(ClassFileReader reader, Object tag){
623 reader.setLocalVarTableDone( this, tag);
628 private void setClassAttributeCount(ClassFileReader reader, int attrCount){
630 reader.setClassAttributeCount( this, attrCount);
633 private void setClassAttribute(ClassFileReader reader, int attrIndex, String name, int attrLength){
634 int p = pos + attrLength;
635 reader.setClassAttribute( this, attrIndex, name, attrLength);
638 private void setClassAttributesDone(ClassFileReader reader){
640 reader.setClassAttributesDone(this);
644 private void setSourceFile(ClassFileReader reader, Object tag, String pathName){
646 reader.setSourceFile( this, tag, pathName);
650 private void setBootstrapMethodCount (ClassFileReader reader, Object tag, int bootstrapMethodCount){
652 reader.setBootstrapMethodCount( this, tag, bootstrapMethodCount);
655 private void setBootstrapMethod (ClassFileReader reader, Object tag, int idx,
656 int refKind, String cls, String mth, String descriptor, int[] cpArgs){
658 reader.setBootstrapMethod( this, tag, idx, refKind, cls, mth, descriptor, cpArgs);
661 private void setBootstrapMethodsDone (ClassFileReader reader, Object tag){
663 reader.setBootstrapMethodsDone( this, tag);
667 private void setInnerClassCount(ClassFileReader reader, Object tag, int innerClsCount){
669 reader.setInnerClassCount( this, tag, innerClsCount);
672 private void setInnerClass(ClassFileReader reader, Object tag, int innerClsIndex, String outerName, String innerName,
673 String innerSimpleName, int accessFlags){
675 reader.setInnerClass( this, tag, innerClsIndex, outerName, innerName, innerSimpleName, accessFlags);
678 private void setEnclosingMethod(ClassFileReader reader, Object tag, String enclosingClass, String enclosedMethod, String descriptor){
680 reader.setEnclosingMethod( this, tag, enclosingClass, enclosedMethod, descriptor);
683 private void setInnerClassesDone(ClassFileReader reader, Object tag){
685 reader.setInnerClassesDone(this, tag);
689 private void setAnnotationCount(ClassFileReader reader, Object tag, int annotationCount){
691 reader.setAnnotationCount( this, tag, annotationCount);
694 private boolean setAnnotation(ClassFileReader reader, Object tag, int annotationIndex, String annotationType){
697 reader.setAnnotation( this, tag, annotationIndex, annotationType);
700 } catch (SkipAnnotation sa) {
701 this.skipAnnotation(false);
707 * This is largely lifted from AnnotationParser.java
710 * u2 num_element_value_pairs;
712 * u2 element_name_index;
713 * element_value value;
714 * } element_value_pairs[num_element_value_pairs]
717 private void skipAnnotation(boolean skipTypeIndex) {
718 if(skipTypeIndex) { // we may want to skip after reading the type name
721 int numKV = readU2();
722 for(int i = 0; i < numKV; i++) {
723 readU2(); // skip name
729 * Skips an element_value
731 private void skipMemberValue() {
732 int tag = readUByte();
734 case 'e': // Enum value
735 pos += 4; // an enum value is a struct of two shorts, for 4 bytes total
738 skipAnnotation(true);
744 pos += 2; // either two bye const val index or two byte class info index
748 private void skipArray() {
750 for(int i = 0; i < len; i++) {
755 private void setAnnotationsDone(ClassFileReader reader, Object tag){
757 reader.setAnnotationsDone(this, tag);
761 private void setTypeAnnotationCount(ClassFileReader reader, Object tag, int annotationCount){
763 reader.setTypeAnnotationCount( this, tag, annotationCount);
766 private void setTypeAnnotationsDone(ClassFileReader reader, Object tag){
768 reader.setTypeAnnotationsDone(this, tag);
773 private void setAnnotationValueCount(ClassFileReader reader, Object tag, int annotationIndex, int nValuePairs){
775 reader.setAnnotationValueCount( this, tag, annotationIndex, nValuePairs);
778 private void setPrimitiveAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
779 String elementName, int arrayIndex, Object val){
781 reader.setPrimitiveAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
784 private void setStringAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
785 String elementName, int arrayIndex, String s){
787 reader.setStringAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, s);
790 private void setClassAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
791 String elementName, int arrayIndex, String typeName){
793 reader.setClassAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, typeName);
797 private void setAnnotationFieldValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex, String elementName, int arrayIndex) {
799 reader.setAnnotationFieldValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex);
803 private void setEnumAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
804 String elementName, int arrayIndex, String enumType, String enumValue){
806 reader.setEnumAnnotationValue( this, tag, annotationIndex, valueIndex, elementName, arrayIndex, enumType, enumValue);
810 private void setAnnotationValueElementCount(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
811 String elementName, int elementCount){
813 reader.setAnnotationValueElementCount(this, tag, annotationIndex, valueIndex, elementName, elementCount);
816 private void setAnnotationValueElementsDone(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex,
819 reader.setAnnotationValueElementsDone(this, tag, annotationIndex, valueIndex, elementName);
823 public void setAnnotationValuesDone(ClassFileReader reader, Object tag, int annotationIndex){
825 reader.setAnnotationValuesDone(this, tag, annotationIndex);
829 private void setParameterCount(ClassFileReader reader, Object tag, int parameterCount){
831 reader.setParameterCount(this, tag, parameterCount);
834 private void setParameterAnnotationCount(ClassFileReader reader, Object tag, int paramIndex, int annotationCount){
836 reader.setParameterAnnotationCount(this, tag, paramIndex, annotationCount);
840 private boolean setParameterAnnotation(ClassFileReader reader, Object tag, int annotationIndex, String annotationType){
843 reader.setParameterAnnotation( this, tag, annotationIndex, annotationType);
846 } catch(SkipAnnotation s) {
847 this.skipAnnotation(false);
851 private void setParameterAnnotationsDone(ClassFileReader reader, Object tag, int paramIndex){
853 reader.setParameterAnnotationsDone(this, tag, paramIndex);
856 private void setParametersDone(ClassFileReader reader, Object tag){
858 reader.setParametersDone(this, tag);
862 public void setSignature(ClassFileReader reader, Object tag, String signature){
864 reader.setSignature(this, tag, signature);
871 * this is the main parsing routine that uses the ClassFileReader interface
872 * to tell clients about the classfile contents
874 * ClassFile structure: http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#74353
875 * u4 magic; // 0xcafebabe
879 * u2 constant_pool_count;
880 * cp_entry constant_pool[constant_pool_count-1];
886 * u2 interfaces_count;
887 * u2 interfaces[interfaces_count];
890 * field_info fields[fields_count];
893 * method_info methods[methods_count];
895 * u2 attributes_count;
896 * attribute_info attributes[attributes_count];
898 public void parse( ClassFileReader reader) throws ClassParseException {
903 int magic = readI4();
904 if (magic != 0xCAFEBABE) {
905 error("wrong magic: " + Integer.toHexString(magic));
908 // we don't do much with the version numbers yet
909 int minor = readU2();
910 int major = readU2();
912 // get the const pool
913 int cpCount = readU2();
914 cpPos = new int[cpCount];
915 cpValue = new Object[cpCount];
918 // the class essentials
919 int accessFlags = readU2();
922 String clsName = (String) cpValue[cpIdx];
925 String superClsName = (String) cpValue[cpIdx];
928 setClass(reader, clsName, superClsName, accessFlags, cpCount);
931 int ifcCount = readU2();
932 parseInterfaces(reader, ifcCount);
935 int fieldCount = readU2();
936 parseFields(reader, fieldCount);
939 int methodCount = readU2();
940 parseMethods(reader, methodCount);
943 int classAttrCount = readU2();
944 parseClassAttributes(reader, classAttrCount);
947 // nothing, just a control exception to shortcut the classfile parsing
952 //--- constpool parsing
954 public static String readModifiedUTF8String( byte[] data, int pos, int len) throws ClassParseException {
956 int n = 0; // the number of chars in buf
957 char[] buf = new char[len]; // it can't be more, but it can be less chars
959 // \u0001 - \u007f : single byte chars: 0xxxxxxx
960 // \u0000 and \u0080 - \u07ff : double byte chars: 110xxxxx, 10xxxxxx
961 // \u0800 - \uffff : tripple byte chars: 1110xxxx, 10xxxxxx, 10xxxxxx
964 for (int i=pos; i<max; i++){
965 int c = data[i] & 0xff;
966 if ((c & 0x80) == 0){ // single byte char 0xxxxxxx
970 if ((c & 0x40) != 0){ // 11xxxxxx
972 // for the sake of efficiency, we don't check for the trailing zero bit in the marker,
973 // we just mask it out
974 if ((c & 0x20) == 0) { // 110xxxxx - double byte char
975 buf[n++] = (char) (((c & 0x1f) << 6) | (data[++i] & 0x3f));
977 } else { // 1110xxxx - tripple byte char
978 buf[n++] = (char) (((c & 0x0f) << 12) | ((data[++i] & 0x3f) << 6) | (data[++i] & 0x3f));
982 throw new ClassParseException("malformed modified UTF-8 input: ");
987 return new String(buf, 0, n);
991 // the protected methods are called automatically, the public parse..Attr() methods
992 // are called optionally from the corresponding ClassFileReader.set..Attribute() method.
993 // Note that these calls have to provide the ClassFileReader as an argument because
994 // we might actually switch to another reader (e.g. MethodInfos for parseCodeAttr)
996 protected void parseCp(int cpCount) throws ClassParseException {
999 byte[] data = this.data;
1000 int[] dataIdx = this.cpPos;
1001 Object[] values = this.cpValue;
1003 //--- first pass: store data index values and convert non-delegating constant values
1004 // cp_entry[0] is traditionally unused
1005 for (int i=1; i<cpCount; i++) {
1008 error("illegal constpool tag");
1010 case CONSTANT_UTF8: // utf8_info { u1 tag; u2 length; u1 bytes[length]; }
1012 int len = ((data[j++]&0xff) <<8) | (data[j++]&0xff);
1014 String s = readModifiedUTF8String( data, j, len);
1021 error("illegal constpool tag");
1023 case CONSTANT_INTEGER: // Integer_info { u1 tag; u4 bytes; }
1026 int iVal = (data[j++]&0xff)<<24 | (data[j++]&0xff)<<16 | (data[j++]&0xff)<<8 | (data[j++]&0xff);
1027 values[i] = new Integer(iVal);
1030 case CONSTANT_FLOAT: // Float_info { u1 tag; u4 bytes; }
1033 int iBits = (data[j++]&0xff)<<24 | (data[j++]&0xff)<<16 | (data[j++]&0xff)<<8 | (data[j++]&0xff);
1034 float fVal = Float.intBitsToFloat(iBits);
1035 values[i] = new Float(fVal);
1038 case CONSTANT_LONG: // Long_info { u1 tag; u4 high_bytes; u4 low_bytes; }
1040 long lVal = (data[j++]&0xffL)<<56 | (data[j++]&0xffL)<<48 | (data[j++]&0xffL)<<40 | (data[j++]&0xffL)<<32
1041 | (data[j++]&0xffL)<<24 | (data[j++]&0xffL)<<16 | (data[j++]&0xffL)<<8 | (data[j++]&0xffL);
1042 values[i] = new Long(lVal);
1044 dataIdx[++i] = -1; // 8 byte cpValue occupy 2 index slots
1047 case CONSTANT_DOUBLE: // Double_info { u1 tag; u4 high_bytes; u4 low_bytes; }
1050 long lBits = (data[j++]&0xffL)<<56 | (data[j++]&0xffL)<<48 | (data[j++]&0xffL)<<40 | (data[j++]&0xffL)<<32
1051 | (data[j++]&0xffL)<<24 | (data[j++]&0xffL)<<16 | (data[j++]&0xffL)<<8 | (data[j++]&0xffL);
1052 double dVal = Double.longBitsToDouble(lBits);
1053 values[i] = new Double(dVal);
1055 dataIdx[++i] = -1; // 8 byte cpValue occupy 2 index slots
1058 case CONSTANT_CLASS: // Class_info { u1 tag; u2 name_index<utf8>; }
1060 values[i] = CpInfo.ConstantClass;
1065 case CONSTANT_STRING: // String_info { u1 tag; u2 string_index<utf8>; }
1067 values[i] = CpInfo.ConstantString;
1072 case FIELD_REF: // Fieldref_info { u1 tag; u2 class_index; u2 name_and_type_index; }
1074 values[i] = CpInfo.FieldRef;
1078 case METHOD_REF: // Methodref_info { u1 tag; u2 class_index; u2 name_and_type_index; }
1080 values[i] = CpInfo.MethodRef;
1084 case INTERFACE_METHOD_REF: // InterfaceMethodref_info { u1 tag; u2 class_index; u2 name_and_type_index; }
1086 values[i] = CpInfo.InterfaceMethodRef;
1090 case NAME_AND_TYPE: // NameAndType_info { u1 tag; u2 name_index<utf8>; u2 descriptor_index<utf8>; }
1092 values[i] = CpInfo.NameAndType;
1097 //--- the Java 8 ones
1099 case METHOD_HANDLE: // MethodHandle_info { u1 tag; u1 reference_kind; u2 reference_index<mthref>; }
1101 values[i] = CpInfo.MethodHandle;
1105 case METHOD_TYPE: // MethodType_info { u1 tag; u2 descriptor_index<utf8>; }
1107 values[i] = CpInfo.MethodType;
1111 case INVOKE_DYNAMIC: // InvokeDynamic_info { u1 tag; u2 bootstrap_method_attr_index; u2 name_and_type_index; }
1113 values[i] = CpInfo.InvokeDynamic;
1118 error("illegal constpool tag: " + data[j]);
1124 //--- second pass: store values of delegating constant values
1125 for (int i=1; i<cpCount; i++){
1126 Object v = cpValue[i];
1128 // we store string and class constants as their utf8 string values
1129 if (v == CpInfo.ConstantClass || v == CpInfo.ConstantString){
1130 cpValue[i] = cpValue[u2(cpPos[i]+1)];
1135 protected void parseInterfaces(ClassFileReader reader, int ifcCount){
1137 setInterfaceCount(reader, ifcCount);
1139 for (int i=0; i<ifcCount; i++){
1140 int cpIdx = readU2();
1141 setInterface(reader, i, classNameAt(cpIdx));
1144 setInterfacesDone(reader);
1148 protected void parseFields(ClassFileReader reader, int fieldCount) {
1150 setFieldCount(reader, fieldCount);
1152 for (int i=0; i<fieldCount; i++){
1153 int accessFlags = readU2();
1155 int cpIdx = readU2();
1156 String name = utf8At(cpIdx);
1159 String descriptor = utf8At(cpIdx);
1161 setField(reader, i, accessFlags, name, descriptor);
1163 int attrCount = readU2();
1164 parseFieldAttributes(reader, i, attrCount);
1166 setFieldDone(reader, i);
1169 setFieldsDone(reader);
1172 protected void parseFieldAttributes(ClassFileReader reader, int fieldIdx, int attrCount){
1173 setFieldAttributeCount(reader, fieldIdx, attrCount);
1175 for (int i=0; i<attrCount; i++){
1176 int cpIdx = readU2();
1177 String name = utf8At(cpIdx);
1179 name = internStdAttrName(cpIdx, name, stdFieldAttrs);
1181 int attrLength = readI4(); // actually U4, but we don't support 2GB attributes
1182 setFieldAttribute(reader, fieldIdx, i, name, attrLength);
1185 setFieldAttributesDone(reader, fieldIdx);
1189 * optionally called by reader to obtain a ConstantValue field attribute
1191 * ConstantValue {u2 attrName<utf8>; u4 attrLength; u2 constIndex<class|string|int|float|long|double> }
1193 * pos is at constIndex
1195 public void parseConstValueAttr(ClassFileReader reader, Object tag){
1196 int cpIdx = readU2();
1197 setConstantValue(reader, tag, cpValue[cpIdx]);
1202 protected void parseMethods(ClassFileReader reader, int methodCount) {
1204 setMethodCount(reader, methodCount);
1206 for (int i=0; i<methodCount; i++){
1207 int accessFlags = readU2();
1209 int cpIdx = readU2();
1210 String name = utf8At(cpIdx);
1213 String descriptor = utf8At(cpIdx);
1215 setMethod(reader, i, accessFlags, name, descriptor);
1217 int attrCount = readU2();
1218 parseMethodAttributes(reader, i, attrCount);
1220 setMethodDone(reader, i);
1223 setMethodsDone(reader);
1226 protected void parseMethodAttributes(ClassFileReader reader, int methodIdx, int attrCount){
1227 setMethodAttributeCount(reader, methodIdx, attrCount);
1229 for (int i=0; i<attrCount; i++){
1230 int cpIdx = readU2();
1231 String name = utf8At(cpIdx);
1233 name = internStdAttrName(cpIdx, name, stdMethodAttrs);
1235 int attrLength = readI4(); // actually U4, but we don't support 2GB attributes
1236 setMethodAttribute(reader, methodIdx, i, name, attrLength);
1239 setMethodAttributesDone(reader, methodIdx);
1242 public void parseExceptionAttr (ClassFileReader reader, Object tag){
1243 int exceptionCount = readU2();
1244 setExceptionCount(reader, tag, exceptionCount);
1246 for (int i=0; i<exceptionCount; i++){
1247 int cpIdx = readU2();
1248 String exceptionType = classNameAt(cpIdx);
1249 setException(reader, tag, i, exceptionType);
1252 setExceptionsDone(reader, tag);
1256 * (optionally) called by reader from within the setMethodAttribute() notification
1257 * This means we have recursive notification since this is a variable length
1258 * attribute that has variable length attributes
1260 * Code_attribute { u2 attr_name_index<utf8>; u4 attr_length;
1261 * u2 max_stack; u2 max_locals;
1262 * u4 code_length; u1 code[code_length];
1263 * u2 exception_table_length;
1264 * { u2 start_pc; u2 end_pc; u2 handler_pc; u2 catch_type<class_entry>;
1265 * } exception_table[exception_table_length];
1266 * u2 attributes_count;
1267 * attribute_info attributes[attributes_count]; }
1269 * pos is at max_stack
1271 public void parseCodeAttr (ClassFileReader reader, Object tag){
1272 int maxStack = readU2();
1273 int maxLocals = readU2();
1274 int codeLength = readI4(); // no code length > 2GB supported
1275 int codeStartPos = pos;
1277 setCode(reader, tag, maxStack, maxLocals, codeLength);
1279 int exceptionCount = readU2();
1280 setExceptionTableCount(reader, tag, exceptionCount);
1282 for (int i = 0; i < exceptionCount; i++) {
1283 int startPc = readU2();
1284 int endPc = readU2();
1285 int handlerPc = readU2();
1287 int cpIdx = readU2();
1288 String catchType = (String) cpValue[cpIdx]; // a Constant_class
1290 setExceptionTableEntry(reader, tag, i, startPc, endPc, handlerPc, catchType);
1292 setExceptionTableDone(reader, tag);
1294 int attrCount = readU2();
1295 parseCodeAttrAttributes(reader, tag, attrCount);
1299 protected void parseCodeAttrAttributes(ClassFileReader reader, Object tag, int attrCount){
1301 setCodeAttributeCount(reader, tag, attrCount);
1303 for (int i=0; i<attrCount; i++){
1304 int cpIdx = readU2();
1305 String name = utf8At(cpIdx);
1307 name = internStdAttrName(cpIdx, name, stdCodeAttrs);
1309 int attrLength = readI4(); // actually U4, but we don't support 2GB attributes
1310 setCodeAttribute(reader, tag, i, name, attrLength);
1313 setCodeAttributesDone(reader, tag);
1317 * optionally called from ClassFileReader.setCodeAttribute() to parse LineNumberTables
1318 * LineNumberTable { u2 attrName; u4 attrLength;
1320 * { u2 startPc; u2 lineNumber; } [lineCount] };
1321 * pos is at lineCount
1323 public void parseLineNumberTableAttr(ClassFileReader reader, Object tag){
1324 int lineCount = readU2();
1325 setLineNumberTableCount(reader, tag, lineCount);
1327 for (int i=0; i<lineCount; i++){
1328 int startPc = readU2();
1329 int lineNumber = readU2();
1330 setLineNumber(reader, tag, i, lineNumber, startPc);
1333 setLineNumberTableDone(reader, tag);
1338 * optionally called from ClassFileReader.setCodeAttribute() to parse LocalVarTables
1339 * LocalVarTableTable { u2 attrName; u4 attrLength;
1341 * { u2 startPc; u2 lineNumber; } [lineCount] };
1342 * pos is at localVarCount
1344 public void parseLocalVarTableAttr(ClassFileReader reader, Object tag){
1345 int localVarCount = readU2();
1346 setLocalVarTableCount(reader, tag, localVarCount);
1348 for (int i=0; i<localVarCount; i++){
1349 int startPc = readU2();
1350 int length = readU2();
1351 int cpIdx = readU2();
1352 String varName = (String) cpValue[cpIdx];
1354 String descriptor = (String) cpValue[cpIdx];
1355 int slotIndex = readU2();
1357 setLocalVar(reader, tag, i, varName, descriptor, startPc, startPc+length-1, slotIndex );
1360 setLocalVarTableDone(reader, tag);
1364 protected void parseClassAttributes(ClassFileReader reader, int attrCount){
1366 setClassAttributeCount(reader, attrCount);
1368 for (int i=0; i<attrCount; i++){
1369 int cpIdx = readU2();
1370 String name = utf8At(cpIdx);
1372 name = internStdAttrName(cpIdx, name, stdClassAttrs);
1374 int attrLength = readI4(); // actually U4, but we don't support 2GB attributes
1375 setClassAttribute(reader, i, name, attrLength);
1378 setClassAttributesDone(reader);
1383 * (optionally) called by ClassFileReader from within setClassAttribute() notification
1385 * InnerClass { u2 nameIdx<utf8>; u4 length; u2 sourceFile<utf8>; }
1387 public void parseSourceFileAttr(ClassFileReader reader, Object tag){
1388 int cpIdx = readU2();
1389 String pathName = utf8At(cpIdx);
1390 setSourceFile(reader, tag, pathName);
1394 * (optionally) called by ClassFileReader from within setClassAttribute() notification
1400 * { u2 innerCls<cls>;
1402 * u2 innerName<utf8>;
1403 * u2 innerAccessFlags;
1404 * } classes[classCount] }
1407 * pos is at classCount
1409 public void parseInnerClassesAttr(ClassFileReader reader, Object tag){
1410 int innerClsCount = readU2();
1411 setInnerClassCount(reader, tag, innerClsCount);
1413 for (int i = 0; i < innerClsCount; i++) {
1414 int cpIdx = readU2();
1415 String innerClsName = (cpIdx != 0) ? (String) cpValue[cpIdx] : null;
1417 String outerClsName = (cpIdx != 0) ? (String) cpValue[cpIdx] : null;
1419 String innerSimpleName = (cpIdx != 0) ? (String) cpValue[cpIdx] : null;
1420 int accessFlags = readU2();
1422 setInnerClass(reader, tag, i, outerClsName, innerClsName, innerSimpleName, accessFlags);
1425 setInnerClassesDone(reader, tag);
1429 * EnclosingMethod_attribute {
1430 * u2 attribute_name_index;
1431 * u4 attribute_length;
1432 * u2 class_index -> Class_info { u1 tag; u2 name_index->utf8 }
1433 * u2 method_index -> NameAndType_info { u1 tag; u2 name_index->utf8; u2 descriptor_index->utf8 }
1436 public void parseEnclosingMethodAttr(ClassFileReader reader, Object tag){
1437 String enclosedMethod = null;
1438 String descriptor = null;
1440 int cpIdx = readU2(); // start of Class_info
1441 String enclosingClass = nameAt(cpIdx);
1443 cpIdx = readU2(); // start of NameAndType_info
1445 // check if this is inside a method - we also get EnclosingMethod_infos for
1446 // classes that are not immediately enclosed
1448 enclosedMethod = nameAt(cpIdx);
1449 descriptor = descriptorAt(cpIdx);
1452 setEnclosingMethod(reader, tag, enclosingClass, enclosedMethod, descriptor);
1456 * BootstrapMethods_attribute {
1457 * u2 attribute_name_index;
1458 * u4 attribute_length;
1459 * u2 num_bootstrap_methods;
1460 * { u2 bootstrap_method_ref; -> MethodHandle
1461 * u2 num_bootstrap_arguments;
1462 * u2 bootstrap_arguments[num_bootstrap_arguments];
1463 * } bootstrap_methods[num_bootstrap_methods];
1466 * pos is at num_bootstrap_methods
1468 public void parseBootstrapMethodAttr (ClassFileReader reader, Object tag){
1469 int nBootstrapMethods = readU2();
1471 setBootstrapMethodCount(reader, tag, nBootstrapMethods);
1473 for (int i=0; i<nBootstrapMethods; i++){
1474 int cpMhIdx = readU2();
1475 int nArgs = readU2();
1476 int[] bmArgs = new int[nArgs];
1477 for (int j=0; j<nArgs; j++){
1478 bmArgs[j] = readU2();
1481 // kind of this method handle
1482 int refKind = mhRefTypeAt(cpMhIdx);
1484 // CONSTANT_Methodref_info structure
1485 int mrefIdx = mhMethodRefIndexAt(cpMhIdx);
1487 String clsName = methodClassNameAt(mrefIdx);
1488 String mthName = methodNameAt(mrefIdx);
1489 String descriptor = methodDescriptorAt(mrefIdx);
1491 setBootstrapMethod(reader, tag, i, refKind, clsName, mthName, descriptor, bmArgs);
1494 setBootstrapMethodsDone( reader, tag);
1497 String nameAt(int nameTypeInfoIdx) {
1498 return utf8At(u2(cpPos[nameTypeInfoIdx] + 1));
1501 String descriptorAt (int nameTypeInfoIdx){
1502 return utf8At( u2( cpPos[nameTypeInfoIdx]+3));
1505 // those are as per http://java.sun.com/docs/books/jvms/second_edition/ClassFileFormat-Java5.pdf
1511 * u2 const_value_index;
1512 * { u2 type_name_index; u2 const_name_index; } enum_const_value;
1513 * u2 class_info_index;
1514 * annotation annotation_value;
1515 * { u2 num_values; element_value values[num_values]; } array_value;
1518 * valid tags are primitve type codes B,C,D,F,I,J,S,Z
1519 * plus: 's'=String, 'e'=enum, 'c'=class, '@'=annotation, '['=array
1521 void parseAnnotationValue(ClassFileReader reader, Object tag, int annotationIndex, int valueIndex, String elementName, int arrayIndex){
1525 int t = readUByte();
1528 // booleans have to be treated differently since there is no CONSTANT_Boolean, i.e. values are
1529 // stored as CONSTANT_Integer in the constpool, i.e. the cpValue doesn't have the right type
1531 val = cpValue[cpIdx];
1532 val = Boolean.valueOf((Integer)val == 1);
1533 setPrimitiveAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
1538 val = cpValue[cpIdx];
1539 val = Byte.valueOf(((Integer)val).byteValue());
1540 setPrimitiveAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
1545 val = cpValue[cpIdx];
1546 val = Character.valueOf((char)((Integer)val).shortValue());
1547 setPrimitiveAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
1552 val = cpValue[cpIdx];
1553 val = Short.valueOf(((Integer)val).shortValue());
1554 setPrimitiveAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
1562 val = cpValue[cpIdx];
1563 setPrimitiveAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, val);
1568 String s = (String) cpValue[cpIdx];
1569 setStringAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, s);
1574 String enumTypeName = (String)cpValue[cpIdx];
1576 String enumConstName = (String)cpValue[cpIdx];
1577 setEnumAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, enumTypeName, enumConstName);
1582 String className = (String)cpValue[cpIdx];
1583 setClassAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex, className);
1587 parseAnnotation(reader, tag, -1, false); // getting recursive here
1588 setAnnotationFieldValue(reader, tag, annotationIndex, valueIndex, elementName, arrayIndex);
1592 int arrayLen = readU2();
1593 setAnnotationValueElementCount(reader, tag, annotationIndex, valueIndex, elementName, arrayLen);
1594 for (int i=0; i<arrayLen; i++){
1595 parseAnnotationValue(reader, tag, annotationIndex, valueIndex, elementName, i);
1597 setAnnotationValueElementsDone(reader, tag, annotationIndex, valueIndex, elementName);
1605 * u2 num_element_value_pairs;
1607 * u2 element_name_index;
1608 * element_value value;
1609 * } element_value_pairs[num_element_value_pairs]
1612 void parseAnnotation (ClassFileReader reader, Object tag, int annotationIndex, boolean isParameterAnnotation){
1613 int cpIdx = readU2();
1614 String annotationType = (String)cpValue[cpIdx];
1615 boolean parseValues;
1616 if (isParameterAnnotation){
1617 parseValues = setParameterAnnotation(reader, tag, annotationIndex, annotationType);
1619 parseValues = setAnnotation(reader, tag, annotationIndex, annotationType);
1622 parseAnnotationValues(reader, tag, annotationIndex);
1626 void parseAnnotationValues (ClassFileReader reader, Object tag, int annotationIndex){
1627 int nValuePairs = readU2();
1628 setAnnotationValueCount(reader, tag, annotationIndex, nValuePairs);
1630 for (int i=0; i<nValuePairs; i++){
1631 int cpIdx = readU2();
1632 String elementName = (String)cpValue[cpIdx];
1633 parseAnnotationValue(reader, tag, annotationIndex, i, elementName, -1);
1636 setAnnotationValuesDone(reader, tag, annotationIndex);
1640 * class, field, method annotation attributes (only one per target)
1642 * Runtime[In]VisibleAnnotations_attribute {
1643 * u2 attribute_name_index;
1644 * u4 attribute_length;
1645 * u2 num_annotations; << pos
1646 * annotation annotations[num_annotations];
1649 public void parseAnnotationsAttr (ClassFileReader reader, Object tag){
1650 int numAnnotations = readU2();
1651 setAnnotationCount(reader, tag, numAnnotations);
1653 for (int i=0; i<numAnnotations; i++){
1654 parseAnnotation(reader, tag, i, false);
1657 setAnnotationsDone(reader, tag);
1661 // JSR 308 type annotation target types
1662 public static final int CLASS_TYPE_PARAMETER = 0x00;
1663 public static final int METHOD_TYPE_PARAMETER = 0x01;
1664 public static final int CLASS_EXTENDS = 0x10;
1665 public static final int CLASS_TYPE_PARAMETER_BOUND = 0x11;
1666 public static final int METHOD_TYPE_PARAMETER_BOUND = 0x12;
1667 public static final int FIELD = 0x13;
1668 public static final int METHOD_RETURN = 0x14;
1669 public static final int METHOD_RECEIVER = 0x15;
1670 public static final int METHOD_FORMAL_PARAMETER = 0x16;
1671 public static final int THROWS = 0x17;
1672 public static final int LOCAL_VARIABLE = 0x40;
1673 public static final int RESOURCE_VARIABLE = 0x41;
1674 public static final int EXCEPTION_PARAMETER = 0x42;
1675 public static final int INSTANCEOF = 0x43;
1676 public static final int NEW = 0x44;
1677 public static final int CONSTRUCTOR_REFERENCE = 0x45;
1678 public static final int METHOD_REFERENCE = 0x46;
1679 public static final int CAST = 0x47;
1680 public static final int CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT = 0x48;
1681 public static final int METHOD_INVOCATION_TYPE_ARGUMENT = 0x49;
1682 public static final int CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT = 0x4a;
1683 public static final int METHOD_REFERENCE_TYPE_ARGUMENT = 0x4b;
1685 public static String getTargetTypeName (int targetType){
1686 switch (targetType){
1687 case CLASS_TYPE_PARAMETER: return "class type parameter";
1688 case METHOD_TYPE_PARAMETER: return "method type parameter";
1689 case CLASS_EXTENDS: return "super class";
1690 case CLASS_TYPE_PARAMETER_BOUND: return "class type parameter bound";
1691 case METHOD_TYPE_PARAMETER_BOUND: return "method type parameter bound";
1692 case FIELD: return "field";
1693 case METHOD_RETURN: return "method return";
1694 case METHOD_RECEIVER: return "method receiver";
1695 case METHOD_FORMAL_PARAMETER: return "method formal parameter";
1696 case THROWS: return "throws";
1697 case LOCAL_VARIABLE: return "local variable";
1698 case RESOURCE_VARIABLE: return "resource variable";
1699 case EXCEPTION_PARAMETER: return "exception parameter";
1700 case INSTANCEOF: return "instanceof";
1701 case NEW: return "new";
1702 case CONSTRUCTOR_REFERENCE: return "ctor reference";
1703 case METHOD_REFERENCE: return "method reference";
1704 case CAST: return "case";
1705 case METHOD_INVOCATION_TYPE_ARGUMENT: return "method invocation type argument";
1706 case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT: return "ctor reference type argument";
1707 case METHOD_REFERENCE_TYPE_ARGUMENT: return "method reference type argument";
1709 return "<unknown target type 0x" + Integer.toHexString(targetType);
1713 public static String getTypePathEncoding (short[] typePath){
1714 if (typePath == null){
1718 StringBuffer sb = new StringBuffer();
1719 for (int i=0; i<typePath.length;i++){
1720 int e = typePath[i];
1722 sb.append( Integer.toString((e>>8) & 0xff));
1723 sb.append( Integer.toString(e & 0xff));
1727 return sb.toString();
1730 public static String getScopeEncoding (long[] scopeEntries){
1731 StringBuffer sb = new StringBuffer();
1732 for (int i=0; i<scopeEntries.length;i++){
1733 long e = scopeEntries[i];
1734 int slotIndex = (int)(e & 0xffff);
1735 int length = (int)((e >> 16) & 0xffff);
1736 int startPc = (int)((e >> 32) & 0xffff);
1743 sb.append( Integer.toString(startPc));
1745 sb.append( Integer.toString(startPc + length-1));
1747 sb.append(slotIndex);
1750 return sb.toString();
1753 // JSR 308 type annotation, which adds 3 fields to the old annotation structure
1755 // type_annotation {
1756 // u1 target_type; // targeted program element (sec 3.2)
1757 // union { // ?? this is probably packed - variable size unions make no sense
1758 // type_parameter_target;
1759 // supertype_target;
1760 // type_parameter_bound_target;
1762 // method_formal_parameter_target;
1767 // type_argument_target;
1768 // } target_info; // targeted program element (sec 3.3)
1770 // type_path target_path; // encoding of annotation position in compound type (array, generic, etc., sec 3.4)
1772 // // standard annotation fields
1773 // u2 type_index; // the annotation type
1774 // u2 num_element_value_pairs;
1776 // u2 element_name_index;
1777 // element_value value;
1778 // } element_value_pairs[num_element_value_pairs];
1781 // struct type_path {
1783 // type_path_entry path[path_length];
1786 // struct type_path_entry {
1787 // u1 type_path_kind;
1788 // // 0: deeper in array type
1789 // // 1: deeper in nested type
1790 // // 2: bound of wildcard typearg
1791 // // 3: type argument of parameterized type
1792 // u1 type_argument_index;
1793 // // 0, if type_path_kind == 0,1,2
1794 // // 0-based index of type arg in parameterized type if type_path_kind i== 3
1797 int getTargetInfoSize (int targetType){
1798 int len = 3; // max static length are xx_TYPE_ARGUMENTs
1799 if (targetType == LOCAL_VARIABLE || targetType == RESOURCE_VARIABLE){
1800 len = Math.max( len, u2(pos) * 6); // three u2 values per entry
1806 int getTypePathSize (short[] typePath){
1807 int typePathSize = 1;
1808 if (typePath != null) {
1809 typePathSize += typePath.length * 2;
1811 return typePathSize;
1815 short[] readTypePath (){
1816 short[] typePath = null;
1818 int pathLength = readUByte();
1819 if (pathLength > 0){
1820 typePath = new short[pathLength];
1821 for (int i=0; i<pathLength; i++){
1822 int pathKind = (short)readUByte();
1823 int argIdx = (short)readUByte();
1824 typePath[i]= (short)((pathKind << 8) | argIdx);
1831 String readAnnotationType (){
1832 int cpIdx = readU2();
1833 String annotationType = (String)cpValue[cpIdx];
1834 return annotationType;
1837 void setTypeAnnotation (ClassFileReader reader, Object tag, int annotationIndex) {
1838 int targetType = readUByte();
1840 switch (targetType){
1841 case CLASS_TYPE_PARAMETER:
1842 case METHOD_TYPE_PARAMETER: {
1843 // type_parameter_target { u1 type_parameter_index; }
1844 int typeParamIdx = readUByte();
1845 reader.setTypeParameterAnnotation( this, tag, annotationIndex, targetType, typeParamIdx, readTypePath(), readAnnotationType());
1848 case CLASS_EXTENDS: {
1849 // supertype_target { u2 supertype_index; }
1850 int superTypeIdx = readU2();
1851 reader.setSuperTypeAnnotation( this, tag, annotationIndex, targetType, superTypeIdx, readTypePath(), readAnnotationType());
1854 case CLASS_TYPE_PARAMETER_BOUND:
1855 case METHOD_TYPE_PARAMETER_BOUND: {
1856 // type_parameter_bound_target { u1 type_parameter_index; u1 bound_index; }
1857 int typeParamIdx = readUByte();
1858 int boundIdx = readUByte();
1859 reader.setTypeParameterBoundAnnotation(this, tag, annotationIndex, targetType, typeParamIdx, boundIdx, readTypePath(), readAnnotationType());
1863 case METHOD_RECEIVER:
1866 reader.setTypeAnnotation( this, tag, annotationIndex, targetType, readTypePath(), readAnnotationType());
1869 case METHOD_FORMAL_PARAMETER: {
1870 // method_formal_parameter_target { u1 method_formal_parameter_index; }
1871 int formalParamIdx = readUByte();
1872 reader.setFormalParameterAnnotation( this, tag, annotationIndex, targetType, formalParamIdx, readTypePath(), readAnnotationType());
1876 // throws_target { u2 throws_type_index; }
1877 int throwsTypeIdx = readU2();
1878 reader.setThrowsAnnotation( this, tag, annotationIndex, targetType, throwsTypeIdx, readTypePath(), readAnnotationType());
1881 case LOCAL_VARIABLE:
1882 case RESOURCE_VARIABLE: {
1883 // this can't just refer to a LocalVarInfo since those depend on debug compile options
1885 // localvar_target {
1886 // u2 table_length; // number of entries, not bytes
1889 // u2 length; // bytecode offset length
1890 // u2 index; // local var idx
1891 // } table[table_length];
1893 int tableLength = readU2();
1894 long[] scopeEntries = new long[tableLength];
1895 for (int i=0; i<tableLength; i++){
1896 int startPc = readU2();
1897 int length = readU2();
1898 int slotIdx = readU2();
1899 scopeEntries[i] = ((long)startPc << 32) | ((long)length << 16) | slotIdx;
1901 reader.setVariableAnnotation( this, tag, annotationIndex, targetType, scopeEntries, readTypePath(), readAnnotationType());
1904 case EXCEPTION_PARAMETER: {
1905 // catch_target { u2 exception_table_index; }
1906 int exceptionIdx = readU2();
1907 reader.setExceptionParameterAnnotation( this, tag, annotationIndex, targetType, exceptionIdx, readTypePath(), readAnnotationType());
1911 case METHOD_REFERENCE:
1912 case CONSTRUCTOR_REFERENCE:
1914 // offset_target { u2 offset; } // insn offset within bytecode
1915 int offset = readU2();
1916 reader.setBytecodeAnnotation(this, tag, annotationIndex, targetType, offset, readTypePath(), readAnnotationType());
1920 case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
1921 case METHOD_INVOCATION_TYPE_ARGUMENT:
1922 case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
1923 case METHOD_REFERENCE_TYPE_ARGUMENT: {
1924 // type_argument_target {
1926 // u1 type_argument_index;
1928 int offset = readU2();
1929 int typeArgIdx = readUByte();
1930 reader.setBytecodeTypeParameterAnnotation(this, tag, annotationIndex, targetType, offset, typeArgIdx, readTypePath(), readAnnotationType());
1935 // <2do - report this to the reader
1936 throw new RuntimeException("unknown type annotation target: 0x" + Integer.toHexString(targetType));
1941 void parseTypeAnnotation (ClassFileReader reader, Object tag, int annotationIndex) {
1943 // this does the respective setXTypeAnnotation() reader callback
1944 //dumpData(pos, 16);
1945 setTypeAnnotation(reader, tag, annotationIndex);
1947 // now set the annotation value pairs
1948 parseAnnotationValues( reader, tag, annotationIndex);
1952 * Runtime[In]VisibleTypeAnnotations_attribute {
1953 * u2 attribute_name_index;
1954 * u4 attribute_length;
1955 * u2 num_annotations;
1956 * type_annotation annotations[num_annotations];
1959 public void parseTypeAnnotationsAttr (ClassFileReader reader, Object tag) {
1960 int numAnnotations = readU2();
1961 setTypeAnnotationCount(reader, tag, numAnnotations);
1963 for (int i=0; i<numAnnotations; i++){
1964 parseTypeAnnotation(reader, tag, i);
1967 setTypeAnnotationsDone(reader, tag);
1971 * RuntimeInvisibleParameterAnnotations_attribute {
1972 * u2 attribute_name_index;
1973 * u4 attribute_length;
1974 * u1 num_parameters; << pos
1976 * u2 num_annotations;
1977 * annotation annotations[num_annotations];
1978 * } parameter_annotations[num_parameters];
1981 public void parseParameterAnnotationsAttr(ClassFileReader reader, Object tag){
1982 int numParameters = readUByte();
1983 setParameterCount(reader, tag, numParameters);
1984 for (int i=0; i<numParameters; i++){
1985 int numAnnotations = readU2();
1987 setParameterAnnotationCount(reader, tag, i, numAnnotations);
1988 for (int j=0; j<numAnnotations; j++){
1989 parseAnnotation(reader, tag, j, true);
1991 setParameterAnnotationsDone(reader, tag, i);
1993 setParametersDone(reader, tag);
1997 * Signature_attribute {
1998 * u2 attribute_name_index;
2000 * u2 signature-index << pos
2003 public void parseSignatureAttr(ClassFileReader reader, Object tag){
2004 int cpIdx = readU2();
2005 setSignature(reader, tag, utf8At(cpIdx));
2010 * AnnotationDefault_attribute {
2011 * u2 attribute_name_index;
2012 * u4 attribute_length;
2013 * element_value default_value; << pos
2016 public void parseAnnotationDefaultAttr(ClassFileReader reader, Object tag){
2017 parseAnnotationValue(reader, tag, -1, -1, null, -1);
2021 // EnclosingMethod_attribute {
2022 // u2 attribute_name_index;
2023 // u4 attribute_length;
2028 // LocalVariableTypeTable_attribute { // Code attr
2029 // u2 attribute_name_index;
2030 // u4 attribute_length;
2031 // u2 local_variable_type_table_length;
2036 // u2 signature_index;
2038 // } local_variable_type_table[local_variable_type_table_length];
2044 public void parseBytecode(JVMByteCodeReader reader, Object tag, int codeLength){
2051 boolean isWide = false; // modifier for Xload,Xstore,ret and iinc
2054 int endPos = pos+codeLength;
2058 while (pos < endPos){
2059 pc = pos - startPos;
2061 int opcode = readUByte();
2066 case 1: // aconst_null
2067 reader.aconst_null();
2069 case 2: // iconst_m1
2093 case 10: // lconst_1
2096 case 11: // fconst_0
2099 case 12: // fconst_1
2102 case 13: // fconst_2
2105 case 14: // dconst_0
2108 case 15: // dconst_1
2112 constVal = readByte();
2113 reader.bipush(constVal);
2116 constVal = readI2();
2117 reader.sipush(constVal);
2120 cpIdx = readUByte();
2125 reader.ldc_w_(cpIdx);
2129 reader.ldc2_w(cpIdx);
2132 localVarIndex = isWide ? readU2() : readUByte();
2133 reader.iload(localVarIndex);
2136 localVarIndex = isWide ? readU2() : readUByte();
2137 reader.lload(localVarIndex);
2140 localVarIndex = isWide ? readU2() : readUByte();
2141 reader.fload(localVarIndex);
2144 localVarIndex = isWide ? readU2() : readUByte();
2145 reader.dload(localVarIndex);
2148 localVarIndex = isWide ? readU2() : readUByte();
2149 reader.aload(localVarIndex);
2236 localVarIndex = isWide ? readU2() : readUByte();
2237 reader.istore(localVarIndex);
2240 localVarIndex = isWide ? readU2() : readUByte();
2241 reader.lstore(localVarIndex);
2244 localVarIndex = isWide ? readU2() : readUByte();
2245 reader.fstore(localVarIndex);
2248 localVarIndex = isWide ? readU2() : readUByte();
2249 reader.dstore(localVarIndex);
2252 localVarIndex = isWide ? readU2() : readUByte();
2253 reader.astore(localVarIndex);
2255 case 59: // istore_0
2258 case 60: // istore_1
2261 case 61: // istore_2
2264 case 62: // istore_3
2267 case 63: // lstore_0
2270 case 64: // lstore_1
2273 case 65: // lstore_2
2276 case 66: // lstore_3
2279 case 67: // fstore_0
2282 case 68: // fstore_1
2285 case 69: // fstore_2
2288 case 70: // fstore_3
2303 case 75: // astore_0
2306 case 76: // astore_1
2309 case 77: // astore_2
2312 case 78: // astore_3
2476 localVarIndex = readU2();
2477 constVal = readI2();
2479 localVarIndex = readUByte();
2480 constVal = readByte();
2482 reader.iinc(localVarIndex, constVal);
2546 reader.ifeq(offset);
2550 reader.ifne(offset);
2554 reader.iflt(offset);
2558 reader.ifge(offset);
2562 reader.ifgt(offset);
2566 reader.ifle(offset);
2568 case 159: // if_icmpeq
2570 reader.if_icmpeq(offset);
2572 case 160: // if_icmpne
2574 reader.if_icmpne(offset);
2576 case 161: // if_icmplt
2578 reader.if_icmplt(offset);
2580 case 162: // if_icmpge
2582 reader.if_icmpge(offset);
2584 case 163: // if_icmpgt
2586 reader.if_icmpgt(offset);
2588 case 164: // if_icmple
2590 reader.if_icmple(offset);
2592 case 165: // if_acmpeq
2594 reader.if_acmpeq(offset);
2596 case 166: // if_acmpne
2598 reader.if_acmpne(offset);
2602 reader.goto_(offset);
2609 localVarIndex = isWide ? readU2() : readUByte();
2610 reader.ret(localVarIndex);
2612 case 170: // tableswitch
2613 pos = (((pc+4)>>2)<<2)+startPos; // skip over padding
2615 defaultOffset = readI4();
2617 int high = readI4();
2619 int len = high-low+1;
2620 nextPos = pos + len*4;
2621 reader.tableswitch(defaultOffset, low, high);
2624 case 171: // lookupswitch
2625 pos = (((pc+4)>>2)<<2)+startPos; // skip over padding
2627 defaultOffset = readI4();
2628 int nPairs = readI4();
2630 nextPos = pos + (nPairs*8);
2631 reader.lookupswitch(defaultOffset, nPairs);
2634 case 172: // ireturn
2637 case 173: // lreturn
2640 case 174: // freturn
2643 case 175: // dreturn
2646 case 176: // areturn
2652 case 178: // getstatic
2653 cpIdx = readU2(); // CP index of fieldRef
2654 reader.getstatic(cpIdx);
2656 case 179: // putstatic
2657 cpIdx = readU2(); // CP index of fieldRef
2658 reader.putstatic(cpIdx);
2660 case 180: // getfield
2661 cpIdx = readU2(); // CP index of fieldRef
2662 reader.getfield(cpIdx);
2664 case 181: // putfield
2665 cpIdx = readU2(); // CP index of fieldRef
2666 reader.putfield(cpIdx);
2668 case 182: // invokevirtual
2669 cpIdx = readU2(); // CP index of methodRef
2670 reader.invokevirtual(cpIdx);
2672 case 183: // invokespecial
2673 cpIdx = readU2(); // CP index of methodRef
2674 reader.invokespecial(cpIdx);
2676 case 184: // invokestatic
2677 cpIdx = readU2(); // CP index of methodRef
2678 reader.invokestatic(cpIdx);
2680 case 185: // invokeinterface
2681 cpIdx = readU2(); // CP index of methodRef
2682 int count = readUByte();
2683 int zero = readUByte(); // must be 0
2684 reader.invokeinterface(cpIdx, count, zero);
2686 case 186: // invokedynamic
2687 cpIdx = readU2(); // CP index of bootstrap method
2690 reader.invokedynamic(cpIdx);
2696 case 188: // newarray
2697 int aType = readUByte();
2698 reader.newarray(aType);
2700 case 189: // anewarray
2701 cpIdx = readU2(); // CP index of component type
2702 reader.anewarray(cpIdx);
2704 case 190: // arraylength
2705 reader.arraylength();
2710 case 192: // checkcast
2711 cpIdx = readU2(); // cast type cp index
2712 reader.checkcast(cpIdx);
2714 case 193: // instanceof
2715 cpIdx = readU2(); // check type cp index
2716 reader.instanceof_(cpIdx);
2718 case 194: // monitorenter
2719 reader.monitorenter();
2721 case 195: // monitorexit
2722 reader.monitorexit();
2726 // affects immediate operand width if next bytecode is:
2727 // iload,fload,aload,lload,dload,
2728 // istore,fstore,astore,lstore,dstore
2732 case 197: // multianewarray
2734 int dimensions = readUByte();
2735 reader.multianewarray(cpIdx, dimensions);
2739 reader.ifnull(offset);
2741 case 199: // ifnonnull
2743 reader.ifnonnull(offset);
2747 reader.goto_w(offset);
2751 reader.jsr_w(offset);
2756 reader.unknown(opcode);
2759 isWide = false; // reset wide modifier
2764 //--- those can only be called from within a JVMByteCodeReader.tableswitch() notification
2765 public void parseTableSwitchEntries(JVMByteCodeReader reader, int low, int high){
2766 for (int val=low; val<=high; val++){
2767 int offset = readI4();
2768 reader.tableswitchEntry(val, offset);
2771 public int getTableSwitchOffset(int low, int high, int defaultOffset, int val){
2772 if (val < low || val > high){
2773 return defaultOffset;
2776 int n = Math.abs(val - low);
2778 int pcOffset = readI4();
2783 //--- those can only be called from within a JVMByteCodeReader.lookupswitch() notification
2784 public void parseLookupSwitchEntries(JVMByteCodeReader reader, int nEntries){
2785 for (int i=0; i<nEntries; i++){
2786 int value = readI4();
2787 int offset = readI4();
2788 reader.lookupswitchEntry(i, value, offset);
2791 public int getLookupSwitchOffset(int nEntries, int defaultOffset, int val){
2792 for (int i=0; i<nEntries; i++){
2793 int match = readI4();
2796 } else if (val == match) {
2797 int offset = readI4();
2803 return defaultOffset;