-Java set attribute table based notes and properties Code

    Property sheet in the previous lecture has appeared several times in the Class file, field table, table method can bring their own set of attribute table, for a description of certain scenes proprietary.

    Class file strict order with other data items required different lengths and content restrictions set attribute table slightly loose some of the requirements of each attribute table has no strict order, and they do not duplicate existing attribute name, and any achieve compilers can write their own definition of the attribute information to the attribute table, ignores attribute it does not recognize the Java virtual machine is running. In order to resolve the Class files correctly, "Java Virtual Machine Specification (brother version 2)" are predefined in the nine virtual machine implementations should be able to identify the property, and in the latest "Java Virtual Machine Specification (Java SE 7)" version The predefined properties has increased 21, the specific content of the table below.  

 

 

For each property, its name needs to reference a type CONSTANT_Utf8_info constant from the constant pool is represented, the structure of two attribute values ​​is fully customizable, only the length of a property is to be described u4 occupied bit attribute value the number can be. Conform to the rules of a property sheet should meet the structural definition of the table.

 

The method of weight after the Java program passes through Javac code compiler process, eventually becomes bytecode instructions stored in the Code attribute. Code attribute set properties appear in the method table, but not all of the method table must be present this property, such as an interface or a method of abstract class does not exist in the Code attribute, if the method table has Code attribute is present, then its structure in the following table.

 

Code properties

    Java programs method body code compiler Javac after processing becomes the final bytecode instructions stored in the Code attribute. Code attribute set properties appear in the method table, but not all of the method table must be present this property, such as an interface or a method of abstract class does not exist in the Code attribute, if the method table has Code attribute is present, then its structure in the following table.

attribute_name_index是一项指向CONSTANT_Utf8_info型常量的索引,常量值固定为“Code”,它代表了该属性的属性名称,attribute_length指示了属性值的长度,由于属性名称索引与属性长度一共为6字节,所以属性值的长度固定为整个属性表长度减去6字节。

  max_stack代表了操作数栈(Operand Stacks)深度的最大值。在方法执行的任意时刻,操作数栈都不会超过这个深度。虚拟机运行的时候需要根据这个值来分配栈帧(Stack Frame)中的操作数栈深度。

    max_locals代表了局部变量表所需的存储空间。在这里,max_locals的单位是Slot,Slot是虚拟机为局部变量分配内存所使用的最小单位。对byte,char、float、int、short、boolean和returnAddress等长度不超过32位的数据类型,每个局部变量占用一个Slot,而double和long这两种64位的数据类型则需要两个Slot来存放。方法参数(包括实例方法中的隐藏参数“this”)、显式异常处理器的参数(Exception Handler Parameter,就是try-catch语句中catch快所定义的异常)、方法体中定义的局部变量都需要使用局部变量表来存放。另外,并不是在方法中用到了多少个局部变量,就把这些局部变量所占Slot之和作为max_locals的值,原因是局部变量表中的Slot可以重用,当代码执行超出一个局部变量的作用域时,这个局部变量所占的Slot可以被其他局部变量所使用,Javac编译器会根据变量的作用于来分配Slot给各个变量使用,然后计算出max_locals的大小。

    code_length和code用来存储Java源程序编译后生成的字节码指令。code_length代表字节码长度,code是用于存储字节码指令的一系列字节流。既然叫字节码指令,那么每个指令就是一个u1类型的单字节,当虚拟机读取到code中的一个字节码时,就可以对应找出这个字节码代表的是什么指令,并且可以知道这条指令后面是否需要跟随参数,以及参数应当如何解释。我们知道一个u1类型的取值范围为 0x00~0xFF,对应十进制的0~255,也就是一共可以表达256条指令,目前,Java虚拟机规范已经定义了其中约200条编码值对应的指令含义。

    关于code_lengt,有一件值得注意的事情,虽然它是一个u4类型的长度值,理论上最大值可以达到 2的32次方-1,但是虚拟机规范中明确限制了一个方法不允许超过65535条字节码指令,即使它实际只使用了u2的长度,如果超过这个限制,Javac编译器也会拒绝编译。一般来讲,编写Java代码时只要不可以去编写一个超长的方法来为难编译器,是不太可能超过这个最大值的限制。但是,某些特殊情况,例如在编译一个很复杂的jsp文件时,某些jsp编译器会吧jsp内容和页面输出的信息归并与一个方法之中,极可能因为方法生成字节码超长的原因而导致编译失败。

    Code的属性是Class文件中最重要的一个属性,如果把一个Java程序的信息分为代码(Code,方法体里面的Java代码)和元数据(Metadata,不包括类、字段、方法定义以及其他信息)两部分,那么在整个Class文件中,Code属性用于描述代码,所有的其他数据项目都用于描述元数据。

 

Guess you like

Origin blog.csdn.net/helianus/article/details/92079985