JVM (C): In-depth analysis of Java bytecode - on
Bytecode article is divided into two parts, Part I is the meaning of existence This paper describes the class files, and the benefits it brings. And to analyze one of its internal constitution --- byte code, and from the next set of instructions aspects, to explain what are the instruction set, and the meaning of their respective representatives. To sum up inevitability of Class files.
significance
He said before the Java virtual machine has a platform-independent, but in fact, JVM language-independent and more reflects it. Performance is the current more and more languages can be run on the JVM, and the logic behind this is that these languages will be compiled into Class file, and then run on the JVM.
In the future,we will consider bounded extensions to the java virtual machine to provide better support for other languages.
Above this is the vision proposed by the JVM relevant personnel, we are more optimistic about the development of the Java language.
Then in the following article, we are going to explore part of the Class document, understand how their internal organization.
Class class file
First we write an original Java source code:
public class TestForEach extends Thread{
private static int ccc = 1;
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = a + b;
}
Here we use the tools provided by JDk compile the code, obtain the following binary stream.
cafe babe 0000 0034 001d 0a00 0600 0f09
0010 0011 0800 120a 0013 0014 0700 1507
0016 0100 063c 696e 6974 3e01 0003 2829
5601 0004 436f 6465 0100 0f4c 696e 654e
756d 6265 7254 6162 6c65 0100 046d 6169
6e01 0016 285b 4c6a 6176 612f 6c61 6e67
2f53 7472 696e 673b 2956 0100 0a53 6f75
.........
For us, this is the need to analyze the document. The first four bytes of the binary stream cafe babe
, which is called the magic number. It represents a .class This is the type of file, followed by the fifth and sixth byte is the minor version number, seventh and eighth byte is the major version number, and we have compiled this version is JDK1.8 under. Then followed by that constant pool, access to labeling information, because this information is calculated very cumbersome trouble, where will not start to calculate, while the official also carefully provided javap tools to decompile to see its form of organization.
Bytecode files
First, we use javap tools to decompile javap -verbose TestForEach
, obtain the following documents
Last modified 2019-5-22; size 461 bytes
MD5 checksum 2602dfd883d5d5e417e26ce2d42b916d
Compiled from "TestForEach.java"
public class TestForEach extends java.lang.Thread
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #6.#22 // java/lang/Thread."<init>":()V
#2 = Fieldref #5.#23 // TestForEach.d:I
#3 = Fieldref #5.#24 // TestForEach.dfinal:I
#4 = Fieldref #5.#25 // TestForEach.ccc:I
#5 = Class #26 // TestForEach
#6 = Class #27 // java/lang/Thread
#7 = Utf8 ccc
#8 = Utf8 I
#9 = Utf8 d
#10 = Utf8 dfinal
#11 = Utf8 ConstantValue
#12 = Integer 2222
#13 = Utf8 <init>
#14 = Utf8 ()V
#15 = Utf8 Code
#16 = Utf8 LineNumberTable
#17 = Utf8 main
#18 = Utf8 ([Ljava/lang/String;)V
#19 = Utf8 <clinit>
#20 = Utf8 SourceFile
#21 = Utf8 TestForEach.java
#22 = NameAndType #13:#14 // "<init>":()V
#23 = NameAndType #9:#8 // d:I
#24 = NameAndType #10:#8 // dfinal:I
#25 = NameAndType #7:#8 // ccc:I
#26 = Utf8 TestForEach
#27 = Utf8 java/lang/Thread
{
public TestForEach();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Thread."<init>":()V
4: aload_0
5: iconst_1
6: putfield #2 // Field d:I
9: aload_0
10: sipush 2222
13: putfield #3 // Field dfinal:I
16: return
LineNumberTable:
line 11: 0
line 17: 4
line 18: 9
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=4, args_size=1
0: iconst_2
1: istore_2
2: iconst_1
3: iload_2
4: iadd
5: istore_3
6: return
LineNumberTable:
line 22: 0
line 23: 2
line 111: 6
static {};
descriptor: ()V
flags: ACC_STATIC
Code:
stack=1, locals=0, args_size=0
0: iconst_1
1: putstatic #4 // Field ccc:I
4: return
LineNumberTable:
line 16: 0
}
SourceFile: "TestForEach.java"
Header
We began to read from scratch, starting with
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Minor version and major version number here were marked Class files and access, in front of the version number has been said here is not to go into details, as the access flags, which represents some of the class or interface to access information, such as whether it is public type, have not been declared as final, and so on.
Constant pool
Next to the ConstantPool constant pool , which can be simply understood as a resource warehouse Class files, many parts have been associated with it.
Wherein storing the literal constants and two types of reference symbols. It can be understood as literal text strings and a final statement to a constant value and so on. Symbol quotes contains the following:
- Classes and interfaces the fully qualified name;
- Name and descriptor fields;
- Descriptor and a method name.
In the above two types of the constant pool is stored in a table, the detailed meanings shown below:
Meaning constant pool for each table and class files we get in this analysis several authors look:
- Methodref: reference symbol method, and the specific content skip 6, line 22, can be seen but the back annotation is a configuration of the parent class Thread
- Fieldref: reference symbol field, the specific content in row 5, 23, can be seen in
TestForEach
the definition of a property of type int class d - Class: Class or interface reference symbol can be seen from the file is a
TestForEach
class that inherits fromThread
- Utf8: indicating that a string field described in the document
ccc,d,dfinal
, the fully qualified name ofjava/lang/Thread
the like, the method describedmain,()V
belong to this category - NameAndType: reference symbol field or method, different from the above is that it does not declare a class which
- .....
More detailed here will not start, but there are constant pool contents not mentioned above, here we go into detail about.
Property sheet collection
Table set of attributes used to describe certain scenes and some secondary information. Attribute table for the above occur, we say down here in detail.
- ConstantValue : Final keyword to define constant values;
- Code : Code compiled from the Java bytecode instruction, meaning elaborate specific instructions in the next;
- LineNumberTable : line number and the Java source code bytecode instructions corresponding relationship;
- SourceFile name of the source file:
- .........
to sum up
This article stresses the important role of Class platform-independent file and reach in the Java language independence played, and describes the Class file ---- an important part of the document identification, constant pool, a set of attributes and so on. In addition, the contents of the file to identify and constant pool has been expanded specific description.
Next article from the JVM instruction set supported content began to introduce, and to instruct the Class file paper set collection as an example to describe the contents of the instruction set.
Article in a public number "iceWang" first-hand update, interested friends can focus the public number, the first time I see the point of knowledge sharing, thank you! Refills.
This series of articles is mainly borrowed from the "in-depth technical analysis JavaWeb insider" and "in-depth understanding of the Java Virtual Machine -JVM advanced features and best practices."