In-depth understanding of the Java Class file format (6)

Source: Internet
Author: User
Tags field table


After the previous articles, I finally finished introducing the constant pool. The reason why I spent so much time introducing the constant pool is that for understanding the format of class files, the constant pool must be understood, because a large number of data items in the constant pool are referenced elsewhere in the class file. For readers who do not know about the constant pool, if they want to learn more about the class file format, or want to continue reading this blog and post-column blog, so I suggest you read the previous blogs and familiarize yourself with the structure of the constant pool, which is helpful for understanding the subsequent content.


Although the constant pool has been introduced, there is still a lot of content in the class file under the constant pool. Next, we will analyze the content in the class file under the constant pool. Don't worry, as long as you understand the constant pool, this content will be easy to understand.


Before entering the body, the overall format of the class file is provided again here. This table has appeared in this article for a deep understanding of the Java Class file format (1. The reason for listing this table is to give the reader a general overview of the class file. The table content is as follows:

Type Name Quantity
U4 Magic 1
U2 Minor_version 1
U2 Major_version 1
U2 Constant_pool_count 1
Cp_info Constant_pool Constant_pool_count-1
U2 Access_flags 1
U2 This_class 1
U2 Super_class 1
U2 Interfaces_count 1
U2 Interfaces Interfaces_count
U2 Fields_count 1
Field_info Fields Fields_count
U2 Methods_count 1
Method_info Methods Methods_count
U2 Attribute_count 1
Attribute_info Attributes Attributes_count


Next we will introduce other content in the class file.



Access Flag Information in the class file

The table above shows that the two bytes under the constant pool are access_flags. Access_flags describes the access modifiers of the current class (or interface), such as public and private. In addition, there is a flag, indicates whether the current class describes the class or interface. The information of access_flags is relatively simple. The information of each flag in access_flags is listed below. I wrote this series of blogs for reference in "deep into java Virtual Machine", but this book is old and I have not explained the new features after java 5, five flag values are listed in this book, and the latest JVM specification is for java 7, with three additional flags added. They are ACC_SYNTHETIC, ACC_ANNOTATION, and ACC_ENUM.

Flag name
Flag Value
Logo meaning
Target Image
ACC_PUBLIC
Zero x 0001
Public type All Types
ACC_FINAL
Zero x 0010
Final type Class
ACC_SUPER
Zero x 0020
Use the new invokespecial Semantics Class and Interface
ACC_INTERFACE
Zero x 0200
Interface Type Interface
ACC_ABSTRACT
Zero x 0400
Abstract type Class and Interface
ACC_SYNTHETIC
Zero x 1000 This class is not generated by user code All Types
ACC_ANNOTATION
Zero x 2000 Annotation type Annotation
ACC_ENUM
Zero x 4000 Enumeration type Enumeration


Other logos are not described. These logos are simple. Readers may feel unfamiliar with the ACC_SUPER mark. The reader will think that the type cannot be modified by the super keyword. What is the ACC_SUPER? As can be seen in the table, the meaning is: use the new invokespecial semantics. Invokespecial is a bytecode command used to call a method. Generally, this bytecode command is used to call a constructor or display the method of calling the parent class using the super keyword. This is the origin of the name ACC_SUPER. Before java 1.2, invokespecial binds Methods statically, And the ACC_SUPER flag is added to the class file during java 1.2, it adds the dynamic binding function for the invokespecial command. There may be several concepts that readers do not quite understand, such as static binding and dynamic binding. These concepts will be detailed in future blogs.
It also needs to be noted that, since access_flags appears at the class level in the class file, it can only describe the type modifier, but cannot describe the field or method modifier, it is hoped that the access_flags here will not be confused with the access modifier in the method table and field table to be introduced later.
In addition, in Java 5, new features such as introduction and annotation and enumeration can be inferred that ACC_ANNOTATION and ACC_ENUM are added to Java 5. Although the class file remains consistent in general, it is not static and will change with the upgrade of the Java version. But in general, the format of the class file is relatively stable, there are not many changes.


This_class in the class file
The two bytes under the access flag access_flags are called this_class, which is the description of the current class. Its two bytes of data are an index of a CONSTANT_Class_info data item in the constant pool. CONSTANT_Class_info has already been introduced in the previous article. CONSTANT_Class_info has a field named name_index pointing to a CONSTANT_Utf8_info, which stores the full qualified name of the current class in this CONSTANT_Utf8_info.
If the current class is Person:
package com.jg.zhang;public class Person {int age;int getAge(){return age;}}

After decompiling Person. class, you can see the following two items in the constant pool:
  Constant pool:   #1 = Class              #2             //  com/jg/zhang/Person   #2 = Utf8               com/jg/zhang/Person   ..................


These two items are the information of the current class. The CONSTANT_Class_info with an index of 1 will be referenced by this_class in the class file. The following is an example (the area of the constant pool is shown in the dotted range ):


Super_class in the class file

Super_class follows this_class. Like this_class, it is an index pointing to data items in the constant pool. It points to a CONSTANT_Class_info. The CONSTANT_Class_info data item describes the information of the superclasses of the current class. Name_index in CONSTANT_Class_info points to a CONSTANT_Utf8_info in the constant pool, and CONSTANT_Utf8_info stores the fully qualified names of the superclasses of the current class. If one class is not explicitly inherited, that is, if the current class directly inherits the Object, the super_class value is 0. As mentioned in the previous article, if an index value is 0, it means that this index does not reference any data items in the constant pool, because the data items in the constant pool start from 1. That is to say, if the super_class in the class file of a class is 0, it means that the class directly inherits the Object class.
The following code is used to describe:
package com.jg.zhang;public class Programer extends Person{Computer computer;public Programer(Computer computer){this.computer = computer;}public void doWork(){computer.calculate();}}

The preceding Programer class inherits from the Person class. The following information exists in the constant pool of The Decompilation Programer. class:
Constant pool:..................   #3 = Class              #4             //  com/jg/zhang/Person   #4 = Utf8               com/jg/zhang/Person

These two items are the information of the parent class of the current class. The CONSTANT_Class_info with the index of 3 will be referenced by the super_class in the class file. The following is an example (the area of the constant pool is shown in the dotted range ):



Interfaces_count and interfaces in the class file

Next, super_class is interfaces_count, which indicates the number of interfaces implemented by the current class or the number of superinterfaces inherited by the current interface. Note: Only interfaces directly implemented by the current class are counted. If the current class inherits from another class, and the other class implements another interface, this interface is not counted in the interfaces_count of the current class. Interfaces_count is followed by interfaces. It can be considered as an array. Each array item is an index pointing to a CONSTANT_Class_info in the constant pool. This CONSTANT_Class_info references a CONSTANT_Utf8_info in the constant pool, this CONSTANT_Utf8_info stores the fully qualified names of interfaces directly implemented or inherited by the current type. The current type implements or inherits Several interfaces. There are several items in the interfaces array that correspond to each other.
The following shows the sample code:
package com.jg.zhang;public class Plane implements IFlyable, Cloneable{@Overridepublic void fly() {}}

The Plane class implements a custom IFlyable interface and a Cloneable interface in JDK. The constant pool contains the following information:
Constant pool: ..................   #5 = Class              #6             //  com/jg/zhang/IFlyable   #6 = Utf8               com/jg/zhang/IFlyable   #7 = Class              #8             //  java/lang/Cloneable   #8 = Utf8               java/lang/Cloneable   ..................


The four data items are the information of the interfaces implemented by the current Plane class. The fifth and sixth items describe the IFlyable interface implemented by Plane, and the seventh and eighth items describe the interface Cloneable interface implemented by Plane. The following figure shows the region of the constant pool in the dotted range ):




Summary
In this blog, I continue to explain the following parts of the constant pool in the class file. This section describes this_class, super_class, interfaces_count, and interfaces. These three data items respectively describe the current class (that is, the class where the current class file is located), The superclass inherited by the current class, and the interfaces implemented by the current class (if the current class file represents an interface, interfaces_count AND interfaces describe the superinterfaces inherited by the current interface ).
These data items hold indexes pointing to the constant pool. The actual information is stored in the constant pool, but the information in the constant pool will be referenced by this_class, super_class, interfaces_count and interfaces.
Through this blog, we can know how the current class in the source file, the superclasses of the current class, And the superinterfaces of the current class are described in the class file. In the next blog, we will explain how to describe the fields defined in the source file and the declared methods 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.