In class files, field tables, method tables, you can bring your own collection of property sheets to describe the information that is proprietary to certain scenarios. Unlike the order, length, and content requirements of other data items in the class file, the restrictions on the set of attribute tables are slightly looser, no longer require that the individual property sheets be in strict order, and any compiler that implements it can write its own defined property information to the property sheet, as long as it is not duplicated with the existing property name. The Java virtual runtime ignores properties that it does not recognize. In order to parse the class file correctly, the Java Virtual Machine Specification (second edition) defines the attributes that should be recognized by the 9 recommended virtual machine implementations, as shown in the following table:
For each property, its name needs to be represented by a constant table of type constant_utf8_info from the constant pool, and the structure of the property value is fully customizable, as long as the number of bits that the property value occupies is stated. A conforming attribute table should meet the structure defined by the following table:
1.Code Properties
After the code in the Java program method body is processed by the Javac compiler, the bytecode instruction is eventually stored inside the code attribute. The Code property appears in the property collection of the method table, but not all methods must have this property sheet, such as an abstract method in an interface or abstract class that does not have a code property, and if the method has a code attribute table, its structure is the following table:
Attribute_name_index is an index to the Constant_utf8_info constant table, which is fixed to "Code", which represents the property name of the attribute, attribute_length indicates the length of the property value, Because the property name index and property length are altogether 6 bytes, the length of the property value is fixed to the length of the entire property sheet minus 6 bytes.
The max_stack represents the maximum depth of the operand stack (Operand Stacks). At any point in the execution of the method, the operand stack does not exceed this depth. This value is required to allocate the operand stack depth in the stack frame (frame) when the virtual machine is running.
Max_locals represents the storage space required for a local variable table. Here, the unit of Max_locals is Slot,slot is the smallest unit used by the virtual machine to allocate memory for the local variable table. For data types with a length of not more than 32 bits, such as byte,char,float,int,shot,boolean,reference and returnaddress, each local variable accounts for 1 slots, A double and a long are two 64-bit data types that require 2 slots to store. The method parameter (including the hidden parameter "This" in the instance method) displays the parameters of the exception handler (Exception Handler Parameter, which is the exception defined by the catch block in the Try-catch statement), Local variables that are defined in the method body need to be stored using local tables. In addition, instead of using more than one local variable in the method, the sum of the slots of these local variables is used as the max_locals value, because slots in the local variable table can be reused, and when code executes beyond the scope of a local variable, The slot in which the local variable resides can be used by other local variables, and the compiler classifies the slots according to the scope of the variables and assigns them to each variable, then calculates the size of the max_locals.
Code_length and code are used to store bytecode instructions generated by the Java source program after compilation. The code_length represents a byte-code length, and code is a series of byte streams used to store bytecode instructions. Since the name is a bytecode instruction, then each instruction is a single byte of the U1 type, and when the virtual machine reads a byte code in code, it can find out what instruction the bytecode represents, and you can know whether the instructions need to follow the parameters, and how the parameters should be understood.
There is one more thing to note about Code_length, although it is a U4 type of length value, theoretically the maximum can reach 2 of 32 square minus 1, but the virtual machine specification limits a method does not allow more than 65,535 bytecode instructions, if the limit is exceeded, The Javac compiler will reject the compilation. In general, as long as we write Java code without deliberately writing an extra-long method, we do not exceed this maximum limit. However, in compiling a complex JSP file, you can cause compilation to fail for this reason.
The code attribute is the most important attribute in a class file, and if the information in a Java program of a table is divided into two parts (code, Java code in the method body) and metadata (Metadata, including classes, fields, method definitions, and other information), then in the entire class file, The code attribute is used to describe the tag, and all other data items are used to describe the metadata.
After the bytecode instruction is the display exception handling table for this method, the exception table is not required for the Code attribute table. The format of the exception table is the following table:
Exception table It contains 4 fields that have the meaning: if the bytecode is from start_pc to END_PC (not including the END_PC), an exception with the type Catch_type or its subclasses appears (Catch_type to point to a constant_ CLASS_INFO constant is indexed), go to line handler_pc to continue processing. When the value of Catch_type is 0 o'clock, any exception will need to be shifted to the handler_pc row for processing. The exception table is actually part of the Java code, and the compiler uses the exception table instead of the simple jump command to implement the Java exception and the finally processing mechanism. Note: the "line" of bytecode is an image description, which refers to the byte code offset from the beginning of the method body, not the line number of the Java source code.
2.Exceptions Properties
The Exceptions property here is an attribute in the method table that is the same as the Code property, not the exception property sheet in the Code property sheet. The Exceptions property sheet is an enumeration of the Checked Exception that may be thrown in the method, that is, the exception that is enumerated after the throws keyword in the method description. Its structure is the following table:
The Number_of_exceptions entry in this property sheet indicates that adapting may throw number_of_exceptions types of checked exceptions, each of which is represented by a exception_index_table entry. The index that points to the Constant_class_info constant table in the constant pool, which represents the type of the checked exception.
3.LineNumberTable Properties
The Linenumbertable property is used to describe the correspondence between the Java source code line number and the byte Code line number (byte code offset). It is not a required property at runtime, but it is generated by default into a class file, and can be canceled or required to generate this information in Javac using the-g:none or-g:lines option. If you choose not to generate the Linenumbertable attribute table, the most important effect on running a program is that when an exception is thrown, the line number of the error is not displayed in the stack, and the breakpoint cannot be set according to the source code when the program is debugged. The Linenumbertable property sheet structure is the following table:
Line_number_table is a set of Line_number_table_length, type Line_number_info, Line_number_info table includes start_pc and Line_ Number two a data item of type U2, which is a byte code line, which is a Java source line number.
4.LocalVariableTable Properties
The Localvariabletable property sheet is used to describe the relationship between variables in a local variable table in a stack frame and variables defined in Java source code, which is not a required property at run time and is not generated by default into a class file, and can be used with-g:none or-G: VARs option to cancel or require this information to be generated. If this property is not generated, the biggest effect is that when others refer to the method, all parameter names are lost, and the IDE may replace the original parameter names with placeholders such as arg0, Arg1, which has no effect on the program's operation, but it can cause great inconvenience to code writing. Also, you cannot get the parameter values from the run-time and up-down file based on the parameter name during debugging. The Localvariabletable property sheet structure is as follows:
The Local_variable_info project represents a stack frame associated with a local variable in the source code, with the following structure:
Index is the slot position of this local variable in the stack frame local variable table. When the data type of this variable is 64 bits (double and long), it occupies a slot of index and index+1 two positions.
After JDK1.5 introduced generics, the Localvariabletable attribute adds a "sister" attribute: Localvaiabletypetable, the newly added property structure is very similar to the Localvariabletable property. Simply replace the descript_index of the record field descriptor with the signature of the field (Singnature), and for non-generic types the parameterized type of the descriptor is erased, and the descriptor cannot accurately describe the generic type. Therefore, the Localvariabletypetable attribute appears.
5.SourceFile Properties
The SourceFile property is used to record the name of the source file that generated this class file. This property is also optional and can be canceled or required to generate this information using the-g:none or-g:source option. In Java, the class name and file are consistent for most classes, but there are special cases (such as inner classes) that are exceptions. If you do not generate this property, when you recruit an exception, half of the stack does not display the error code's owning file name. This property is a property of a room length and is structured as follows:
The Sourcefile_index data item is the index of the Constant_utf8_info constant in the constant pool, and the constant value is the file name of the source file.
6.ConstantValue Properties
The purpose of the Constantvalue property is to notify the virtual machine to automatically assign a value to a static variable. This property can only be used by variables modified by the static keyword. Variable definitions such as "int x = 123" and "static int x = 123" are common in Java programs, but the method and time at which a virtual machine assigns values to these two variables is different. The assignment of a variable of a non-static type (that is, an instance variable) is done in the instance constructor <init> method, and there are two options for the class variable: assignment is done in the class constructor <clinit> method, Or use the Constantvalue property to assign a value. The current selection of the Sun Javac compiler is: If you use both final and static to modify a variable, And the data type of this variable is the basic type or java.lang.String, generate the Constantvalue property to initialize, if the variable is not final decorated, or is not a basic type or string, then select in <client> Initialized in the Class builder. The Constantvalue property sheet structure is as follows:
The Constantvalue property is a fixed-length property whose attribute_length data item value must be 2. Constantvalue_index data items represent a reference to a literal constant in a constant pool, and depending on the field type, the literal can be constant_long_info,constant_float_info,constant_double_ One of the Info,constant_integer_info and Constant_string_info constants.
7.InnerClasses Properties
The Innerclasses property sheet is used to record the association between the inner class and the host class. If an inner class is defined in a class, the compiler generates a Innerclasses property sheet for it and the inner classes it contains. The table structure is as follows:
The data item number_of_classes represents the number of internal class information that needs to be recorded, and the information for each of the inner class's classes is described by a inner_class_info table. The Inner_class_info table structure is as follows:
Both Inner_class_info_index and Outer_class_info_index are indexes that point to Constant_class_infon constants in a constant pool, representing the symbolic references of the inner class and the host class, respectively. Inner_name_index is the index of the Constant_utf8_info constant in the constant pool, which represents the name of the inner class, or 0 if it is an anonymous inner class. Inner_class_access_flags is an access flag for an inner class, typed in the class's access_flags, and its value range is the following table:
8.Deprecated and Synthetic properties
The deprecated and synthetic properties are attribute to the Boolean property of the flag type, only with and without the difference, without the concept of a property value.
The deprecated property is used to represent a class, field, or method that has been designated by the program author as deprecated and can be set by using @deprecated annotations in code.
Synthetic represents this field or method is not directly generated by the Java source code, but is added by the compiler itself, after JDK1.5, identifies a class, the field or method is the compiler automatically generated, you can also set their access flag in the acc_synthetic flag bit, The most typical of these is bridge Method. All classes, methods, and fields that are produced by non-user code should have at least one entry in the synthetic property and the acc_synthetic flag bit, with the only exception being the instance constructor "<init>" method and the class constructor "<clinit" method.
The deprecated and synthetic attribute tables are structured as follows:
Where the value of the ATTRIBUTE_LENGTH data item must be 0, because there is no property value that needs to be set.
A total of 10 attributes were added in JDK1.5 and JDK1.6, as follows:
In-depth understanding of Java Virtual Machine Notes---property sheet collections