class文件结构
访问标记
类型标记
常量池
cp_info {
u1 tag; u1 info[]; }
常量类型表
1) CONSTANT_Class
The CONSTANT_Class_info structure is used to represent a class or an interface:
name_index 指向 CONSTANT_Utf8_info
2) CONSTANT_Fieldref, CONSTANT_Methodref, and CONSTANT_InterfaceMethodref
Fields, methods, and interface methods are represented by similar structures:
class_index 指向 CONSTANT_Class_info
name_and_type_index 指向 CONSTANT_Methodref_info CONSTANT_InterfaceMethodref_info
3) CONSTANT_String
The CONSTANT_String_info structure is used to represent constant objects of the type java.lang.String:
string_index 指向 CONSTANT_Utf8_info
4) CONSTANT_Integer and CONSTANT_Float
The CONSTANT_Integer_info and CONSTANT_Float_info structures represent four-byte numeric (int and float) constants:
5) CONSTANT_Long and CONSTANT_Double
The CONSTANT_Long_info and CONSTANT_Double_info represent eight-byte numeric (long and double) constants:
6) CONSTANT_NameAndType
The CONSTANT_NameAndType_info structure is used to represent a field or method, without indicating which class or interface type it belongs to:
name_index 指向 CONSTANT_Utf8_info
descriptor_index 指向 CONSTANT_Utf8_info
7) CONSTANT_Utf8
The CONSTANT_Utf8_info structure is used to represent constant string values.
字段
The items of the field_info structure are as follows:
access_flags
name_index 指向CONSTANT_Utf8_info
descriptor_index 指向 CONSTANT_Utf8_info
attributes_count 属性计数
attributes[] 所有属性
方法
Each method, and each instance initialization method <init>, is described by a variable-length method_info structure. The structure has the following format:
access_flags
name_index 指向CONSTANT_Utf8_info
descriptor_index 指向 CONSTANT_Utf8_info
attributes_count 属性计数
attributes[] 所有属性
属性
1) SourceFile Attribute
attribute_name_index 指向 CONSTANT_Utf8_info "SourceFile"
attribute_length 属性长度 = 2
sourcefile_index 指向CONSTANT_Utf8_info的源文件名 如:foo.java ,没有路径
2) ConstantValue Attribute
attribute_name_index 指向 CONSTANT_Utf8_info "ConstantValue"
attribute_length 定长2
constantvalue_index 指向常量池中的值,可以为以下类型
Field Type | Entry Type |
long | CONSTANT_Long |
float | CONSTANT_Float |
double | CONSTANT_Double |
int, short, char, byte, boolean | CONSTANT_Integer |
java.lang.String | CONSTANT_String |
3) Code Attribute
The items of the Code_attribute structure are as follows:
attribute_name_index 指向 CONSTANT_Utf8_info "Code"
attribute_length 属性长度,不包括初始的6字节
max_stack 方法的最大栈大小
max_locals 最大局部变量数
code_length 代码长度
code[] 代码数据
exception_table_length 异常表长度
exception_table[] 异常表
start_pc, end_pc 异常捕获的指令范围
handler_pc 捕获后的跳转位置
catch_type 指向CONSTANT_Class_info, 捕获类型
attributes_count 属性计数
attributes[] 所有属性
4) Exceptions Attribute(方法throws的异常)
attribute_name_index 指向 CONSTANT_Utf8_info "Exceptions"
attribute_length 不包括初始的6字节
number_of_exceptions 异常数
exception_index_table[] 异常表 指向CONSTANT_Class_info
5) LineNumberTable Attribute(行号表)
attribute_name_index 指向CONSTANT_Utf8_info "LineNumberTable"
attribute_length 不包括初始的6字节
line_number_table_length 行号表长度
line_number_table[] 行号表
start_pc 开始的指令位置
line_number 行号,对应于源文件
6) LocalVariableTable Attribute(局部变量表)
attribute_name_index 指向 CONSTANT_Utf8_info "LocalVariableTable"
attribute_length 不包括初始的6字节
local_variable_table_length 局部变量表长度
local_variable_table[] 局部变量表
start_pc, length 指令开始位置,长度
name_index, descriptor_index 名字索引,描述, 指向CONSTANT_Utf8_info
index 局部变量索引,也就是变量占用的slot