博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ClassFile之Attributes
阅读量:2155 次
发布时间:2019-05-01

本文共 7931 字,大约阅读时间需要 26 分钟。

Attributes

Attributes are used in the ClassFilefield_infomethod_info, and Code_attribute structures of the class file format (, , , ).

All attributes have the following general format:

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

For all attributes, the attribute_name_index must be a valid unsigned 16-bit index into the constant pool of the class. The constant_pool entry at attribute_name_index must be a CONSTANT_Utf8_info structure () representing the name of the attribute. The value of theattribute_length item indicates the length of the subsequent information in bytes. The length does not include the initial six bytes that contain the attribute_name_index and attribute_length items.

23 attributes are predefined by this specification. They are listed three times, for ease of navigation:

  •  is ordered by the attributes' section numbers in this chapter. Each attribute is accompanied by the first version of the class file format in which it was defined, and the corresponding version of the Java SE platform. (For old class file versions, the JDK release is used instead of the Java SE platform version).

  •  is orderd by the first version of the class file format in which each attribute was defined.

  •  is ordered by the location in a class file where each attribute is defined to appear.

Within the context of their use in this specification, that is, in the attributes tables of the class file structures in which they appear, the names of these predefined attributes are reserved.

Any conditions on the presence of a predefined attribute in an attributes table are specified explicitly in the section which describes the attribute. If no conditions are specified, then the attribute may appear any number of times in an attributes table.

The predefined attributes are categorized into three groups according to their purpose:

  1. Five attributes are critical to correct interpretation of the class file by the Java Virtual Machine:

    • ConstantValue

    • Code

    • StackMapTable

    • Exceptions

    • BootstrapMethods

    In a class file of version V, each of these attributes must be recognized and correctly read by an implementation of the Java Virtual Machine if the implementation recognizes class files of version V, and V is at least the version where the attribute was first defined, and the attribute appears in a location where it is defined to appear.

  2. Twelve attributes are critical to correct interpretation of the class file by the class libraries of the Java SE platform:

    • InnerClasses

    • EnclosingMethod

    • Synthetic

    • Signature

    • RuntimeVisibleAnnotations

    • RuntimeInvisibleAnnotations

    • RuntimeVisibleParameterAnnotations

    • RuntimeInvisibleParameterAnnotations

    • RuntimeVisibleTypeAnnotations

    • RuntimeInvisibleTypeAnnotations

    • AnnotationDefault

    • MethodParameters

    Each of these attributes in a class file of version V must be recognized and correctly read by an implementation of the class libraries of the Java SE platform if the implementation recognizes class files of version V, and V is at least the version where the attribute was first defined, and the attribute appears in a location where it is defined to appear.

  3. Six attributes are not critical to correct interpretation of the class file by either the Java Virtual Machine or the class libraries of the Java SE platform, but are useful for tools:

    • SourceFile

    • SourceDebugExtension

    • LineNumberTable

    • LocalVariableTable

    • LocalVariableTypeTable

    • Deprecated

    Use of these attributes by an implementation of the Java Virtual Machine or the class libraries of the Java SE platform is optional. An implementation may use the information that these attributes contain, or otherwise must silently ignore these attributes.

Table 4.7-A. Predefined class file attributes (by section)

Attribute Section class file Java SE
ConstantValue 45.3 1.0.2
Code 45.3 1.0.2
StackMapTable 50.0 6
Exceptions 45.3 1.0.2
InnerClasses 45.3 1.1
EnclosingMethod 49.0 5.0
Synthetic 45.3 1.1
Signature 49.0 5.0
SourceFile 45.3 1.0.2
SourceDebugExtension 49.0 5.0
LineNumberTable 45.3 1.0.2
LocalVariableTable 45.3 1.0.2
LocalVariableTypeTable 49.0 5.0
Deprecated 45.3 1.1
RuntimeVisibleAnnotations 49.0 5.0
RuntimeInvisibleAnnotations 49.0 5.0
RuntimeVisibleParameterAnnotations 49.0 5.0
RuntimeInvisibleParameterAnnotations 49.0 5.0
RuntimeVisibleTypeAnnotations 52.0 8
RuntimeInvisibleTypeAnnotations 52.0 8
AnnotationDefault 49.0 5.0
BootstrapMethods 51.0 7
MethodParameters 52.0 8

 

Table 4.7-B. Predefined class file attributes (by class file version)

Attribute class file Java SE Section
ConstantValue 45.3 1.0.2
Code 45.3 1.0.2
Exceptions 45.3 1.0.2
SourceFile 45.3 1.0.2
LineNumberTable 45.3 1.0.2
LocalVariableTable 45.3 1.0.2
InnerClasses 45.3 1.1
Synthetic 45.3 1.1
Deprecated 45.3 1.1
EnclosingMethod 49.0 5.0
Signature 49.0 5.0
SourceDebugExtension 49.0 5.0
LocalVariableTypeTable 49.0 5.0
RuntimeVisibleAnnotations 49.0 5.0
RuntimeInvisibleAnnotations 49.0 5.0
RuntimeVisibleParameterAnnotations 49.0 5.0
RuntimeInvisibleParameterAnnotations 49.0 5.0
AnnotationDefault 49.0 5.0
StackMapTable 50.0 6
BootstrapMethods 51.0 7
RuntimeVisibleTypeAnnotations 52.0 8
RuntimeInvisibleTypeAnnotations 52.0 8
MethodParameters 52.0 8

 

Table 4.7-C. Predefined class file attributes (by location)

Attribute Location class file
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

 

4.7.1. Defining and Naming New Attributes

Compilers are permitted to define and emit class files containing new attributes in the attributes tables of class file structures, field_info structures, method_info structures, and Code attributes (). Java Virtual Machine implementations are permitted to recognize and use new attributes found in these attributes tables. However, any attribute not defined as part of the class file specification must not affect the semantics of the class file. Java Virtual Machine implementations are required to silently ignore attributes they do not recognize.

For instance, defining a new attribute to support vendor-specific debugging is permitted. Because Java Virtual Machine implementations are required to ignore attributes they do not recognize, class files intended for that particular Java Virtual Machine implementation will be usable by other implementations even if those implementations cannot make use of the additional debugging information that the class files contain.

Java Virtual Machine implementations are specifically prohibited from throwing an exception or otherwise refusing to use class files simply because of the presence of some new attribute. Of course, tools operating on class files may not run correctly if given class files that do not contain all the attributes they require.

Two attributes that are intended to be distinct, but that happen to use the same attribute name and are of the same length, will conflict on implementations that recognize either attribute. Attributes defined other than in this specification must have names chosen according to the package naming convention described in The Java Language Specification, Java SE 8 Edition (JLS §6.1).

Future versions of this specification may define additional attributes.

 

 

 

 

转载地址:http://xkawb.baihongyu.com/

你可能感兴趣的文章
Windows程序设计:直线绘制
查看>>
linux之CentOS下文件解压方式
查看>>
Django字段的创建并连接MYSQL
查看>>
div标签布局的使用
查看>>
HTML中表格的使用
查看>>
(模板 重要)Tarjan算法解决LCA问题(PAT 1151 LCA in a Binary Tree)
查看>>
(PAT 1154) Vertex Coloring (图的广度优先遍历)
查看>>
(PAT 1115) Counting Nodes in a BST (二叉查找树-统计指定层元素个数)
查看>>
(PAT 1143) Lowest Common Ancestor (二叉查找树的LCA)
查看>>
(PAT 1061) Dating (字符串处理)
查看>>
(PAT 1118) Birds in Forest (并查集)
查看>>
数据结构 拓扑排序
查看>>
(PAT 1040) Longest Symmetric String (DP-最长回文子串)
查看>>
(PAT 1145) Hashing - Average Search Time (哈希表冲突处理)
查看>>
(1129) Recommendation System 排序
查看>>
PAT1090 Highest Price in Supply Chain 树DFS
查看>>
(PAT 1096) Consecutive Factors (质因子分解)
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>