ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; }
從結構上看能夠分爲幾大塊 文件頭、常量池、接口、字段、函數、屬性html
u4 magic; //固定值 0xCAFEBABE u2 minor_version; u2 major_version; u2 access_flags; //訪問修飾 u2 this_class; //類名 常量池下標 u2 super_class; //父類名 常量池下標 若是是0 就是java/lang/Object; </pre></code> 我把這幾個描述了類基本信息的字段稱爲文件頭 major_version.minor_version表示該class文件的版本號,由編譯器版本決定,然而不一樣版本的虛擬機只會支持必定版本範圍內的class文件,若是不在則會拒絕解析。 例如 openJDK中的實現 <pre><code> // Check version numbers - we check this even with verifier off if (!is_supported_version(major_version, minor_version)) { if (name == NULL) { Exceptions::fthrow( THREAD_AND_LOCATION, vmSymbols::java_lang_UnsupportedClassVersionError(), "Unsupported major.minor version %u.%u", major_version, minor_version); }
常量池包含了class文件中使用到的例如 函數標識/類型信息/字符串等等,運行時加載到內存中造成運行時常量池java
常量項中文件中使用變長結構描述git
cp_info { u1 tag; //表示常量的類型 u1 info[]; //具體常量的內容結構 不一樣類型常量有不一樣的結構 } /*常量類型*/ Constant Type Value CONSTANT_Class 7 CONSTANT_Fieldref 9 CONSTANT_Methodref 10 CONSTANT_InterfaceMethodref 11 CONSTANT_String 8 CONSTANT_Integer 3 CONSTANT_Float 4 CONSTANT_Long 5 CONSTANT_Double 6 CONSTANT_NameAndType 12 CONSTANT_Utf8 1 CONSTANT_MethodHandle 15 CONSTANT_MethodType 16 CONSTANT_InvokeDynamic 18
例如:Utf8_info常量,更多的能夠查看規範github
CONSTANT_Utf8_info { u1 tag; u2 length; //字符串長度 u1 bytes[length]; //字符串數據(utf-8編碼) }
解析常量池的時候要注意:常量池長度爲 constant_pool_count -1 可是 下標從1開始數組
attribute_info { u2 attribute_name_index; u4 attribute_length; u1 info[attribute_length]; }
屬性項能夠包含在class、method、field、code中,做爲具體信息項oracle
在class中能夠描述文件信息,在method中能夠描述字節碼內容,函數棧信息,在code中能夠描述行號等 因此attribute_info一樣是具有不一樣類型的變長結構。可是attribute_info並無tag這樣的專門標記去標識類型,而是使用名字attribute_name。jvm
//一部分Attribute Name Attribute Section class file Java SE ConstantValue §4.7.2 45.3 1.0.2 Code §4.7.3 45.3 1.0.2 StackMapTable §4.7.4 50.0 6 Exceptions §4.7.5 45.3 1.0.2 InnerClasses §4.7.6 45.3 1.1 //.......
Code_attribute結構函數
Code_attribute { u2 attribute_name_index; u4 attribute_length; u2 max_stack; //最大棧深度 u2 max_locals; //局部變量數量 u4 code_length; //字節碼內容長度 u1 code[code_length]; //字節碼內容 u2 exception_table_length; //異常處理表 由try,catch/finaly 產生 { u2 start_pc; u2 end_pc; u2 handler_pc; u2 catch_type; } exception_table[exception_table_length]; u2 attributes_count; attribute_info attributes[attributes_count]; }
method_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; //必定會包含一個code屬性項 attribute_info attributes[attributes_count]; } field_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; }
能夠看到函數/字段中的內容具體有屬性來描述this
對於class文件解析,咱們最關心的可能就兩個 常量池和字節碼編碼
一條字節碼由操做碼,操做數組成,不一樣的字節碼操做數的長度/表示意義可能不同,對着規範翻譯就好
例如invokevirtual字節碼就是 0xb6 u2 2字節的操做數在運行時指向的是一個instance method ref