Class文件

2021-07-14  本文已影响0人  Shaw_Young

字节码文件里面是什么?

源代码经过编译器编译之后便会生成一个字节码文件,字节码是一种二进制的类文件,它的内容是JVM的指令,而不像C、C++经由编译器直接生成机器码

什么是字节码指令?(byte code)

Java虚拟机的指令由一个字节长度的、代表某种特定操作含义的操作码(opcode)以及跟随其后的零至多个代表此操作所需参数的操作数(operand)所构成。虚拟机中许多指令不包含操作数,只有一个操作码。

Class文件本质和内部数据类型

Class文件格式采用一种类似C语言结构体的方式进行数据存储,这种结构中只有两种类型:无符号数

换句话说,充分理解了每一个字节码文件的细节,自己也可以反编译出Java源文件来。

Class文件的总体结构如下:
1、魔数
2、Class文件版本
3、常量池
4、访问标志
5、类索引,父类索引,接口索引集合
6、字段表集合
7、方法表集合
8、属性表集合

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];
}
类型 名称 说明 长度 数量
u4 magic 魔数,识别Class文件个数 4个字节 1
u2 minor_version 副本版本号(小版本) 2个字节 1
u2 major_version 主版本号(大版本) 2个字节 1
u2 constant_pool_count 常量池计数器 2个字节 1
cp_info constant_pool 常量池表 n个字节 constant_pool_count-1
u2 access_flags 访问标识 2个字节 1
u2 this_class 类索引 2个字节 1
u2 super_class 父类索引 2个字节 1
u2 interfaces_count 接口计数器 2个字节 1
u2 interfaces 接口索引集合 2个字节 interfaces_count
u2 fields_count 字段计数器 2个字节 1
field_info fields 字段表 n个字节 fields_count
u2 methods_count 方法计数器 2个字节 1
method_info methods 方法表 n个字节 methods_count
u2 attributes_count 属性计数器 2个字节 1
attribute_info attributes 属性表 n个字节 attributes_count
上一篇 下一篇

猜你喜欢

热点阅读