[JVM Parsing series] [six]class constant pools, access flags, class inheritance relationships, how do you describe a class in bytecode?) __jvm

Source: Internet
Author: User

Last time we talked about the magic number and the version number, we went on to say today. For the sake of convenience, I'll take that piece of Ventura over here for everyone to see.


By the figure is visible next is a call Constant_pool_count translation called Chang number, before we said class file only two kinds of data structure, unsigned number and table, and the entire article without separators, in the absence of separators in the case of how we distinguish between the number of the table of 1 of the dividing line. (For example: Constant_pool in the diagram, let's figure out how to mark the end of the Constant_pool.) Yes, his end position is all in front of the U2 type of constant_pool_count implementation, I open a class file everyone look.

I've crossed 43 out of here. Because the designer emptied the 0 and said the constant pool has a total [1,43] constant so it's 42. (only constant pool 0 vacancies, the rest starting from 0)

That means there are 42 Cp_info table types of data structures, so let's take a look at how cp_info is structured.

What we keep in a constant pool. Literal and symbolic references.

1, the literal quantity, Baidu Encyclopedia explains this:


There are several keywords, the source code, fixed value. Careful reading should be similar to the constants in Java. But the difference between them is. The literal is generally the right value, that is, int a = 0, and this 0 is the literal amount.

2, symbolic reference, generally refers to the class and interface fully qualified name, field name and descriptor, method name and descriptor. The name of the class and interface and the name and type of the field, the name of the method and the return value can be used to mark things.

Let's start with a smaller class and take a closer look at its structure (this class only implements a Main method, a very simple class, no other variables, static variables exist, and then parsing is this kind of)

First Class diagram:


Constant pool count and Chang for the class:


We can see that its Chang is 0x10 converted to decimal is 16 and then remove 0 digits is a total of 15 constants

We use Javap-verbose Xxxx.class to look at the structure of this class.


We can see that the first column has a lot of names similar to Class,utf, this is the CP_INFO table structure, this table structure has a special place, is the beginning will have a U1 sign bit. There are a total of 11 types, in the jdk1.7 after the new 3 is not the table. There are 11 types of these 11 types that we can divide into two kinds.

1. Reference type: The Class,methodref,nameandtype in the above picture and the FieldRef and Interfacemethodref

2. Literal quantity: Utf8,interger float long double string

Reference types mean that these types refer to other types, which is not good to understand. For example, look at the name of the class type he refers to #2,#2 is the UTF8 type that represents the qualified names of class.

About the 11 detailed structure of the network there are many, our class parsing is a preliminary analysis, I may add in the future, this time will not add. In fact, this is a very simple combination of JAVAP can fully understand, I believe that we can see more to understand.

Then, according to the topmost figure, the end of the constant pool is the access flag. Whether this class file is a class or an excuse, public or abstract, is it a final modifier. Similarly, we open a class file to see


The access flag bit 0x0021 is shown in the figure, so how do we look at its information? I'll give you a table to see:


Please call me a soul painter, which still remains the box I entered and forgot to point it out ...

So according to the figure above, the class we just profiled should occupy public and super two, so the value is 0x0021.

And take a look at the top of the picture. We should then say that This_class and Super_class also have an indefinite length of interfaces, and we have already talked about interfaces this structure in the constant pool. In fact, we analyze to now, the description of a class is also basically finished (the next section on the method), in addition to the inheritance of the class does not say, then we will be the relationship of the succession of tears to finish off him.

1, This_class as the name suggests, in fact, is the class's fully qualified name

2, Super_class, this is about Fred's fully qualified name.

3, interface, of course, that is, interface.

Come on, I'll show you the picture.

00

Accidentally cut a 00, my own typing, in fact, ThisClass is 01,superclass is 03, the interface does not inherit so count is 00. Let's take a look at where our constant values are indexed.


You can see that 01 is pointing to the test class, 03 point to the object class, as we predicted, the description of our class is finished, the 11 table structures in the constant pool are not spoken, but we usually use the JAVAP tool to not understand those table structure, then we have time to make up

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.