class file Format (2)-Deep understanding of Classfile__java

Source: Internet
Author: User
Tags array length deprecated field table
1.access_flagsFollowing the Chang, the following two bytes represent access control permissions for the current class or interface, as follows:
1.1, Acc_syntheticIndicates that the current class is generated by the compiler, rather than by the compiler compiled by the user for the program source code. 1.2, Acc_enumThe current class, or the parent class of the current class, is declared as an enumeration type. 1.3, Acc_interfaceRepresents an interface type that, if not set, indicates that the class file is a class type. If the ACC_INTERFACE flag is set, the ACC_ABSTRACT flag should also be set, and the Acc_final, Acc_supper, and acc_enum flags should not be set. 1.4, Acc_annotationFlags an annotation type. If the acc_annatation is set, the ACC_INTERFACE flag must also be set. 1.5, Acc_superFlag invokespecial instructions specifically use which semantics (invokespecial directives have two semantics), the current JVM should set this tag. In JDK1.0.2, there is no acc_super flag in the access_flags of the class file.
2.this_classThis_class is a U2-type integer that points to a valid index of a constant pool that holds constant_class_info structure items, describing the current class or interface information, as follows:
3.super_classFor a class, the value of the Super_class is either 0, or an integer value of the U2 type, which is a valid index of the constant pool, where the CONSTANT_CLASS_INFO structure item is stored. The structure item is used to describe the direct parent class information of the class defined in the current class file. Also, there is no acc_final flag in the Access_flags in the immediate parent class of the current class and in the class file of its indirect parent class.
If the value of Super_class is 0, the current class file can only be the class file of the Java.lang.Object class, and the Java.lang.Object class is the only class that does not have a parent class.
For a class file for an interface, the Super_class must be a valid constant pool index, which holds the CONSTANT_CLASS_INFO structure item that describes the Java.lang.Object class information.
4.interface_countThe value is an unsigned integer of type U2 that represents the number of direct parent interfaces of the current class or interface.
5.interfaces[]Interface array or interface table, each element in the array is an unsigned integer of type U2, representing a valid index of the constant pool, where the CONSTANT_CLASS_INFO structure item is stored, describing the immediate parent interface information of the current class or interface. The order in which interfaces appear in the interface[] array is consistent with the order of the interfaces given in the source code, i.e., Interfaces[0] corresponds to the leftmost interface in the source code. Specifically as follows:
6.fields_countField counter, an integer of type U2, Fields_count represents the number of members of the current Class file field table fields[] array. 7.fields[]A field table that describes all the fields (class variables, instance variables) that are declared in the current class or interface, excluding parts that are inherited from the parent interface or the parent class, and each member of the table is a FIELD_INFO structure item, as follows:
Where the FIELD_INFO structure item is used to describe the fields in the class file, the concrete structure of the FIELD_INFO structure item is as follows:

7.1, Access_flagsA mask that flags the access control permissions and properties of this field, and its value range and meaning are shown in the following table:
If Acc_synthetic is marked in the field's Access_flags mask, the field is automatically generated by the compiler and does not appear in the source code.           If a acc_enum is marked in the Access_flags mask, the value of the field is a member of an enumerated type. A field can have multiple properties in the table at the same time, but some properties are mutually exclusive, such as a maximum of one of the Acc_private, acc_protected, acc_public attributes, and cannot have acc_final, ACC_     Volatile property. All fields in interface must have Acc_public, Acc_static, acc_final properties, and possibly Acc_synthetic properties, and other property values in the table cannot be owned. 7.2, Name_indexPoints to a valid index in a constant pool in which the Constant_utf8_info constant entry is stored, representing the unqualified name of the valid field. 7.3, Descriptor_indexPoints to a valid index in a constant pool at which the Constant_utf8_info constant entry is stored, representing the descriptor for the valid field. 7.4, Attributes_countRepresents the number of attached properties for the current field. 7.5, attributes[]Represents the property sheet for the current field, and each element in the table is a ATTRIBUTE_INFO structure item.         The ATTRIBUTE_INFO structure item is used to describe a specific attribute value, attribute_info the concrete content of the structure item, this article later will draw the Specialized Section to explain, does not do too much description here. In the official JVMS7 (Java Virtual Machine specification), the types of properties that can appear in the FIELD_INFO structure item's property sheet (attributes[]) are: Constantvalue, synthetic, Signature, Deprecated, Runtimevisibleannotations, runtimeinvisibleannotations, as follows:

JVMS7 stipulates that the JVM's implementation must be able to recognize and correctly read the Constantvalue attribute in the FIELD_INFO structure item's property sheet (attributes[]). If the JVM's implementation supports 49.0 or later class files, the JVM must be able to recognize and read the FIELD_INFO structure item's property sheet (attributes[) correctly from the class file with version number 49.0 or higher. The following property items are in: Signature, Runtimevisibleannotations, Runtimeinvisibleannotations. 8.methods_countMethod counter, an integer of type U2 that represents the number of members of the party publication Methods[] array of the current class file.
9.methods[]Method table array that describes all the methods in the class, including instance initialization methods and class initialization methods, the methods[] method table only describes all the methods of the current class, but does not describe methods inherited from the parent or parent interface, and each member in the Methods[method table is Method_ The info structure item, which is used to fully describe a method in the current class or interface Method_info. Specifically as follows:
Method_info describes all the methods declared in the current class or interface, including instance methods, class methods, instance initialization methods, class, or interface initialization methods, and the concrete structure of the METHOD_INFO structure item is as follows:
9.1, Access_flagsFlags the mask for the access Control permission and property of the method currently described by Method_info, whose value range and significance are shown in the following table:

The Acc_varargs flag represents a method at the source code level with variable-length arguments.
The Acc_bridge flag indicates that the method is a bridge method generated by the Java compiler.
The Acc_synthetic flag indicates that the method is a compiler-generated method that does not appear in the source code of the program, and there are, of course, some method exceptions that can participate in JVMS7 's methods chapters and synthetic attribute chapters.
Class can have multiple attribute values in the table at the same time, but some properties are mutually exclusive, for example, you can only have one property in Acc_private, acc_protected, Acc_public, if access_flags set ACC_         Abstract property, the method is not able to set acc_final, Acc_native, Acc_private, Acc_static, Acc_strict, acc_synchronized properties. All methods of interface must set the Acc_abstract and Acc_public properties, and these interface methods may also have Acc_varargs, Acc_bridge, acc_synthetic properties, and in addition, You cannot have other properties in the table above.
For instance initialization methods, you can have one of acc_private, acc_protected, Acc_public, and you can have Acc_strict, Acc_varargs, acc_synthetic properties, and in addition,         The instance initialization method cannot have other properties in the table above. For initialization methods for classes or interfaces, they are invoked implicitly by the Java Virtual Machine, in which the remaining properties are ignored except for acc_strict. 9.2, Name_indexPoints to a valid index in a constant pool in which the Constant_utf8_info constant entry is stored, either an initialization method (<init>, <clinit>) or a valid, unqualified name for the method. 9.3, Descriptor_indexPoints to a valid index in a constant pool at which the Constant_utf8_info constant entry is stored, representing the descriptor for the valid method.
9.4, Attributes_countRepresents the number of attached properties for the current method.
9.5, attributes[]Represents the property sheet for the current method, as is the property sheet for the FIELD_INFO structure item, and each element in the Method_info property sheet is a ATTRIBUTE_INFO structure item, which is not overly described here.
In the official JVMS7 (Java Virtual Machine specification), the types of properties that can appear in the METHOD_INFO structure item's property sheet (attributes[]) are: Code, exceptions, synthetic, Signature, Deprecated, Runtimevisibleannotations, runtimeinvisibleannotations, Runtimevisibleparameterannotations, Runtimeinvisibleparameterannotations, Annotationdefault, as follows:
JVMS7 stipulates that the JVM's implementation must be able to recognize and correctly read the code, exceptions properties in the METHOD_INFO structure item's property sheet (attributes[]). If the JVM's implementation supports 49.0 or later class files, the JVM must be able to recognize and read the METHOD_INFO structure item's property sheet (attributes[) correctly from the class file with version number 49.0 or higher. The following property items are in: Signature, Runtimevisibleannotations, Runtimeinvisibleannotations, Runtimevisibleparameterannotations, Runtimeinvisibleparameterannotations, Annotationdefault. 10.attributes_countThe property counter, an integer of type U2, representing the number of members of the property array attributes[] of the class described by the classes file.
11.attributes[]The current class file property sheet, where each element in the property sheet is a ATTRIBUTE_INFO structure item. 11.1, Attribute_infoAttribute_info structure items are referenced in the property sheet of the Classfile, the property sheet of the FIELD_INFO structure item, the property sheet of the METHOD_INFO structure item, the property sheet of the Code property structure Item Code_attributes, Each element of these property sheets can be described by attribute_info structure items, which specify the general layout of all properties, as follows:
11.1.1, Attribute_name_indexAn integer of type U2, pointing to a valid index of the constant pool, which is a Constant_utf8_info constant entry that represents the name of the property described by the current attribute_info.
11.1.2, Attribute_lengthA U4 type of integer that indicates the byte length of the property's content, or the number of bytes that the info[] array occupies.
11.1.3, Info[attribute_lenght]An array of bytes containing the contents of the property, with attribute_lenght specified in the array length.
11.2, attributes[]The properties can be predefined by the JVM or can be customized by the program in the source code, and some pre-defined property entries are indicated in JVMS7, as shown in the following example:
The structure and meaning of these predefined property items are followed by a special section that is explained in some detail in this article.
JVMS7 also gives the Javase version number and the class file version number for the first occurrence of these predefined properties, as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.