概述
- 字节码采用Big-Endian,是Spark、PowerPC等处理器的默认字节序;x86则是用Little-Endian
jvm-001.jpg
- 字节码文件采用一种类似于C语言结构体的伪结构来存储数据,只有两种数据类型:无符号数和表
- 无符号数
- 基本数据类型,以u1,u2,u4,u8分别代表1,2,4,8字节的无符号数
- 可用来描述数字、索引引用、数量值或者按UTF-8编码构成字符串值
- 表
- 由多个无符号数或者其他表作为数据项构成的复合数据类型
- 习惯性地以“_info”结尾
- 整个Class文件本质上也就是一张表
- 无符号数
文件结构
ClassFile {
u4 magic; # 0xCAFEBABE
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info contant_pool[constant_pool_count – 1]; # 之所以-1,是因为#0被做他用了
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];
}
magic, minor_version, major_version
specifies information about the version of the class, and the version of the JDK this class was compiled for
constant_pool
similar to a symbol table although it contains more data this is described in more detail below
access_flags
provides the list of modifiers for this class
this_class
index into the constant_pool providing the fully qualified name of this class i.e. org/jamesdbloom/foo/Bar
sup