java虚拟机规范(se8)——class文件格式(四)

4.7 属性

  属性用于class文件格式中的ClassFile,field_info,method_info和Code_attribute结构。

  所有的属性都是下面的格式:

attribute_info {
    u2 attribute_name_index;
    u4 attribute_length;
    u1 info[attribute_length];
}

  对于所有的属性,attribute_name_index是类文件常量池中有效的16位索引。在这个索引位置的常量池条目必须是CONSTANT_Utf8_info结构,表示这个属性的名称。attribute_length表示接下来内容长度有多少字节。长度不包含开始的6个字节(即不包含attribute_name_index和attribute_lenth)。

  23个预定义的属性在此规范中定义了,为了便于导航,使用三个不同顺序的表格来列出来。

  表4.7-A按本章属性节号排序。每个属性都带有定义它的类文件格式的第一个版本,以及Java SE平台的相应版本。(对于旧的类文件版本,使用JDK发行版而不是Java SE平台版本)。

  表4.7-B是由在其中定义的每个属性的类文件格式的第一版本排序。

  表4.7-C是根据类文件中定义每个属性要出现的位置来排序的。

  在本规范中使用它们的上下文中,也就是说,在它们出现的类文件结构的属性表中,这些预定义属性的名称是保留的。

  属性表中存在预定义属性的任何条件都在描述属性的部分中显式指定。如果没有指定条件,则属性可以在属性表中出现任意次数。

  预定义的属性根据其用途分为三组:

  1、五个属性对于Java虚拟机正确解释类文件至关重要:

  • ConstantValue
  • Code
  • StackMapTable
  • Exceptions
  • BootstrapMethods

    在版本为V的类文件中,如果java虚拟机的实现能够识别V版本的类文件,那么这些属性中的每一个都必须被Java虚拟机的实现正确识别和读取,并且版本V至少是属性最初被定义的版本,并且属性出现在它被定义中出现的位置。

  2、12个属性对于Java SE平台类库对类文件的正确解释至关重要:

  • InnerClasses
  • EnclosingMethod
  • Synthetic
  • Signature
  • RuntimeVisibleAnnotations
  • RuntimeInvisibleAnnotations
  • RuntimeVisibleParameterAnnotations
  • RuntimeInvisibleParameterAnnotations
  • RuntimeVisibleTypeAnnotations
  • RuntimeInvisibleTypeAnnotations
  • AnnotationDefault
  • MethodParameters

    如果ava SE平台类库的实现需要识别版本V的类文件,那么版本V的类文件中的这些属性中的每一个都必须被类库的实现识别和正确读取,并且版本V至少是属性最初被定义的版本,并且属性出现在它被定义中出现的位置。

  3、对于Java虚拟机或Java SE平台的类库来说,六个属性对正确解释类文件并不重要,但对工具很有用:  

  • SourceFile
  • SourceDebugExtension
  • LineNumberTable
  • LocalVariableTable
  • LocalVariableTypeTable
  • Deprecated

    对于java虚拟机或者Java SE平台的类库的实现来说,使用这些属性是可选的。实现可能使用这些属性包含的信息,也可以直接忽略这些属性。

  

Table 4.7-A. 预定义的类文件属性(根据章节排序)

属性章节class 文件Java SE
ConstantValue §4.7.2 45.3 1.0.2
Code §4.7.3 45.3 1.0.2
StackMapTable §4.7.4 50.0 6
Exceptions §4.7.5 45.3 1.0.2
InnerClasses §4.7.6 45.3 1.1
EnclosingMethod §4.7.7 49.0 5.0
Synthetic §4.7.8 45.3 1.1
Signature §4.7.9 49.0 5.0
SourceFile §4.7.10 45.3 1.0.2
SourceDebugExtension §4.7.11 49.0 5.0
LineNumberTable §4.7.12 45.3 1.0.2
LocalVariableTable §4.7.13 45.3 1.0.2
LocalVariableTypeTable §4.7.14 49.0 5.0
Deprecated §4.7.15 45.3 1.1
RuntimeVisibleAnnotations §4.7.16 49.0 5.0
RuntimeInvisibleAnnotations §4.7.17 49.0 5.0
RuntimeVisibleParameterAnnotations §4.7.18 49.0 5.0
RuntimeInvisibleParameterAnnotations §4.7.19 49.0 5.0
RuntimeVisibleTypeAnnotations §4.7.20 52.0 8
RuntimeInvisibleTypeAnnotations §4.7.21 52.0 8
AnnotationDefault §4.7.22 49.0 5.0
BootstrapMethods §4.7.23 51.0 7
MethodParameters §4.7.24 52.0 8

 

Table 4.7-B. 预定的类文件属性(根据类文件版本排序)

属性class 文件Java SE章节
ConstantValue 45.3 1.0.2 §4.7.2
Code 45.3 1.0.2 §4.7.3
Exceptions 45.3 1.0.2 §4.7.5
SourceFile 45.3 1.0.2 §4.7.10
LineNumberTable 45.3 1.0.2 §4.7.12
LocalVariableTable 45.3 1.0.2 §4.7.13
InnerClasses 45.3 1.1 §4.7.6
Synthetic 45.3 1.1 §4.7.8
Deprecated 45.3 1.1 §4.7.15
EnclosingMethod 49.0 5.0 §4.7.7
Signature 49.0 5.0 §4.7.9
SourceDebugExtension 49.0 5.0 §4.7.11
LocalVariableTypeTable 49.0 5.0 §4.7.14
RuntimeVisibleAnnotations 49.0 5.0 §4.7.16
RuntimeInvisibleAnnotations 49.0 5.0 §4.7.17
RuntimeVisibleParameterAnnotations 49.0 5.0 §4.7.18
RuntimeInvisibleParameterAnnotations 49.0 5.0 §4.7.19
AnnotationDefault 49.0 5.0 §4.7.22
StackMapTable 50.0 6 §4.7.4
BootstrapMethods 51.0 7 §4.7.23
RuntimeVisibleTypeAnnotations 52.0 8 §4.7.20
RuntimeInvisibleTypeAnnotations 52.0 8 §4.7.21
MethodParameters 52.0 8 §4.7.24

 

Table 4.7-C. 预定义的类文件属性 (根据位置排序)

属性位置class 文件
SourceFile ClassFile 45.3
InnerClasses ClassFile 45.3
EnclosingMethod ClassFile 49.0
SourceDebugExtension ClassFile 49.0
BootstrapMethods ClassFile 51.0
ConstantValue field_info 45.3
Code method_info 45.3
Exceptions method_info 45.3
RuntimeVisibleParameterAnnotationsRuntimeInvisibleParameterAnnotations method_info 49.0
AnnotationDefault method_info 49.0
MethodParameters method_info 52.0
Synthetic ClassFilefield_infomethod_info 45.3
Deprecated ClassFilefield_infomethod_info 45.3
Signature ClassFilefield_infomethod_info 49.0
RuntimeVisibleAnnotationsRuntimeInvisibleAnnotations ClassFilefield_infomethod_info 49.0
LineNumberTable Code 45.3
LocalVariableTable Code 45.3
LocalVariableTypeTable Code 49.0
StackMapTable Code 50.0
RuntimeVisibleTypeAnnotationsRuntimeInvisibleTypeAnnotations ClassFilefield_infomethod_infoCode 52.0
posted @ 2019-09-26 17:17  lwli  阅读(357)  评论(0编辑  收藏  举报