Java Class file internal structure resolution procedures

  • 2020-04-01 02:32:06
  • OfStack


When I was in college, I had read the second edition of the Java virtual machine specification (JVM), and recently reviewed the latest version of the third edition (Java se version 1.7) of the Java virtual machine specification.
The Java language is cross-platform, so to speak, written once and run everywhere. Is cross-platform, Java defines a set of the operating system, hardware irrelevant bytecode format, the bytecode is represented with Java class files, Java class file defines the virtual machine can identify the internal bytecode format, this format is platform independent, on Linux system or in the Windows system are consistent. This is like an HTML file, where we define the specification, and the system just displays the contents according to the specification. Just like HTML is a class file, the browser is a virtual machine, through the browser to perform the rendering process of HTML, whether we use mobile phone, Windows, apple system Internet, display the content is the same. The Java virtual machine can load predefined bytecode from a class file, or from a network, database, or message file.
Next comes the Java class file structure, which is based on byte streams and encoded with unicode. If you think about it, you can also represent it entirely in an XML file, except that the bytecode text represented in XML can be large, take up a lot of space, time-consuming to parse, and easy to manually modify, resulting in unknown errors. Below is the internal structure of the class file


ClassFile { 
u4 magic; 
u2 minor_version; 
u2 major_version; 
u2 constant_pool_count; 
cp_info constant_pool[constant_pool_count-1]; 
u2 access_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]; 
}


< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201311/20131128093705.jpg? 2013102893835 ">


A brief explanation:

U4 represents four unsigned bytes

U4 magic: is a fixed number, called magic number in the Java virtual machine, which is used to identify whether the file structure is supported by the Java virtual machine. Currently, it is 0xCAFEBABE

U2 minor_version; U2 major_version; Represents the minor version number and the major version number

U2 constant_pool_count; Cp_info constant_pool [constant_pool_count - 1); This is the number of constant pools and the constant pool information

U2 access_flags: represents the class access flag, for example: public protected

U2 this_class: represents the name of this class such as java.lang.object

U2 super_class: stands for parent class name

U2 interfaces_count; U2 interfaces [interfaces_count]; Implementation of the interface format and interface class name

U2 fields_count; Field_info fields [fields_count]; Number of fields and field information

U2 methods_count; Method_info the methods [methods_count]; Number of methods and method information

U2 attributes_count; Attribute_info attributes [attributes_count]; The internal attribute information in the Java class file has nothing to do with the properties defined in the Java language and is intended solely for the Java virtual machine

All of the above is the internal structure of the class file defined in the Java virtual machine specification (JVM), which can be referred to in the Java virtual machine specification (Java SE version 7)

Each structure defines its own structure information, such as: constant pool structure, field information, method information, class information, and these structures refer to each other

The information in the constant pool is the most complex of all. All the information at runtime of the Java virtual machine is obtained from the constant pool, which defines several structures

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201311/20131128093733.jpg? 2013102894018 ">


Related articles: