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

Source: Internet
Author: User
Tags types of tables

 

JVM learning notes directory:

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

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

 

Constant pool

The previous blog article introduced the "magic number" and "primary and secondary version number" of the Class file. The entry of the constant pool data item is followed by the "primary and secondary version number" data item. The constant pool of the Class file is the data type most associated with other projects in the Class file structure. It is also one of the Data projects that occupy the largest Class file space, it is also the first table-type data project in the Class file. In order to explain and view the following Class file structure table and instance Class file:

EXAMPLE Class:

package com.beliefbetrayal.clazz;

public class ClassFileTest {

private int m;

public int getM() {
return m;
}

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

Whether it is an unsigned number or a 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. Because the constants in the constant pool of the Class are not fixed, the entry of the constant pool requires a capacity counter "contant_pool_count" to record the number of constants in the constant pool, it is of the u2 (2 bytes) type. Use WinHex to open the Class file and obtain:

Figure 1 shows part of the Class file

The index of the capacity counter of the constant pool starts from 1 rather than from 0, when a zero index is left blank, the following data pointing to the index value of the constant pool needs to be expressed as "no reference to any constant" under special circumstances. Because the capacity counter of the volume pool is u2, its value is "0x0018" and is converted into a 10-digit number of 24, which indicates that there are 23 constant indexes in the constant pool as 1 ~ 23. To verify the accuracy, we can use the javap tool provided by JDK to analyze the section of the Class file using javap (only the section of the constant pool is listed ):

We can also see that the Class file does have 23 constants. The following describes the constant pool. The constant pool mainly stores two types of constants: Literal and Symbolic Reference ). The literal is generally a text string or a constant value declared as final. Symbol references are generally "Fully Qualified Name", "field Name and Descriptor", and "method Name and Descriptor ".

 

Every constant in the constant pool is a table type. Before JDK6.0, there are 11 types of table data in the structure. These 11 types of tables share a common feature, the first sign of the u1 type at the beginning of the table (tag, value: 1 ~ 12, sign 2 Vacancy), which indicates the current constant belongs to that constant type. The specific meaning of 11 constant types is shown in:

Look back at the figure "part of Information 1 in the Class file". The first constant has a flag of "0x07" and is converted to a 10-digit number of 7, you can view the "constant pool project type" table and find that the constant belongs to the CONSTANT_Class_info type. The constant of the secondary type represents the symbolic reference of a class or interface. The structure of CONSTANT_Class_info is as follows:

The tag flag has already explained that name_index is an index value that points to a constant of the CONSTANT_Utf8_info type in the constant pool. This constant represents the fully qualified name of this class (interface, view the figure "part of Information 1 of the Class file". The name_index value is "0x0002", which is converted to 2 in decimal order and points to the second constant in the constant pool. Continue to view the graph "part of Information 1 of the Class file" and find the flag of the second constant as "0x01" and convert it to the 10-digit number 1, check the "constant pool project type" table. It is indeed a constant of the CONSTANT_Utf8_info type. CONSTANT_Utf8_info type constant structure:

Length indicates the length of the UTF-8 encoded string, which is followed by the length of the UTF-8 encoded string. View the figure "part of the Class file information 1". The length value is "0x0026" and is converted to a decimal number of 38, it indicates a string that is followed by a length of 38 bytes. See figure 2 of "part of the Class file:

Figure 2 shows part of the Class file

On the right side of the image, we can see that the selected 38-byte length value is com/beliefbetrayal/clazz/ClassFileTest, which is the fully qualified name of our example class. After so many analyses, we can now view the constant pool information analyzed by the javap tool (view the constant pool information ), the message "constant #1 = class #2" indicates that the constant points to the #2 constant, "constant #2 = Asciz com/beliefbetrayal/clazz/ClassFileTest" indicates that the constant value is "com/beliefbetrayal/clazz/ClassFileTest". We can see that our analysis is correct..

According to part of "Class file information 2" figure, the third constant's flag is "0x07" converted to 10 hexadecimal 7, you can view the "constant pool project type" table and find that the constant belongs to the CONSTANT_Class_info type. The constant of the secondary type represents the symbolic reference of a class or interface. According to the "CONSTANT_Class_info structure chart", the name_index value is "0x0004" and is converted to 4 in decimal order, which indicates pointing to the fourth constant. According to part of the "Class file information 2" figure, the fourth constant is marked as "0x01" and converted to a 10-digit number of 1, view the "constant pool project type" table. We can see that it is a constant of the CONSTANT_Utf8_info type. According to the CONSTANT_Utf8_info type structure chart, the length value is "0x0010" and is converted to a hexadecimal value of 16, it indicates a string followed by 16 bytes, for example:

Figure 3 shows part of the Class file

View the right side of the image. The selected 16-byte length value is java/lang/Object. Now let's go back and view the constant pool information analyzed by the javap tool (view the constant pool information). We are very happy to see that our analysis has been verified by ^_^. Make persistent efforts to analyze the 5th constants, which are marked as "0x01" and converted to a 10-digit number of 1, view the "constant pool project type" table. We can see that it is a constant of the CONSTANT_Utf8_info type. According to the CONSTANT_Utf8_info type structure, the length value is "0x0001" and is converted to the 10-digit number 1, it indicates a string followed by 1 byte length, for example:

 

Figure 4 shows part of the Class file

On the right side of the image, we can see that the value of the selected 1-byte length is "m", which is the name of the member variable we have defined. So far, we have analyzed 5 of the 23 constants in the ClassFileTest constant pool, and other constants can be calculated using similar methods. After manual derivation, the information analyzed by the javap tool can be compared to verify the correctness of the derivation. The following table lists the 11 types of constants in the constant pool:

 

View the constant pool information analyzed by the javap tool (view the constant pool information). After careful observation, you will find that many constants appear inexplicably, such as "I" and "LineNumberTable, these automatically generated constants will be referenced by field tables, method tables, and attribute tables. They will be used to describe some content that is inconvenient to express using "fixed bytes.

The introduction to the constant pool is over. We don't need to manually calculate every constant. We need to master the method. JDK has provided us with a powerful Class file analysis tool javap.


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.