java Class文件結(jié)構(gòu)解析常量池字節(jié)碼
Class文件結(jié)構(gòu)
整體結(jié)構(gòu)
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]; }
從結(jié)構(gòu)上看可以分為幾大塊 文件頭、常量池、接口、字段、函數(shù)、屬性
文件頭
u4 magic; //固定值 0xCAFEBABE
u2 minor_version;
u2 major_version;
u2 access_flags; //訪問(wèn)修飾
u2 this_class; //類(lèi)名 常量池下標(biāo)
u2 super_class; //父類(lèi)名 常量池下標(biāo) 如果是0 就是java/lang/Object;
我把這幾個(gè)描述了類(lèi)基本信息的字段稱(chēng)為文件頭
major_version.minor_version表示該class文件的版本號(hào),由編譯器版本決定,然而不同版本的虛擬機(jī)只會(huì)支持一定版本范圍內(nèi)的class文件,如果不在則會(huì)拒絕解析。
例如 openJDK中的實(shí)現(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ù)標(biāo)識(shí)/類(lèi)型信息/字符串等等,運(yùn)行時(shí)加載到內(nèi)存中形成運(yùn)行時(shí)常量池
常量項(xiàng)中文件中使用變長(zhǎng)結(jié)構(gòu)描述
cp_info { u1 tag; //表示常量的類(lèi)型 u1 info[]; //具體常量的內(nèi)容結(jié)構(gòu) 不同類(lèi)型常量有不同的結(jié)構(gòu) } /*常量類(lèi)型*/ 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; //字符串長(zhǎng)度 u1 bytes[length]; //字符串?dāng)?shù)據(jù)(utf-8編碼) }
解析常量池的時(shí)候要注意:常量池長(zhǎng)度為 constant_pool_count -1 但是 下標(biāo)從1開(kāi)始
屬性表
attribute_info { u2 attribute_name_index; u4 attribute_length; u1 info[attribute_length]; }
屬性項(xiàng)可以包含在class、method、field、code中,作為具體信息項(xiàng)
在class中可以描述文件信息,在method中可以描述字節(jié)碼內(nèi)容,函數(shù)棧信息,在code中可以描述行號(hào)等 所以attribute_info同樣是具備不同類(lèi)型的變長(zhǎng)結(jié)構(gòu)。但是attribute_info并沒(méi)有tag這樣的專(zhuān)門(mén)標(biāo)記去標(biāo)識(shí)類(lèi)型,而是使用名字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結(jié)構(gòu)
Code_attribute { u2 attribute_name_index; u4 attribute_length; u2 max_stack; //最大棧深度 u2 max_locals; //局部變量數(shù)量 u4 code_length; //字節(jié)碼內(nèi)容長(zhǎng)度 u1 code[code_length]; //字節(jié)碼內(nèi)容 u2 exception_table_length; //異常處理表 由try,catch/finaly 產(chǎn)生 { 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; //一定會(huì)包含一個(gè)code屬性項(xiàng) 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ù)/字段中的內(nèi)容具體有屬性來(lái)描述
字節(jié)碼解析
對(duì)于class文件解析,我們最關(guān)心的可能就兩個(gè) 常量池和字節(jié)碼
一條字節(jié)碼由操作碼,操作數(shù)組成,不同的字節(jié)碼操作數(shù)的長(zhǎng)度/表示意義可能不一樣,對(duì)著規(guī)范翻譯就好
例如invokevirtual字節(jié)碼就是 0xb6 u2 2字節(jié)的操作數(shù)在運(yùn)行時(shí)指向的是一個(gè)instance method ref
參考文檔
本文代碼 ClassParserDemo
以上就是java Class文件結(jié)構(gòu)解析常量池字節(jié)碼的詳細(xì)內(nèi)容,更多關(guān)于java Class 文件解析的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring的構(gòu)造函數(shù)注入屬性@ConstructorBinding用法
這篇文章主要介紹了關(guān)于spring的構(gòu)造函數(shù)注入屬性@ConstructorBinding用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12獲取系統(tǒng)參數(shù)System.getProperties()與配置文件參數(shù)@Value(“${key}“)
這篇文章主要介紹了獲取系統(tǒng)參數(shù)System.getProperties()與配置文件參數(shù)@Value("${key}"),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05SpringCloud Gateway跨域配置代碼實(shí)例
這篇文章主要介紹了SpringCloud Gateway跨域配置代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11gradle項(xiàng)目中資源文件的相對(duì)路徑打包技巧必看
這篇文章主要介紹了gradle項(xiàng)目中資源文件的相對(duì)路徑打包技巧必看篇,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11Java中l(wèi)ist.foreach不能使用字符串拼接的問(wèn)題
這篇文章主要介紹了Java中l(wèi)ist.foreach不能使用字符串拼接的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09springboot手寫(xiě)一個(gè)自己的starter源碼
在本篇文章里小編給大家整理了關(guān)于springboot手寫(xiě)一個(gè)自己的starter源碼的全部知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-06-06