Chapter 6 Java Class File (Chapter Six: The Java Class File)

xiaoxiao2021-04-08  5

First, what is the Java class file Java class file is a binary representation of the Java program. Each class file represents a class or interface. It is not possible to put multiple classes or interfaces in a class file. This makes it executed on any host regardless of which type of platform is generated regardless of the class file. Although class files are part of the Java architecture, he is not unacceptable to Java language. You can compile the programs of other languages ​​into class files, or compile the Java program file into other binary forms. The Java class file is a binary stream based on the 8-bit byte. The data block sequence, the form of a splitcher, the form of BIG-Endian is stored. Second, the contents of the class files Java's class file contains information about classes and interfaces required for all Java virtual machines. All types of information in all types of files are stored in the following four basic types of storage: Table 6-1. Class File "Primitive Types" U1 A Single Unsigned byte U2 Two unsigned bytes U4 FOUR UNSIGNED BYTES U8 Eight Unsigned Bytes class files in the main in order storage part table 6-2: table 6-2 Format of a ClassFile table Type & # 9; Name Count u4 magic 1 u2 minor_version 1 u2 major_version 1 u2 constant_pool_count 1 cp_info constant_pool constant_pool_count-1 u2 access_flags 1 u2 this_class 1 u2. super_class 1 u2 interfaces_count 1 u2 interfaces interfaces_count u2 fields_count 1 field_info fields fields_count u2 methods_count 1 method_info methods methods_count u2 attributes_count 1 attribute_info attributes attributes_count 1, the beginning of the magic code (magic) a Java class file for each four bytes are the magic code (OxCAFEBABE ). Class files can be easily identified by magic coding. 2, the four bytes left by the sub-version number and the main version (Minor_Version and Major_Version) are the secondary version number and the main version of the version. But Java technology is evolving, some new features may be added to class files. The change in the format of each class file will change the version number. The virtual machine recognizes the class file you can handle through the version number. Java virtual machines often handle only a given home version number and some of the sub-version numbers under. The virtual machine must reject those documents that are no longer processed. 3, the constant number and constant pool (constant_pool_count and constant_pool) Next is the constant pool, which contains which categories or interfaces accessed, such as strings, Final Variable Values, class names, Method name. The constant pool is stored as a list.

The number of constants in the list is the previously saved "constant_pool_count". Many constants in a constant pool references other constants in a constant pool, which will eventually convert direct references to constants in constant pools. Although the index in the constant list starts from 1, the number of constants is still 0, such as 15 in a constant list, then its constant number is 16. Every constant will have a sign to represent his type. When the virtual machine reads this flag, you will know the specific type of this constant. Table 6-3 lists these flags:. Table 6-3 Constant pool tags Entry Type Tag Value Description CONSTANT_Utf8 1 A UTF-8 encoded Unicode string CONSTANT_Integer 3 An int literal value CONSTANT_Float 4 A float literal value CONSTANT_Long 5 A long literal value CONSTANT_Double 6 A double literal value CONSTANT_Class 7 A symbolic reference to a class or interface CONSTANT_String 8 A String literal value CONSTANT_Fieldref 9 A symbolic reference to a field CONSTANT_Methodref 10 A symbolic reference to a method declared in a class CONSTANT_InterfaceMethodref 11 A symbolic reference to a method declared In an interface constant_nameAndType 12 Part of a Symbolic Reference To A Field Or Method Table 6-3 There is a corresponding table to describe some of the details indicated by this flag, these corresponding flags will The logo name _info is end. For example, the constant_class flag corresponds to constant_class_info. The constant pool plays a very important role in the dynamic link of the program. In addition to the various constant values ​​on the top, the constant pool contains three symbols references: the full name, field name, and descriptor, method name, and descriptor of the class and interface. A field is an instance or class variable in a class or interface, and the field descriptor is the type of field. The descriptor of the method is the number, order, and type of the method and the return value and parameters. Use these full names when the virtual machine links this or interface to other classes or interfaces. Since the class file does not contain any information about the memory structure, this link can only be present in the form of symbol reference. The virtual machine converts these symbols to the actual address when executed. See Chapter 8, "The Linking Model" in Chapter VII.

4. Access flag (Access_flags) The two bytes behind the constant pool are access flags, indicating aspects of this class or interface, and he has the following values: Table 6-4. Flag Bits in the access_flags item of ClassFile tables Flag Name Value Meaning if Set Set By ACC_PUBLIC 0x0001 Type is public classes and interfaces ACC_FINAL 0x0010 class is final classes only ACC_SUPER 0x0020 Use new invokespecial semanticsClasses and interfaces ACC_INTERFACE 0x0200 Type is an interface, not a class All interfaces, no classes ACC_ABSTRACT 0x0400 Type Is Abstract All Interfaces, the Some Classes Acc_super flag is to comply with the old Sun's vintage compiler. All access signs that are not used must be set to 0. 5. The next two bytes of the class name (this_class) saves an index of a constant pool. The entity in this constant pool must be constant_clss_into type, and he contains the logo and name index. The logo is constatn_class, and that name index should be an index of the constant_utf8_info type that saves this class or interface. 6. After the parent class (super_class) this_class is the two bytes of super_class, and he is also an index of a constant pool, which saves the full name of the parent class, which is the same as this_class. When the father category is java.lang.object, Super_Class should be 0. All interface Super_class is 0. 7. (Interfaces_count and interfaces) interfaces_count saves the number of parent interfaces, and some constant pools are saved in an array form in Interfaces. Each index pointing to a constant of constant_class_info, where the full name of each parent interface is saved. The order of this array is that the parent interface appears in the order of left to right in the Implements, Extends statements. 8, (Fields_Count and Fields) field Save in a list of Field_info, Fields_count is the length of this list. The field saved in the field_info list is just the variables in the class or interface, the field inherited from the parent class or parent interface is not saved here. One of the Field_INFO tables describes information about a field, including: field name, descriptor, access. If a field is declared for final, the information of this field is saved in the field_info table, and it is saved in a constant pool. 9. The information of the (Methods_Count and Methods) method is saved in the method_info table, and mehtods_count is the length of the table. Method_INFO tables only save classes or interfaces in the interface, do not save methods from the parent class or interface. The method name and descriptor (return value and parameter type) are saved in the Method_INFO table.

If it is not an abstract method, it will also store the size of the stack (saving local variable), the maximum value of the operand stack, the captured exception list, the selection code, the optional line number, and the local variable table. If the method throws some exceptions being checked, Method_info will also contain a list of inspected exceptions. 10, (Attributes_Count and Attributes) class files are the number of attributes and a list of Atribute_info. The Atribute_info table saves some indexes to constant_utf8_info in the constant pool, where the name of the property is saved. Two types of properties are defined in the Java virtual machine specification: source code and internal classes. Third, the character reference in the Special Strings Constant Pool contains three specific strings: fully qualified name, simple name, descriptor. All character references for a class or interface must contain a fully qualified name. Each field or method has a simple name and descriptor as a fully qualified name. These specific strings are used to represent the simple names and descriptors and descriptors of the classes defined in the file. 1. Fully Qualified Names This class or interface is provided, such as java.lang.Object when references the classes and interfaces in a constant pool. 2, Simple Names fields and methods are saved in a simple name in the form of a simple name. For example, there is a reference to the String toString () method of a java.lang.object class in a constant pool, saving "toString"; java.lang.system out field, saved as "OUT". 3, the descriptor "field and method of the character reference will contain a descriptor. The descriptor of the field provides the type of field. The descriptor of the method provides the return value, parameter number, parameter type.

All descriptors have Type List: FieldDescriptor: FieldType ComponentType: FieldType FieldType: BaseType ObjectType ArrayType BaseType: Terminal Type B byte C char D double F float I int J long S short Z boolean ObjectType: L ; ArrayType: [ComponentType ParameterDescriptor : FieldType MethodDescriptor: (ParameterDescriptor *) ReturnDescriptor ReturnDescriptor: FieldType V Table 6-6 Examples of field descriptors Descriptor Field Declaration I int i; [[J long [] [] windingRoad; [Ljava / lang / Object; java.lang.. Object [] stuff; ljava / util / hashtable; java.util.hashtable Ht; [[z boolean [] [] [] isready; table 6-7. Examples of method descriptor descriptor method declaration () i int getsize () ; () Ljava / lang / string; string toString (); ([ljava / lang / string;) v vioid main (string [] args; () V void Wait () (Ziljava / Lang / String; II) Z Boolean RegionMatches (Boolean Ignorecase, Int Tooffset, String Other, Int Ooffset, INT LEN); [BII) I I int i (Byte [] B, INT OFF, INT LEN; four, constant pool constant pool is organized into a list of cp_info types,


New Post(0)