JAVA virtual machine configuration file [class]

Code compilation of results from the local machine it into bytecode, is a small step in the development of storage format, but it is a big step in the development of programming languages.

1. The independence of the foundation:

Various platforms are unified virtual machine bytecode stored as a program format, which is the cornerstone of platform-independent. At the same time, language independence also been valued by developers, JAVA virtual machine can also run other programs.

2. class file structure:

Class file is the basic unit of 8-bit bytes of binary stream, with no separators , it is a necessary element for the entire run of the Class file.

Class file structure stored in a dummy data, comprising: unsigned and tables . Unsigned belong to the basic data types, to u1, u2, u4, u8 equals 1 byte, 2-byte unsigned number, 4 bytes and 8 bytes. It can be used to describe the figures, reference index, in accordance with the number of values or utf-8 encoded string value. A plurality of data table is an unsigned number, or other configuration consistent with the structure of the table.

2.1 Class file version with the magic number

The first four bytes called magic number, only role is to confirm whether the file is a virtual machine that can be accepted by Class file. 0xCAFEBABE is the magic number JAVA file. 5,6 byte minor version number and major version number is 7-8.

2.2 constant pool

Then after the version number is constant pool. Placing the inlet of a constant pool u2 data type, representative of the count value of the constant pool. The count value from the beginning, if it is 22, it means that a total of 21 constants, the index range of 1 to 21.

There are two types of constant pool constants: literal and symbolic references . Literal mouth is often said constants, including strings, numbers, and so on. Symbol references includes classes and interfaces fully qualified names, field names and descriptors, the method name and descriptor categories.

Constant pool of project types are as follows:

Each type of item have different data structures, such as UTF8 encoding format string constant on the following:

2.3 Access logo

After the constant pool followed by two bytes access flag. Common signs are as follows:

2.4 category index, and the index of the parent class interface index set

前两个都是u2类型的数据,类索引用于确定类的全限定名,父类索引用于确定父类的全限定名,接口索引集合用来描述这个类实现了哪些接口。接口索引第一项,接口计数器表示索引表的容量。

2.5 字段表集合

包括类级变量,实例级变量,不包括方法声明的局部变量。字段表的结构如下:

全限定名包括路径,之间用/表示,简单名字只有一个名字。描述符描述字段的数据类型,方法的参数列表和返回值。

2.6 方法表集合

结构如下:

2.7 属性表集合

2.7.1 code属性

其中code用来存储JAVA源程序编译后生成的字节码命令。每个指令由一个u1的字节码表示,即字节码最多有256条命令 。

code属性是字节码中最重要的属性,JAVA程序分为代码和元数据,Code属性用于描述代码,所有其他数据项目用于描述元数据。

2.7.2 Exceptions属性

这里的Excepetions属性是与方法平级的一项属性,与Code属性中的异常表不同。

2.7.3 LineNumber属性(非必须)

用与描述JAVA源码行号与字节码行号之间的对应关系。

2.7.4 LocalVariableTable属性(非必须)

用于描述栈帧中局部变量表中的变量与JAVA源码中定义的变量之间的关系。

2.7.5 SourceFile属性(非必须)

用于记录生成这个Class文件的源码文件名称。对于一些类名与文件名不一致的情况,不用这个属性,当抛出异常的时候,无法定位到出错代码的文件名。

2.7.6 ConstantValue属性

用于通知虚拟机自动为静态变量赋值。

2.7.7 InnerClasses属性

用于记录内部类和宿主类之间的关联。

2.7.8 Deprecated和Synthetic属性

Deprecated属性用于表示某个类,字段或者方法已经被程序定为不再使用。

Synthetic属性用于代表此方法或者字段并不是由JAVA源码产生的,而是编译器自己产生的。

2.7.9 StackMapTable属性

在虚拟机类加载字节码验证阶段被新类型检查验证器使用。

2.7.10 Signature属性

任何类、接口、初始化方法或成员的泛型签名如果包含了类型变量或参数化类型,则Signature属性会为他记录泛型签名信息。

2.7.11 BootstrapMethods属性

用于保存incokedynamic指令引用的引导方法限定符。

发布了73 篇原创文章 · 获赞 11 · 访问量 9480

Guess you like

Origin blog.csdn.net/fanyuwgy/article/details/103825313
Recommended