首页 > 技术文章 > Java虚拟机规范-Class文件格式阅读摘要

qiyu 2013-10-01 19:43 原文

ClassFile结构:每一个Class文件对应于一个如下所示的ClassFile结构体

 1 ClassFile {
 2     u4 magic;//魔数,值固定为0xCAFEBABE
 3     u2 minor_version;//副版本号
 4     u2 major_version;//主版本号
 5     u2 constant_pool_count;//常量池计数器,值等于constant_pool表中的成员数加1
 6     cp_info constant_pool[constant_pool_count-1];//常量池,包含Class文件结构及其子结构中引用的所有字符串常量、类或接口名、字段名和其它常量
 7     u2 access_flags;//访问标志,参见表1-1
 8     u2 this_class;//this_class的值必须是对constant_pool表中项目的一个有效索引值
 9     u2 super_class;//super_class的值必须为0或者是对constant_pool表中项目的一个有效索引值
10     u2 interfaces_count;//接口计数器
11     u2 interfaces[interfaces_count];//接口表,interfaces[]数组中的每个成员的值必须是一个对constant_pool表中项目的一个有效索引值
12     u2 fields_count;//字段计数器
13     field_info fields[fields_count];//字段表
14     u2 methods_count;//方法计数器
15     method_info methods[methods_count];//方法表
16     u2 attributes_count;//属性计数器
17     attribute_info attributes[attributes_count];//属性表
18 }

其中,u1,u2和u4,分别代表了1、2和4个字节的无符号数。在Java SDK中这些类型的数据可以通过实现接口java.io.DataInput 中的readUnsignedByte、readUnsignedShort和readInt方法进行读取。

 表1-1

访问和修饰符标记
标记名 含义
ACC_PUBLIC 0x0001 可以被包的类外访问。
ACC_FINAL 0x0010 不允许有子类。
ACC_SUPER 0x0020 当用到invokespecial指令时,需要特殊处理的父类方法。
ACC_INTERFACE 0x0200 标识定义的是接口而不是类。
ACC_ABSTRACT 0x0400 不能被实例化。
ACC_ANNOTATION 0x2000 标识注解类型
ACC_ENUM 0x4000 标识枚举类型
ACC_SYNTHETIC 0x1000 标识并非Java源码生成的代码。

推荐阅读