Tag: Method Gravity not note tar another abstract class method table AST
Reprint: http://blog.csdn.net/zhangjg_blog/article/details/21888943
After the first few articles, finally introduced the Chang, the reason is to spend so much effort to introduce the constant pool, because for understanding the class file format, constant pool is necessary to understand, because the class file in other places, a large number of reference to the constant pool of data items. For those who do not know the constant pool, if you want to learn more about the class file format, or want to continue reading this blog and later in this column of the blog, then I suggest that I first read some of the previous blog, the structure of the constant pool familiar, it is helpful to understand the later content.
Although the constant pool is covered, there is much more to the class file under the constant pool. Next, we analyze the contents of the class file below the constant pool, and don't worry, it's easy to understand as long as the constant pool is clear.
Before you begin entering the body, give the overall format of the class file here again. This form has appeared in a deep understanding of the Java class file format (a) in this article. The reason this table is listed here is to give the reader a general overview of the class file again. The contents of the table are as follows:
| Type |
Name |
Number |
| 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 |
Let's start by introducing the rest of the class file.
access flag information in the class file As you can see from the table above, the 2 bytes below the constant pool are access_flags. Access_flags describes the access modifiers of the current class (or interface), such as public, private, etc., in addition, there is a flag bit, which flags the current amount This class describes the classes, or interfaces. Access_flags information is relatively simple, and the information for each flag bit in Access_flags is listed below. Originally wrote this series of blog reference is "deep Java Virtual machine", but this book is older, about the new features of Java 5 is not explained, the book contains 5 flag values, and the latest JVM specification is for Java 7, which added an additional three flag bits. They were acc_synthetic, acc_annotation and Acc_enum.
| Flag Name |
Flag value |
Logo meaning |
Against the image of the |
| Acc_public |
0x0001 |
Public type |
All types |
| Acc_final |
0x0010 |
Final type |
Class |
| Acc_super |
0x0020 |
Using the new invokespecial semantics |
Classes and Interfaces |
| Acc_interface |
0x0200 |
Interface type |
Interface |
| Acc_abstract |
0x0400 |
Abstract type |
Classes and Interfaces |
| Acc_synthetic |
0x1000 |
The class is not generated by user code |
All types |
| Acc_annotation |
0x2000 |
Annotation type |
Annotations |
| Acc_enum |
0x4000 |
Enum type |
Enumeration |
Other signs are not introduced, these signs are very simple. It may be acc_super that the reader feels more unfamiliar. The reader will think that the type cannot be modified by the Super keyword, so what does this acc_super do? As you can see in the table, it means: Use the new invokespecial semantics. Invokespecial is a bytecode directive that is used to invoke a method that, in general, is used when calling a constructor method or using the Super keyword to display a method that invokes the parent class. This is the origin of the name Acc_super. Prior to Java 1.2, Invokespecial's call to the method was statically bound, while the Acc_super flag was added to the class file in Java 1.2, which added dynamic binding functionality to the Invokespecial directive. There may be several concepts that the reader is not quite aware of, such as static bindings, dynamic bindings, and so on, which are described in detail later in the blog. It is also important to note that since Access_flags appears at the class level of the classes file, it can only describe the modifier of the type, not the modifier of the field or method, and I hope the reader will not place the Access_flags is confused with the access modifiers in the Method table and field table that you want to describe later. In addition, in Java 5, the introduction and new features of annotations and enumerations, it can be speculated that acc_annotation and Acc_enum are added in the Java 5 version. Although the class file is generally consistent, but he is not static, but also with the Java version of the promotion and change, but overall, the class file format is relatively stable, the change is not a lot of places.
This_class in the class file
Access Flag access_flags The following two bytes are called This_class, which is a description of the current class. Its two-byte data is an index to a Constant_class_info data item in a constant pool. Constant_class_info has been introduced in the above article. There is a field in Constant_class_info called Name_index, which points to a constant_utf8_info that holds the fully 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 Person.class is deserialized, 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 information for the current class. The Constant_class_info with index 1 will be referenced by This_class in the Class file. Here is an example diagram (where the range of constant pools is represented within a dashed line): Super_class in the class file Super_class immediately after the This_class. Like This_class, it is an index that points to a constant pool data item. It points to a constant_class_info, the Constant_class_info data item that describes the superclass information of the current class. The Name_index in Constant_class_info points to a constant_utf8_info in the constant pool, where Constant_utf8_info holds the fully qualified name of the superclass of the current class. If there is no explicit inheritance of one, that is, if the current class inherits directly from object, then the Super_class value is 0. As we mentioned in the previous article, if an index value is 0, it means that the index does not reference data items in any constant pool, because the data items in the constant pool start at 1. That is, if Super_class is 0 in a class file, it means that the class inherits the object class directly. This is illustrated by the following code:
- Package Com.jg.zhang;
- Public class Programer extends person{
- Computer computer;
- Public programer (computer computer) {
- this.computer = computer;
- }
- public void DoWork () {
- Computer.calculate ();
- }
- }
the Programer class above inherits from the person class. Then decompile Programer. class, which has the following information in its constant pool:
- 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. Constant_class_info with index 3 will be referenced by Super_class in the Class file. Here is an example diagram (where the range of constant pools is represented within a dashed line): Interfaces_count and interfaces in the class file the Super_class is followed by Interfaces_count, which represents the number of interfaces implemented by the current class or the number of hyper-interfaces inherited by the current interface. Note that only interfaces that are implemented directly by the current class are counted, and if the current class inherits another class and another class implements an interface, the interface is not counted in the interfaces_count of the current class. After Interfaces_count is interfaces, he can be seen as an array in which each array item is an index, pointing to a constant_class_info in the constant pool, which constant_class_ Info will also refer to a constant_utf8_info in the constant pool, which holds the fully qualified name of the interface with the current type directly implemented or inherited. The current type implements or inherits several interfaces, and in the interfaces array there are several items corresponding to them. Here's a sample code:
- Package Com.jg.zhang;
- Public class Plane implements Iflyable, cloneable{
- @Override
- public void Fly () {
- }
- }
the plane class implements a custom Iflyable interface and implements a cloneable interface in a JDK, and its constant pool has 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
- .........
- .........
these four items of data are information about the interfaces implemented by the current plane class. Items Fifth and sixth describe the Iflyable interfaces implemented by plane, and seventh and eighth describe the interface cloneable interfaces implemented by plane. The following is (the area within the dashed range that represents the constant pool):
Summary in this blog, we continue to explain the sections below the constant pool in the class file. The main explanations are three parts, namely This_class, Super_class, Interfaces_count and interfaces. These three data items describe the current class (the class where the current class file resides), the superclass that the current class inherits, and the interface that the current class implements (if the current class file represents an interface, then Interfaces_ Count and interfaces describe the hyper-interfaces inherited by the current interface). These data items hold indexes that point to a constant pool. The real information is stored in a constant pool, except that the information in the constant pool is referenced by This_class, Super_class, Interfaces_count, and interfaces. Through this blog we can know the current class in the source file, the superclass of the current class and how the current class's hyper-interface is described in the class file. In the next blog post, you'll explain the fields defined in the source file, and how the declared method is described in the class file.
(6) In-depth understanding of Java class file format (vi)