In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the Java bytecode file in JVM example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Each class bytecode file uniquely corresponds to a class or interface. The basic information of the class or interface is recorded in the class file, but the reverse is not true. Not every class or interface has a unique corresponding bytecode file. First of all, the bytecode of the class or interface can not be stored as a file, and the bytecode can be generated directly from memory instead of .class files. The principle of dynamic proxy is to generate byte code stream directly in memory, load the class according to the loaded byte code stream, instantiate the class and generate the proxy object.
Information recorded in bytecode files: magic, primary and secondary versions of class files, number of constant pools and constant pool tables, access flags of classes or interfaces, class indexes, superclass indexes, number of interfaces and interface tables, number of fields and field tables, number of methods and methods table, sjz.java.tedu.cn attributes and property tables, class c structures are used to represent each attribute in jvm, there are two types of structures, unsigned numbers and tables The table ends with _ info, and u1, 2, 4, and 8 bytes are represented by u1, 2, 4 and 8 bytes, respectively
ClassFile {
U4 magic
U2 minor_version
U2 major_version
U2 constant_pool_count
Cp_info constant_ pool[constant _ pool_count]
U2 acc_flags
U2 this_class
U2 super_class
U2 interfaces_count
U2 interfaces[interfaces _ count]
U2 fields_count
Field_info fields[fields _ count]
U2 methods_count
Method_info methods[methods _ count]
U2 attributes_count
Attribute_info attributes[attributes _ count]
}
Magic: four bytes are used to define whether the bytecode file conforms to the virtual machine specification, ensuring that the bytecode file does not threaten the security of the virtual machine. If cafebabe, the bytecode conforms to the virtual machine specification.
Major and minor version number: four bytes, the first two bytes represent the minor version number, and the last two bytes represent the major version number, which is used to define the compiled bytecode file format version. The low version virtual machine refuses to run the high version bytecode file, but the high version virtual machine is downwards compatible with the low version bytecode file.
Constant pool number and constant pool table: this part mainly counts the literals and symbolic references of classes or interfaces. The literal quantity and symbolic reference attributes of each class or interface are not necessarily the same. All need two bytes in length to represent the number of constant pools. Constant pool count starts from 1. For example, if there are a total of two literal symbolic references in a class or interface, the number of constant pools is 2. Note that it is not counted from 0. 0 may be used to represent references that do not appear in the class or interface, to represent the index of the super parent class java.long.Object fully qualified name, constant pool table is an array of 18 types, each type has a tag attribute corresponding to it, constant_utf8_info is used to represent constant information such as text strings, class and interface fully qualified names, field names and descriptors, method names and descriptors Constant_integer_info is used to store constant information of int type. Only values of int type are stored, and no other reference information is stored. For example, the symbol reference information of final int axiom 1 is a, and the value is 1, which is used to store constant information of float type. It only stores float values, not reference information, constant_long_info is used to store constant information of long type, and constant_double_info is used to store constant information of double type. Constant_class_info is used to store valid references to constant pool list constant_unf8_info class and interface fully qualified names, constant_string_info is used to store a valid index of a constant item in the specified constant pool list constant_utf8_info, points to a text string, and constant_fieldref_info is used to store valid indexes to constant pool list constant_class_info constant items and constant_nameandtype_info constant items Constant_methodref_info is used to store valid indexes to constant pool lists constant_class_info constant items and constant_nameandtype_info constant items, constant_interfacemethodref_info is used to store valid indexes to constant pool lists constant_class_info constant items and constant_nameandtype_info constant items, constant_nameandtype_info is used to store valid indexes to constant pool list constant_utf8_info constant items, and corresponding classes and descriptors can be found through all Constant_methodtype_info is used to store constant_utf8_info constant items pointing to the constant pool, denote the method type, find the corresponding method descriptor, constant_methodhandle_info is used to represent the method handle, property reference_kind represents the type of the method handle, 1-4 represents the method handle created for the field, 5-8 represents the handle created for class methods (construction, instance, static), 9 represents the handle created for the interface method The reference_index attribute points to the list of constant pools corresponding to reference_kind, and constant_invokedynamic_info is used to store the valid index of the bootstrap method bootstrap_method array in the current bytecode file and the valid index to the constant pool constant_nameandtype_info constant item.
Constant_utf8_info {
U1 tag
U2 length
U1 bytes [length]
}
Constant_integer_info {
U1 tag
U4 bytes
}
Constant_float_info {
U1 tag
U4 float
}
Constant_long_info {
U1 falg
U8 long
}
Constant_double_info {
U1 tag
U4 double
}
Constant_class_info {
U1 tag
U2 name_index
}
Constant_string_info {
U1 tag
U2 string_index
}
Constant_fieldref_info {
U1 tag
U2 class_index
U2 name_and_type_index
}
Constant_mathodref_info {
U1 tag
U2 class_index
U2 name_and_type_index
}
Constant_interfacemathodref_info {
U1 tag
U2 class_index
U2 name_and_type_index
}
Constant_nameandtype_info {
U1 tag
U2 class_index
U2 descriptor_index
}
Constant_methodhandle_info {
U1 tag
U2 reference_kind
U2 reference_index
}
Constant_methodtype_info {
U1 tag
U2 descriptor_index
}
Constant_invokedynamic_info {
U1 tag
U2 bootstrap_method_attr_index
U2 name_and_type_index
}
Access flag: two bytes used to define the access rights of the class or interface represented by the bytecode file, such as determining whether the class is modified by abstract or interface, whether the class is modified by final, acc_interface indicates an interface, acc_annotation indicates an annotation type, acc_enum indicates 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, and acc_abstract indicates that the class is abstract. Acc_super indicates that the class needs to take special actions when calling the instance method
Class index and parent index: four bytes used to store a valid index to the constant pool constant_class_info, while constant_class_info stores the constant_utf8_info constant item index of the constant pool list, according to which the fully qualified names of the class and parent class can be found
Number of interfaces and interface table: interface properties are used to store the number of direct superclass interfaces of the current class or interface. The interface table is an array. Remember, only the constant pool table counts from 1, and the rest starts from table 0 below the array. Each array element of the interface table array stores the valid index of the direct superclass interface of this class or interface. Find the constant item corresponding to constant_class_info according to this index. According to constant_class_info, you can find the fully qualified name of the corresponding direct superclass interface.
Number of fields and field table: the number of fields is used to store the number of fields of a class or interface, including class fields and instance fields. The field table is an array, and each array element is equivalent to an object of field_info structure. The field_info structure contains the complete information of the field: field identifier, field access modifier, whether the field is a class field or an instance field, whether the field is constant, etc. Note: the fields here represent the fields of the current class or interface, excluding fields inherited from the superclass or interface. The java syntax specification does not allow different fields with the same field name in the same class or interface, but different fields with the same field name are allowed in the class file, only with different field descriptors. Field_info structure attributes: access_flags is used to store field access flags, acc_private,acc_protected Acc_public,acc_final,acc_static,acc_volatile, etc., note: acc_final and acc_volatile cannot appear in the same field at the same time. Name_index points to the valid index of the constant pool constant_utf8_info constant item, gets the simple name of the current field, and descriptor_index points to the valid index of the constant pool constant_utf8_info constant item. Get the current field descriptor, attribute_count, attributions [attributes _ count], these two items represent the attribute information of the field. Used to describe field-related information, each element of the attribute array is an element of an attribute_info structure
Field_info {
U2 access_flags
U2 name_index
U2 descriptor_index
U2 attributes_count
Attribute_info attributes[attribuyes _ count]
}
Number of methods and method table: the number of methods is used to store the number of methods of the current class or interface, including instance methods, class methods, abstract methods, method table is an array, each array element is a method_info structure, method_info structure contains complete information about methods: method descriptors, access modifiers, whether it is a final method, whether it is an abstract method, etc. Note: the method array here only represents all methods of the current class or interface, and does not include methods inherited from the superclass and parent interface. The java syntax specification java class or interface does not allow methods with exactly the same method signature to appear at the same time, which will compile and report errors, while the bytecode level allows methods with exactly the same method signature to appear at the same time. You only need to ensure that the return value types of the methods are different. The structural attribute of method_info is the same as that of field_info, but the type of acc_flags is different. Note that acc_abstract cannot be used with acc_final,acc_static,acc_private,acc_synchronized,acc_native at the same time, and only acc_public,acc_abstract can be used in interface methods. Here I have a question: does the default method in the interface have corresponding access flags? note: methods can only use acc_private,acc_protected,acc_public access flags.
Method_info {
U2 access_flags
U2 name_index
U2 descriptor_index
U2 attributes_count
Attribute_info attributes[attribuyes _ count]
}
Number of attributes and property sheet: the number of attributes indicates the number of attribute_info tables in the current bytecode file. The attribute table is an array, each array element is an attribute_info structure, and the attribute_info structure represents the complete information of the attribute: the first item points to the valid index of a constant item in the constant pool constant_utf8_info. According to this index, the corresponding name of the attribute can be found. The attribute can appear in the classFile, field table, and method table. Used to describe the relevant information, attribute_info structure complete information: attribute_name_index points to the valid index of constant pool constant_utf8_info constant items to obtain the attribute name, attribute_length is used to indicate the length of the info array, and info[ attribute _ length] is used to store the data information of attributes. The data information that must be implemented: Code,ConstantValue,Exceptions, code attributes are used to describe the specific related information of method_info. Attribute_name_index points to the valid index of the constant pool constant_utf8_info constant item and obtains the attribute name, that is, code,attribute_length represents the attribute_info length, 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 describes the local variable table of the current method, code_length represents the length of the code array, and code [] represents the compiled bytecode of the current method. ContantVaue is located in the field_info structure, which is used to notify the virtual machine to initialize the class variable, that is, the field needs to be modified by acc_static. The current class constant does not need to be initialized by ContantValue, so it has already been executed, and the instance field does not need to be initialized at this time.
Attribute_info {
U2 attribute_name_index
U4 attribute_length
U1 info[attribute _ length]
}
Code_attribute {
U2 attribute_name_index
U4 attribute_length
U2 max_stack
U2 max_locals
U4 code_length
U1 code[code _ length]
U2 exception_table_length
Exception_info exception_ table[exception _ table_length]
U2 attributes_count
Attribute_info attributes[attributes _ count]
}
Constantvalue_attribute {
U2 attribute_name_index
U4 attribute_length
U2 constantvalue_index
}
Simple name and descriptor:
A simple name refers to a simple description of a field or method. For example, the simple name of the person class:private String name field is name,Object class:public String toString (). The simple name of the method is toString. Note: the simple name of the field or method is not allowed. : / [equivalent ASSIC or Unicode character representation
Descriptor generally refers to the type of field or method, field descriptor represents field type description, method descriptor represents parameter description and return value description, for example: person class:private String name field descriptor is Ljava.lang.String,com.test.Person class:setName (String name) method descriptor is (Ljava.lang.String) V, note: v indicates that the return value is void type, and if the parameter is void, it is expressed as () Ljava.lang.Object. A method descriptor that indicates that the return value of a no parameter is a java.lang.Object reference type
Field descriptor interpretation: B byte byte type, Z boolean Boolean type, C char character type, S short short integer, I integer, L long long integer, F float single schedule floating point type, D double double precision floating point type, L reference object reference type, [reference array reference type
The above is all the contents of the article "sample Analysis of Java Bytecode Files in JVM". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.