`

JVM学习笔记-属性(Attributes)

 
阅读更多

 

Attributes

As mentioned above, attributes appear in several places inside a Java class file. They can appear in the ClassFile, field_info, method_info, and Code_attribute tables. The Code_attribute table, an attribute itself, is described later in this section.

如前所述,属性在Java class文件中多处出现。它们可以出现在ClassFile,field_info、method_info和Code_attribute表中。Code_attribute表本身即为一个属性。

 

Every attribute follows the same general format of the attribute_info table, shown in Table 6-24. The first two bytes of an attribute, the attribute_name_index, form an index into the constant pool of a CONSTANT_Utf8_info table that contains the string name of the attribute. Each attribute_info, therefore, identifies its "type" by the first item in its table much like cp_info tables identify their type by the initial tag byte. The difference is that whereas the type of a cp_info table is indicated by an unsigned byte value, such as 3 (CONSTANT_Integer_info), the type of an attribute_info table is indicated by a string.

 

Following the attribute_name_index is a four-byte attribute_length item, which gives the length of the entire attribute_info table minus the initial six bytes. This length is necessary because anyone, following certain rules (outlined below), is allowed to add attributes to a Java class file. Java Virtual Machine implementations are allowed to recognize new attributes. Implementations must ignore any attributes they donít recognize. The attribute_length allows virtual machines to skip unrecognized attributes as they parse the class file.

 

Anyone who wishes to add a new attribute to a Java class file must follow these two rules:

  1. Any attribute that is not predefined by the specification must not affect the semantics of class or interface types. New attributes can only add more information to the class file, such as information used during debugging.

     

  2. The attribute must be named using the reverse Internet domain name scheme that is defined for package naming in the Java Language Specification. For example, if your Internet domain name were artima.com and you wished to create a new attribute named CompilerVersion, you would name the attribute: COM.artima.CompilerVersion.
如果需要向Java class文件中加入新的属性,除Sun公司以外的任何人都必须遵循下列两个步骤:
1.      任何不是由规范进行与定义的属性都不能影响类或者接口类型的语义。新的属性只能向class文件添加新的信息,如在调试过程中用到的信息等。
2.      属性必须使用与Internet域名方案颠倒的命名方式,Internet域名方案是针对Java语言规范中包的命名所定义的。例如,如果拥有Internet域名artima.com,而需要创建的新属性为CompilerVersion,那么属性则应该命名为com.artima.CompilerVersion。


Table 6-24. Format of an attribute_info table

Type Name Count
u2 attribute_name_index 1
u4 attribute_length 1
u1 info attribute_length

attribute_name_index

The attribute_name_index gives the index in the constant pool of a CONSTANT_Utf8_info entry that contains the name of the attribute.

attribute_length

The attribute_length item indicates the length in bytes of the attribute data excluding the initial six bytes that contain the attribute_name_index and attribute_length.

info

The info item contains the attribute data.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics