Analysis of the Class file structure on the path of JVM exploration (1): Format and definition of Class files

Source: Internet
Author: User

JVM learning notes directory:

Parsing the Class file structure of JVM exploration path (2): constant pool

JVM exploration path Class file structure parsing (3): access modifier, Class index, parent index, and interface index set

Format and definition of Class files

A Class file is a set of binary streams in 8 bytes (1 Byte = 8 bit, and the computer also has 1 Byte = 16 bit or 1 Byte = 32bit, each data item is strictly ordered in the Class file, without any separator. When a data item needs to occupy more than 8 bytes of space, it is divided into several 8 bytes for storage in the way of the top position.

There are only two types of data in the Class file structure: unsigned number and table.Unsigned numberIs a basic data type. u1, u2, u4, and u8 are used to represent the unsigned numbers of 1 byte, 2 byte, 4 byte, and 8 byte, respectively, the unsigned number can be used to describe the number, index reference, number value and a string consisting of UTF-8 encoding;TableIs a composite data type consisting of multiple unsigned numbers or other tables as data items, all tables are used to ending with "_ info" (the entire Class file can be regarded as a table ).

It is the structure table of the Class file, whether it is a number of unsigned or table type, when you need to describe multiple data of the same type but the number is not fixed. It is often used to add several consecutive data items to a front-end capacity counter. For example, contant_pool_count, interfaces_count, fields_count, and methods_count are all front-end capacity counters.

To facilitate the interpretation of these data types, a very simple class is provided as an example:

package com.beliefbetrayal.clazz;

public class ClassFileTest {

private int m;

public int getM() {
return m;
}

public void setM(int m) {
this.m = m;
}
}

This example is very simple. It defines a class, which has a member variable and the corresponding get and set methods. Now we can find the Class file corresponding to this Class and use WinHex (open the binary file in hexadecimal format, 1 Byte = 8 bit, so two hexadecimal numbers represent one Byte) open the Class file. The following information is displayed:

The figure shows some information about the Class file.

The first Data Project "magic" in the Class file structure, which is of the u4 type (4 bytes), so 0xCAFEBABE is its value. This data item is called "magic number ", it is used to determine whether the file is a Class file that can be received by the virtual machine. The virtual machine uses the magic number instead of the extension mechanism to determine the security of the Class file, because the extension can be modified at will. You can view all the qualified Class files. The magic number is "0 xCAFEBABE ".

The second and third data items in the Class file structure are: "minor_version" and "major_version", which respectively indicate the secondary version and major version information of the Class file. Their types are u2 (2 bytes), so 0x0000 indicates the minor version, and 0x0032 indicates the major version. The version number of the Class file starts from 45. After JDK1.1, the main version number of each JDK major version is increased by 1. JDK 45.0 supports version ~ 45.65535 Class file, JDK 46.0 supports the version ~ 46.65535 and so on. Currently, the latest JDK1.7 supports 51.0 ~ 51.65535. Note that JDK of a higher version can be backward compatible with Class files of previous versions, but it cannot run Class files of later versions. 0x0032 is converted to 50 in decimal format. According to the above derivation, this Class file is a Class file that can be executed by JDK1.6 or later virtual machines (it can also be reflected that this Class file is compiled by the JDK1.6 compiler ).


Next analysis: constant pool (contant_pool) in the Class file)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.