Java bytecode file detailed

Source: Internet
Author: User
Tags array length field table instance method

Each class byte code file is unique to a class or interface, class file in the basic information of the classes or interfaces, but not the other way, not every class or interface has a unique corresponding bytecode file, the first class or interface bytecode can not be stored as a file, You can generate a byte code directly from memory without generating a. class file, and the principle of dynamic proxy is to generate a byte stream in direct memory, class loading operations based on the load byte stream, class instantiation, and proxy object generation.

Bytecode files record information: Magic, class file primary and secondary version, constant pool number and constant pool table, class or interface access flag, class index, superclass index, interface number and interface table, number of fields and field table, method number and Method table, attribute property and attribute table

Magic: Four bytes, used to define whether this bytecode file conforms to the virtual machine specification, to ensure that bytecode files do not threaten the security of the virtual machine, and that the bytecode conforms to the virtual machine specification if Cafebabe

Primary and secondary version number: Four bytes, the first two bytes represents the minor version number, the last two bytes represents the major version number, used to define the compiled bytecode file format version, the first version of the virtual machine refused to run the high-version bytecode file, but the high-version virtual opportunity backwards-compatible with the low-version bytecode file

Constant pool number and constant pool table: this part of the main statistical class or interface literal and symbolic reference, for each class or interface has the literal and symbolic reference properties are not necessarily the same, all need two bytes of length to represent the number of constant pool, the number of constant pool count starting from 1, such as a total of two literal symbol references in the class or interface , the number of constant pools is 2, note that it is not counted from 0, and 0 may be useful to represent a reference that does not appear in the class or interface. The index used to represent the Java.long.Object fully qualified name of the super-parent class, the constant pool table is an array, a total of 18 types, each with a tag property corresponding to it, Constant_utf8_info used to represent the text string, class and interface fully qualified name, Constant information such as field names and descriptors, method names, and descriptors, Constant_integer_info is used to store constant information of type int, only values of type int are stored, and no other conforming reference information is stored, such as  final int a=1  The symbol reference information is a, the value 1,constant_float_info is used to store the float type constant information, and only the float value is stored, no reference information is stored, and Constant_long_info is used to store a long type of constant information constant The _double_info is used to store constant information of type double, constant_class_info is used to store a valid reference to a constant pool list Constant_unf8_info class and interface fully qualified name, Constant_string_ Info stores a valid index of a constant entry for the specified constant pool list constant_utf8_info, points to a text string, and Constant_fieldref_info is used to store a pointer to a constant pool list Constant_class_ An info constant entry and a valid index of the Constant_nameandtype_info constant entry, Constant_methodref_info used to store a pointer to a constant pool list Constant_class_info constant entries and Constant_ Nameandtype_info a valid index of a constant entry, Constant_interfacemethodref_info is used to store a pointer to a constant pool list Constant_class_info constant entries and Constant_ The valid index of the Nameandtype_info constant entry, Constant_nameandtype_info is used to store a valid index to the constant pool list Constant_utf8_info constant entries, and can be found by all the corresponding classes and descriptions, Constant_methodtype_info is used to store Constant_utf8_info constant entries that point to constant pools, represent method types, find corresponding method descriptors, Constant_methodhandle_info to represent method handles, The Reference_kind property represents the type of a method handle, 1-4 represents a method handle created for a field, 5-8 represents a handle created for a class method (construct, instance, static), and 9 represents a handle created for an interface method, Reference_ The Index property points to a list of constant pools that correspond to Reference_kind, and Constant_invokedynamic_info is used to store the bootstrap method in the current bytecode file Bootstrap_ A valid index of the method array and a valid index pointing to the constant pool Constant_nameandtype_info constant entry

Access flags: Two bytes, which defines the access rights of the class or interface represented by the bytecode file, such as whether the class or interface is determined, if the classes are abstract decorated, whether the class is final decorated, acc_interface is the interface, and acc_annotation is the annotation type , Acc_enum represents an enumeration type, acc_public indicates that the class or interface is public access, acc_final indicates that the class is not allowed to be inherited, Acc_abstract represents the class as an abstract class, and Acc_super indicates that the class calls an instance method for special operations

Class index and parent class index: four bytes to store a valid index to the constant pool constant_class_info, and Constant_class_info to store the Constant_utf8_info constant entry index of the constant pool list, The fully qualified name of the class and parent class can be found based on this index

Number of interfaces and Interfaces table: interface properties are used to store the number of direct superclass interfaces of the current class or interface, and the interface table is an array, remember that only the constant pool table is counted from 1, the remainder is counted from the array below table 0, and each array element of the interface table arrays stores a valid index of the direct superclass interface of such or According to this index find constant_class_info corresponding constant term, according to Constant_class_info can find the corresponding direct superclass interface fully qualified name

Number of fields and field table: The number of fields used to store the number of fields for a class or interface, including class and instance fields, the field table as an array, and each array element equal to an FIELD_INFO structure object, and the Field_info structure contains the field full information: field identifier field access modifiers, whether the field is a class field or an instance field, if the field is constant, and so on, note: Here the field represents the field of the current class or interface, excluding fields inherited from the superclass or interface, and the Java syntax specification does not allow different fields to appear in the same class or interface for the same field name. But the class file can allow different fields with the same field name, only the field descriptor is different, Field_info structure properties: Access_flags is used to store the access flags of the field, ACC_PRIVATE,ACC_PROTECTED,ACC_ Public,acc_final,acc_static,acc_volatile, note: acc_final and Acc_volatile cannot appear in the same field at the same time, Name_index point to the constant pool constant_utf8_ The valid index of the info constant entry, gets the simple name of the current field, Descriptor_index a valid index to the constant pool Constant_utf8_info constant entry, gets the descriptor of the current field, attribute_count,attributes [Attributes_count] These two items represent the property information of the field, which is used to describe field-related information, and each element of the attribute array is an element of a attribute_info structure

Method number and Method table: The number of methods used to store the current class or interface method number, including instance method, class method, abstract method, method table is an array, each array element is a method_info structure, method_info structure contains the complete information of the method: Method descriptor , the access modifier, whether it is the final method, whether it is an abstract method, and so on, note that the method array here simply represents all methods of the current class or interface, and does not include methods inherited from the superclass and parent interfaces. Java Syntax specification Java class or interface does not allow the method signature exactly the same way, will compile the error, and the byte-code level allows the method signature exactly the same way, just to ensure that the method's return value type is different, Method_info structure properties and Field_ Info is consistent, but acc_flags types are different, note that acc_abstract cannot be used with acc_final,acc_static,acc_private,acc_synchronized,acc_native at the same time, Interface methods can only use acc_public,acc_abstract, here I have a question, the interface in the default method has no corresponding access flags, note that the:<init> method can only use ACC_PRIVATE,ACC_ Protected,acc_public Access Flag

Number of attributes and attribute table: The number of attributes represents the number of Attribute_info tables in the current bytecode file, the attribute table is an array, and each array element is a attribute_info structure, attribute_ The info structure represents the complete information for a property: The first item points to a valid index of a constant constant_utf8_info a const item, and according to this index, the attribute name can be found, and the attribute may appear in the Classfile, Field table, method table to describe the relevant information, Attribute_info structure Complete information: attribute_name_index pointer to a constant pool constant_utf8_info a valid index of a constant entry, gets the property name, Attribute_length is used to indicate the info array length, INFO[ATTRIBUTE_LENGTH] Data information that is used to store properties, data information that must be implemented: Code,constantvalue,exceptions, the Code attribute is used to describe specific information about Method_info, Attribute_name_index a valid index to the constant pool Constant_utf8_info constant entry, gets the property name, that is, code,attribute_length represents the attribute_info length for attribute , excluding the initial 6 bytes of Attribute_name_index and Attribute_length, Max_stack describes the maximum stack depth of the current method, max_locals a local variable table describing the current method, Code_ Length is the code array, code[] represents the compiled bytecode of the current method, Contantvaue is in the field_info structure, and is used to notify the virtual machine to initialize the class variable, where the field needs to be acc_static decorated. The current class constants do not require Contantvalue to notify the virtual machine to perform the initialization, the instance fields do not need to be initialized at this time,

Java bytecode file detailed

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.