java Class文件結構解析常量池字節(jié)碼
Class文件結構
整體結構
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]; }
從結構上看可以分為幾大塊 文件頭、常量池、接口、字段、函數(shù)、屬性
文件頭
u4 magic; //固定值 0xCAFEBABE
u2 minor_version;
u2 major_version;
u2 access_flags; //訪問修飾
u2 this_class; //類名 常量池下標
u2 super_class; //父類名 常量池下標 如果是0 就是java/lang/Object;
我把這幾個描述了類基本信息的字段稱為文件頭
major_version.minor_version表示該class文件的版本號,由編譯器版本決定,然而不同版本的虛擬機只會支持一定版本范圍內的class文件,如果不在則會拒絕解析。
例如 openJDK中的實現(xiàn)
// 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文件中使用到的例如 函數(shù)標識/類型信息/字符串等等,運行時加載到內存中形成運行時常量池
常量項中文件中使用變長結構描述
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常量,更多的可以查看規(guī)范
CONSTANT_Utf8_info { u1 tag; u2 length; //字符串長度 u1 bytes[length]; //字符串數(shù)據(jù)(utf-8編碼) }
解析常量池的時候要注意:常量池長度為 constant_pool_count -1 但是 下標從1開始
屬性表
attribute_info { u2 attribute_name_index; u4 attribute_length; u1 info[attribute_length]; }
屬性項可以包含在class、method、field、code中,作為具體信息項
在class中可以描述文件信息,在method中可以描述字節(jié)碼內容,函數(shù)棧信息,在code中可以描述行號等 所以attribute_info同樣是具備不同類型的變長結構。但是attribute_info并沒有tag這樣的專門標記去標識類型,而是使用名字attribute_name。
//一部分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; //局部變量數(shù)量 u4 code_length; //字節(jié)碼內容長度 u1 code[code_length]; //字節(jié)碼內容 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]; }
函數(shù)表/字段表
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]; }
可以看到函數(shù)/字段中的內容具體有屬性來描述
字節(jié)碼解析
對于class文件解析,我們最關心的可能就兩個 常量池和字節(jié)碼
一條字節(jié)碼由操作碼,操作數(shù)組成,不同的字節(jié)碼操作數(shù)的長度/表示意義可能不一樣,對著規(guī)范翻譯就好
例如invokevirtual字節(jié)碼就是 0xb6 u2 2字節(jié)的操作數(shù)在運行時指向的是一個instance method ref
參考文檔
本文代碼 ClassParserDemo
以上就是java Class文件結構解析常量池字節(jié)碼的詳細內容,更多關于java Class 文件解析的資料請關注腳本之家其它相關文章!
相關文章
spring的構造函數(shù)注入屬性@ConstructorBinding用法
這篇文章主要介紹了關于spring的構造函數(shù)注入屬性@ConstructorBinding用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12獲取系統(tǒng)參數(shù)System.getProperties()與配置文件參數(shù)@Value(“${key}“)
這篇文章主要介紹了獲取系統(tǒng)參數(shù)System.getProperties()與配置文件參數(shù)@Value("${key}"),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05Java中l(wèi)ist.foreach不能使用字符串拼接的問題
這篇文章主要介紹了Java中l(wèi)ist.foreach不能使用字符串拼接的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09