Class file structure

< H1 align = "left" > platform independence

Java is a platform independent language, which benefits from the bytecode storage file generated after java source code compilation, that is, class file, and the implementation of Java virtual machine. Not only can java code be compiled into a class file storing bytecode by using java compiler, but also program code can be compiled into a class file by using compilers of other languages such as jruby. Virtual machine does not care what language class comes from. As long as it conforms to a certain structure, it can run in Java. The semantics of various variables, keywords and operators in the Java language are ultimately composed of multiple bytecode commands. Therefore, the semantic description ability provided by bytecode commands will certainly be stronger than that of the Java language itself, which provides a basis for other languages to realize some language features different from Java, And this is the reason for security verification when loading classes.

< H1 align = "left" > < a name = "T1" target = "_blank" > class file structure < p align = "left" > class file is a group of binary streams based on 8-bit bytes. Each data item is arranged in the class file in strict order without any separator in the middle, which makes almost all the contents stored in the whole class file are necessary data for program operation. According to the Java virtual machine specification, the class file format is stored in a pseudo structure similar to the C language structure. There are only two data types in this pseudo structure: unsigned number and table. Unsigned numbers belong to the basic data type. U1, U2, U4 and U8 represent unsigned numbers of 1, 2, 4 and 8 bytes respectively. A table is a data type consisting of multiple unsigned numbers or other tables as data items. All tables habitually end with "_info".

Magic and version

The first four bytes of each class file are called magic numbers (Magic), whose only function is to judge whether the file is a class file that can be accepted by the virtual machine. Its value is fixed to 0xcafebabe. The next four bytes of magic store the minor version number and major version number of the class file. The JDK of higher version can be downward compatible with the class file of lower version, but can not run the class file of higher version.

access_ flag

After the constant pool ends, The next two bytes represent the access flag (access_flag). This flag is used to identify the access information of some classes or interface levels, including: whether this class is a class or interface, whether it is defined as public type, abstract type, if it is a class, whether it is declared as final, etc. each access information is represented by a hexadecimal flag value. If there are multiple access information at the same time, the flag is obtained The value is the logical or of the flag value of these kinds of access information.

this_ class、super_ class、interfaces

fields

Field_info is used to describe variables declared in an interface or class. Fields include class level variables or instance level variables, but do not include variables declared in methods. Field names, data types, modifiers, etc. cannot be fixed, and can only be described by referring to constants in the constant pool. The following is the latest format of the field table:

methods

attributes

There are too many systems in the attribute_info. You can carry your own attribute table set in the class file, field table and method table to describe the specific information of some scenarios.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>