Java Class File 解析
解析Java Class File
1.环境
操作系统: mac
Java: 1.8
参考文档: https://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf
2.准备工作
将源文件编译成class文件,使用特殊工具查看16进制的内容,本次使用工具为sublime
自行官网下载: http://www.sublimetext.com/
2.1源文件
2.2 编译
编译成对应的HelloClass.class文件,用sublime打开
下面就将对这个class文件进行解析,过程是枯燥无聊的
3.Class Format
引用 -> java虚拟机规范 4.1 The ClassFile Structure
A class file consists of a single ClassFile structure:
一个class文件的组成结构
3.1 magic
it has the value 0xCAFEBABE,默认前4个字节就是java的魔术 cafe babe
3.2 minor_version, major_version
对应的就是java的版本,一个占2个字节,一共4个字节,java8对应的就是52,16进制就是34(没有去官网文档证实,数据来源于网络)
3.3 constant_pool_count
常量池的大小 -> 一共两个字节,我们编译的值是0x0014,也就代表常量池大小为20
3.4 constant_pool[constant_pool_count-1];
这里就是常量池,真正的大小为常量池大小减1
All constant_pool table entries have the following general format:
所有常量池对象有以下通用格式
3.4.1 Constant pool tags table
Constant Type | Value |
---|---|
CONSTANT_Class | 7 |
CONSTANT_Fieldref | 9 |
CONSTANT_Methodref | 10 |
CONSTANT_InterfaceMethodref | 11 |
CONSTANT_String | 8 |
CONSTANT_Integer | 3 |
CONSTANT_Float | 4 |
CONSTANT_Long | 5 |
CONSTANT_Double | 6 |
CONSTANT_NameAndType | 12 |
CONSTANT_Utf8 | 1 |
CONSTANT_MethodHandle | 15 |
CONSTANT_MethodType | 16 |
CONSTANT_InvokeDynamic | 18 |
3.4.2 The CONSTANT_Class_info Structure
The CONSTANT_Class_info structure is used to represent a class or an interface:
CONSTANT_Class_info 用于表示一个类或者一个接口
name_index:
The value of the name_index item must be a valid index into the constant_pool table. The constant_pool entry at that
index must be a CONSTANT_Utf8_info structure (§4.4.7) representing a valid binary class or interface name encoded in
internal form
3.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures
class_index The value of the class_index item must be a valid index into the constant_pool table. The constant_pool
entry at that index must be a CONSTANT_Class_info structure (§4.4.1) representing a class or interface type that has the
field or method as a member. The class_index item of a CONSTANT_Methodref_info structure must be a class type, not an
interface type. The class_index item of a CONSTANT_InterfaceMethodref_info structure must be an interface type. The
class_index item of a CONSTANT_Fieldref_info structure may be either a class type or an interface type.
name_and_type_index The value of the name_and_type_index item must be a valid index into the constant_pool table. The
constant_pool entry at that index must be a CONSTANT_NameAndType_info structure (§4.4.6). This constant_pool entry
indicates the name and descriptor of the field or method. In a CONSTANT_Fieldref_info, the indicated descriptor must be
a field descriptor (§4.3.2). Otherwise, the indicated descriptor must be a method descriptor (§4.3.3). If the name of
the method of a CONSTANT_Methodref_info structure begins with a '<' ('\u003c'), then the name must be the special
name
3.4.3 The CONSTANT_String_info Structure
The CONSTANT_String_info structure is used to represent constant objects of the type String:
string_index The value of the string_index item must be a valid index into the constant_pool table. The constant_pool
entry at that index must be a CONSTANT_Utf8_info structure (§4.4.7) representing the sequence of Unicode code points to
which the String object is to be initialized.
3.4.5 The CONSTANT_Integer_info and CONSTANT_Float_info Structures
3.4.6 The CONSTANT_Long_info and CONSTANT_Double_info Structures
3.4.7 The CONSTANT_NameAndType_info Structure
name_index The value of the name_index item must be a valid index into the constant_pool table. The constant_pool entry
at that index must be a CONSTANT_Utf8_info structure (§4.4.7) representing either the special method name
or a valid unqualified name denoting a field or method
(§4.2.2).
descriptor_index The value of the descriptor_index item must be a valid index into the constant_pool table. The
constant_pool entry at that index must be a CONSTANT_Utf8_info structure (§4.4.7) representing a valid field descriptor
or method descriptor (§4.3.2, §4.3.3).
3.4.8 The CONSTANT_Utf8_info Structure
3.4.9 The CONSTANT_MethodHandle_info Structure
3.4.10 The CONSTANT_MethodType_info Structure
3.4.11 The CONSTANT_InvokeDynamic_info Structure
3.5 u2 access_flags
两个字节的 访问修饰符
Flag Name | Value | Interpretation |
---|---|---|
ACC_PUBLIC | 0x0001 | Declared public; may be accessed from outside its package. |
ACC_FINAL | 0x0010 | Declared final; no subclasses allowed. |
ACC_SUPER | 0x0020 | Treat superclass methods specially when invoked by the invokespecial instruction. |
ACC_INTERFACE | 0x0200 | Is an interface, not a class. |
ACC_ABSTRACT | 0x0400 | Declared abstract; must not be instantiated. |
ACC_SYNTHETIC | 0x1000 | Declared synthetic; not present in the source code. |
ACC_ANNOTATION | 0x2000 | Declared as an annotation type. |
ACC_ENUM | 0x4000 | Declared as an enum type. |
3.6 this_class
this_class The value of the this_class item must be a valid index into the constant_pool table. The constant_pool entry
at that index must be a CONSTANT_Class_info structure (§4.4.1) representing the class or interface defined by this class
file
3.7 super_class
For a class, the value of the super_class item either must be zero or must be a valid index into the constant_pool
table. If the value of the super_class item is nonzero, the constant_pool entry at that index must be a
CONSTANT_Class_info structure representing the direct superclass of the class defined by this class file. Neither the
direct superclass nor any of its superclasses may have the ACC_FINAL flag set in the access_flags item of its ClassFile
structure. If the value of the super_class item is zero, then this class file must represent the class Object, the only
class or interface without a direct superclass. For an interface, the value of the super_class item must always be a
valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure
representing the class Object.
3.8 interfaces_count
The value of the interfaces_count item gives the number of direct superinterfaces of this class or interface type.
3.9 interfaces[]
Each value in the interfaces array must be a valid index into the constant_pool table. The constant_pool entry at each
value of interfaces[i], where 0 ≤ i < interfaces_count, must be a CONSTANT_Class_info structure representing an
interface that is a direct superinterface of this class or interface type, in the left-to-right order given in the
source for the type
3.10 fields_count
The value of the fields_count item gives the number of field_info structures in the fields table. The field_info
structures represent all fields, both class variables and instance variables, declared by this class or interface type.
3.11 fields[]
The structure has the following format:
Field access and property flags
Flag Name | Value | Interpretation |
---|---|---|
ACC_PUBLIC | 0x0001 | Declared public; may be accessed from outside its package. |
ACC_PRIVATE | 0x0002 | Declared private; usable only within the defining class. |
ACC_PROTECTED | 0x0004 | Declared protected; may be accessed within subclasses. |
ACC_STATIC | 0x0008 | Declared static. |
ACC_FINAL | 0x0010 | Declared final; never directly assigned to after object construction (JLS §17.5). |
ACC_VOLATILE | 0x0040 | Declared volatile; cannot be cached. |
ACC_TRANSIENT | 0x0080 | Declared transient; not written or read by a persistent object manager. |
ACC_SYNTHETIC | 0x1000 | Declared synthetic; not present in the source code. |
ACC_ENUM | 0x4000 | Declared as an element of an enum. |
3.12 methods_count[]
The value of the methods_count item gives the number of method_info structures in the methods table.
3.13 methods[]
Method access and property flags
Flag Name | Value | Interpretation |
---|---|---|
ACC_PUBLIC | 0x0001 | Declared public; may be accessed from outside its package. |
ACC_PRIVATE | 0x0002 | Declared private; accessible only within the defining class. |
ACC_PROTECTED | 0x0004 | Declared protected; may be accessed within subclasses. |
ACC_STATIC | 0x0008 | Declared static. |
ACC_FINAL | 0x0010 | Declared final; must not be overridden (§5.4.5). |
ACC_SYNCHRONIZED | 0x0020 | Declared synchronized; invocation is wrapped by a monitor use. |
ACC_BRIDGE | 0x0040 | A bridge method, generated by the compiler. |
ACC_VARARGS | 0x0080 | Declared with variable number of arguments. |
ACC_NATIVE | 0x0100 | Declared native; implemented in a language other than Java. |
ACC_ABSTRACT | 0x0400 | Declared abstract; no implementation is provided. |
ACC_STRICT | 0x0800 | Declared strictfp; floating-point mode is FP strict. |
ACC_SYNTHETIC | 0x1000 | Declared synthetic; not present in the source code. |
3.14 attributes_count
The value of the attributes_count item gives the number of attributes in the
attributes table of this class.
3.15 attributes[]
这里东西太多了 去文档自己查吧 都是对照文档进行解析的
4 解析文件
__EOF__

本文链接:https://www.cnblogs.com/immortal-mode/p/16715053.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!