Java Class类文件结构

3 篇文章 0 订阅

class 文件结构

https://www.viralpatel.net/tutorial-java-class-file-format-revealed/

A Java class file is consist of 10 basic sections:
Magic Number: 0xCAFEBABE
Version of Class File Format: the minor and major versions of the class file
Constant Pool: Pool of constants for the class
Access Flags: for example whether the class is abstract, static, etc.
This Class: The name of the current class
Super Class: The name of the super class
Interfaces: Any interfaces in the class
Fields: Any fields in the class
Methods: Any methods in the class
Attributes: Any attributes of the class (for example the name of the sourcefile, etc.)

class文件由10个部分组成

  1. 魔数 Magic Number
  2. 版本号 minor &major versions
  3. 常量池 Constant Pool
  4. 类访问标记 Access Flags
  5. 类索引 This Class
  6. 超类索引 Super Class
  7. 接口表索引 Interfaces
  8. 字段表 Fields
  9. 方法表 Methods
  10. 属性表 Attributes
    optimizing Java作者编了一句顺口溜
    My Very Cute Animal Turns Savage In Full Moon Areas
    我可爱的动物在满月时变得野蛮
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

魔数

0xCAFEBABE 咖啡宝贝
在这里插入图片描述
0xCAFEBABE 是JVM识别 .class文件的标志,
还是有个CAFEDEAD魔数,用于对象持久化

版本号

在这里插入图片描述
这是里
0x 00 00 00 34 十六进制 转为十进制 52 java8
在这里插入图片描述
Major Version
java1.4 48
java 5 49
java 6 50
java7 51
java8 52

常量池

版本号之后就是常量池的区域
结构类似

struct{
 u2  constant_pool_count; 常量池大小
 cp_info  constant_pool[constant_pool_count-1]
}
  1. 常量池的大小为n,真正有效的索引是 1~n-1,如果是10,就是1-9,0属于保留索引,可供特殊情况使用
  2. 常量池项cp_info集合,最多包含n-1个元素,因为long和double会占用2个索引位置
    cp_info结构
cp_info{
u1 tag;
u1 info[];
}

cp_info的第一个字节表示常量项的类型tag,接下来的几个字节表示常量的具体内容。
目前JAVA虚拟机定义了14种常量tag类型,常量名都心CONSTANT开头

类型tag值
-CONSTANT_Utf8_info-1
CONSTANT_Integer_info3
CONSTANT_Float_info4
CONSTANT_Long_info5
CONSTANT_Double_info6
CONSTANT_Class_info7
CONSTANT_String_info8
CONSTANT_Fieldref_info9
CONSTANT_Methodref_info10
CONSTANT_InterfaceMethodref_info11
CONSTANT_NameAndType_info12
CONSTANT_MethodHandle_info15
CONSTANT_MethodType_info16
CONSTANT_InvokeDynamic_info18

javap -v HelloWorld
在这里插入图片描述

Access flages

访问标志,用来标识一个类为final,abstrac,public 之类由2个字节表示
总共有16个标记位,可代使用,目前只使用了8个

标识位标记十六进制描述
0ACC_PUBLIC1是否为public
4ACC_FINAL10是否为final
5ACC_SUPER20不再使用
9ACC_INTERFACE200是类还是接口
10ACC_ABSTRACT400是否为abstract
12ACC_SYNTHETIC1000编译器自动生成,不是用户源代码编译生成
13ACC_ANNOTATION2000是否为注解
14ACC_ENUM1是否为枚举类
this_class ,super_name,interfaces

表示继承关系
this_class表示类索引
super_name表示直接父类的索引
interfaces 表示类或者接口的直接父接口

字段表
struct{
u2 fields_count;
field_info fields[fields_count];
}

fields_count 表示数量
field_info 表示集合
field_info 结构

 u2 	access_flags; 访问标志
 u2 	name_index;  字段名
 u2 	descriptor_index; 字段描述索引,指向常量池的字符串常量
 u2 	attributes_count; 属性个数
 attribute_info attributes[attributes_count]; 属性集合
 

  1. 第一部分 access_flags访问标记如ACC_PUBLIC(public)之类的
访问标记名描述
ACC_PUBLIC声明为public
ACC_PRIVATE 声明为private
ACC_PROTOECTED声明为protected
ACC_STATIC声明为static
ACC_FINAL声明为final
ACC_VOLATILE声明为volatile,解决内存可见性的问题
ACC_TRANSIENT声明为transient,被transient修饰的字段默认为被序列化
ACC_SYNITHETIC声明为这个字段由编译器生成
ACC_ENUM声明为枚举类型的变量
  1. 字段描述符
描述符类型
Bbyte
Cchar
Ddouble
Ffloat
Iint
Jlong
Sshort
Zboolean
L ClassName;引用类型, L +对象类型的全限定名+“;"
[一维数组
  1. 字段属性
    ConstantValue,Synthetic,Signature,Deprecated,RuntimeVisibleAnnotations,RuntimeInvisibleAnnotations 这6个
方法表

{
u2 method_count;
method_info methods[method_count];
}

method_count表示方法的数量
methods方法集合,共有method_count个方法

method_info 结构

 u2 	access_flags; 访问标志  private,public 之类
 u2 	name_index;  方法名
 u2 	descriptor_index; 方法描述索引,指向常量池的字符串常量
 u2 	attributes_count; 属性个数
 attribute_info attributes[attributes_count]; 属性集合
属性表
u2   attributes_count;
attributes_info attributes[attributes_count];

attributes_count属性个数,attributes属性集合
attributes_info 结构


 u2 	attribute_name_index;  方法名  指向常量池的索引,可以得到attribute 名字
 u4 	attribute_length; 属性个数
 u1    info[attribute_length]; 属性集合
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值