class文件結(jié)構(gòu)
- Magic Number
- CA FE BA BE
- Minor Version
- class文件的版本號(hào)
- Major Version
- constant_pool_count
- constant_pool 長度為constant_pool_count-1的表
- 1 CONSTANT_Utf8_info
- tag:1 占用一個(gè)字節(jié)的空間
- length 占用的字節(jié)數(shù)
- bytes 長度為length的字符串
- 3 CONSTANT_Integer_info
- tag:3
- bytes 4個(gè)字節(jié)寿酌,Big-Endian(高位在前)存儲(chǔ)int值
- 4 CONSTANT_Float_info
- tag:4
- bytes 4個(gè)字節(jié)柄慰,Big-Endian存儲(chǔ)float值
- 5 CONSTANT_Long_info
- tag:5
- bytes 8個(gè)字節(jié),Big-Endian存儲(chǔ)long值
- 6 CONSTANT_Double_info
- tag:6
- bytes 8個(gè)字節(jié)煞肾,Big-Endian存儲(chǔ)double值
- 7 CONSTANT_Class_info
- tag:7
- index 2字節(jié)指向類全限定明項(xiàng)的索引
- 8 CONSTANT_String_info
- tag:8
- index 2字節(jié)指向字符串自變量的索引
- 9 CONSTANT_Fieldref_info
- tag:9
- index 2字節(jié)指向聲明字段的類或者接口描述符CONSTANT_Class_info的索引項(xiàng)
- index 2字節(jié)指向字段描述符CONSTANT_NameAndType的索引項(xiàng)
- 10 CONSTANT_Methodref_info
- tag:10
- index 2字節(jié)指向聲明字段的類或者接口描述符CONSTANT_Class_info的索引項(xiàng)
- index 2字節(jié)指向字段描述符CONSTANT_NameAndType的索引項(xiàng)
- 11 CONSTANT_InterfaceMethodref_info
- tag:11
- index 2字節(jié)指向聲明字段的類或者接口描述符CONSTANT_Class_info的索引項(xiàng)
- index 2字節(jié)指向字段描述符CONSTANT_NameAndType的索引項(xiàng)
- 12 CONSTANT_NameAndType_info
- tag:12
- index 2字節(jié)指向該字段或方法名稱常量項(xiàng)的索引
- index 2字節(jié)指向該字段或方法描述符常量項(xiàng)的索引
- 15 CONSTANT_MethodHandle_info
- tag:15
- reference_kind 1字節(jié) 1-9之間的一個(gè)值条辟,決定了方法句柄的類型冀偶。方法句柄類型的值標(biāo)示方法句柄的字節(jié)碼行為鬓梅。
- reference_index 2字節(jié) 對(duì)常量池的有效引用
- 16 CONSTANT_MethodType_info
- tag:16
- descriptor_index 2字節(jié) 指向Utf8_info結(jié)構(gòu)表示的方法描述符
- 18 CONSTANT_InvokeDynamic_info
- tag:18
- bootstrap_method_attr_index 2字節(jié) 當(dāng)前Class文件中引導(dǎo)方法表bootstrap_methods[]數(shù)組的有效索引
- name_and_type_index 2字節(jié) 指向NameAndType_info表示的方法名和方法描述符
- 1 CONSTANT_Utf8_info
- access_flags (位運(yùn)算)
- ACC_PUBLIC 0x0001 是否為public類型
- ACC_FINAL 0x0010 是否為final類型
- ACC_SUPER 0x0020 該標(biāo)志必須為真,JDK1.0.2之后編譯出來的內(nèi)容必須為真况凉,指明invokespectial指令使用新語義
- ACC_INTERFACE 0x0200 是否為接口
- ACC_ABSTRACT 0x0400 是否為接口或抽象類
- ACC_SYNTHETIC 0x1000 編譯器自動(dòng)生成谚鄙,非用戶代碼產(chǎn)生
- ACC_ANNOTATION 0x2000 是否為注解
- ACC_ENUM 0x4000
- this_class
- super_class
- interfaces_count
- interfaces
- fields_count
- fields
- access_flags 2字節(jié)
- ACC_PUBLIC 0x0001
- ACC_PRIVATE 0x0002
- ACC_PROTECTED 0x0004
- ACC_STATIC 0x0008
- ACC_FINAL 0x0010
- ACC_VOLATILE 0x0040
- ACC_TRANSIENT 0x0080
- ACC_SYNTHETIC 0x1000
- ACC_ENUM 0x4000
- name_index u2
- descriptor_index u2
- B - byte
- C - char
- D - double
- F - float
- I - int
- J - long
- S - short
- Z - boolean
- V - void
- L - Object
- 數(shù)組[ -
- 一維數(shù)組[B[Ljava/lang/String
- 多維數(shù)組[[C[[[Ljava/lang/String
- attributes_count
- attributes
- access_flags 2字節(jié)
- methods_count
- methods
- access_flags 2字節(jié)
- ACC_PUBLIC 0x0001
- ACC_PRIVATE 0x0002
- ACC_PROTECTED 0x0004
- ACC_STATIC 0x0008
- ACC_FINAL 0x0010
- ACC_SYNTHETIC 0x020
- ACC_BRIDGE 0x0040 編譯器產(chǎn)生的橋接方法
- ACC_VARARGS 0x0080
- ACC_NATIVE 0x0100
- ACC_ABSTRACT 0x0400
- ACC_STRICTFP 0x0800
- ACC_SYNTHETIC 0x1000
- name_index u2
- descriptor_index u2
- 先參數(shù)列表(放在小括號(hào)內(nèi)部)后返回值
- void m() -> ()V
- String toString() -> ()Ljava/lang/String;
- long pos(int[] arr1, int arr2, long length) -> ([IIJ)J
- attributes_count
- attributes
- access_flags 2字節(jié)
- attributes_count
- attributes
- 既有預(yù)定義屬性,也可以自定義刁绒,java虛擬機(jī)自動(dòng)忽略不認(rèn)識(shí)的屬性
- Code - 方法表 - 該方法編譯成的字節(jié)碼指令
- u2 attribute_name_index
- u4 attribute_length
- u2 max_stack
- u2 max_locals
- u4 code_length
- code
- u2 exception_table_length
- exception_table
- u2 attribute_count
- attributes
- ConstantValue - 字段表 - final關(guān)鍵字定義的常量值
- Deprecated - 類闷营、方法表、字段表
- Exceptions - 方法表
- EnchosingMethod - 類文件 - 局部類或匿名類的外部封裝方法
- InnerClass - 類文件 - 內(nèi)部類列表
- LineNumberTable - Code屬性 - java源碼的行號(hào)與字節(jié)碼指令的對(duì)應(yīng)關(guān)系
- LocalVariableTable - Code屬性 - 方法局部變量表
- SourceFile - 類文件 - 源文件名稱
IDEA可以安裝插件查看class文件結(jié)構(gòu)
jclasslib
https://plugins.jetbrains.com/plugin/index?xmlId=jclasslib
可以選中編譯完成的class文件膛锭,在idea菜單view下有個(gè)show bytecode with jclasslib
jclasslib.png
class文件結(jié)構(gòu)