JVM的可执行文件是*.Class格式,每个*.JAVA文件经过编译器编译后都要生成ClassFile文件。ClassFile文件是由一个8位的字节流组成,每ClassFile都有着固定的格式。
ClassFile的文件结构:(表一)
字节数 |
名称 |
备注 |
4 |
Magic |
值为OXCAFEBABE 用来标记CLASS的文件 |
4 |
Version |
其中前两位是副版本号,后两位是主版本号 |
2 |
Constant_pool_count |
Constat_pool总数,根据自己数我们可知一个JAVA类文件中最多能有65535个Constant_pool |
不定长 |
Constant_pool[] |
用来描述Constant_pool中各种字符串常数,类名和其他变长结构的表。Constant_Pool表的第一项是JVM内部使用的不显示在ClassFile中,因此Constant_pool中显示的Item应该从1~Constant_pool_count-1。注:这是ClassFile文件中最关键的部分之一。(见表三) |
2 |
access_flag |
类和接口中使用的修饰符掩码。(见表二) 其中:CLASSFILE可以使用的修饰符可以相加。如:0X0200表示Public,0x0001表示Class,则:0x0021表示Public Class。 |
2 |
this_class |
表示ClassFile中的定义的类或者接口;this_class的项必须是Constant_pool中的有效索引 |
2 |
super_class |
对于一个类Super_class项的值必须为0,或者Constant_pool表的一个有效索引。如果Super_class项的值不为零,则该索引处的constant_pool表项必须是classfile定义类的超类CONSTANT_class_info结构。此类不能位Final。
若Super_class的值为零,则该classfile必须表示java.lang.Object. 它是唯一的没有超类的类或者接口。
对于接口,Super_class的值必须总是constat_pool表的一个有效索引,该索引处的constant_pool表项必须是表示类java.lang.Object的一个CONSTANT_Class_info结构。 |
2 |
interfaces_count |
类或接口的直接超类接口的个数 |
2 |
interface[] |
Constant_pool表的有效索引。其中,interace[i]是CONSTANT_Class_info的结构。 |
2 |
fields_count |
fields_info结构的个数。 |
|
fields[] |
见field_info结构表 |
2 |
methods_count |
method_info结构的个数 |
|
methods[] |
结构见Method_info |
2 |
attributes_count |
attributes表中的个数 |
|
attributes[] |
attributes[]的每个值是一个变长属性结构。一个classfile结构可以有与他相关联的任意个数的属性。 |
