符号引用
我们用命令javap -v Math.class 获取可读的字节码信息,如下
我们类信息中的所有符号都放在了常量池中,如下:
...
public class com.suibibk.jvm.Math
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Class #2 // com/suibibk/jvm/Math
#2 = Utf8 com/suibibk/jvm/Math
#3 = Class #4 // java/lang/Object
#4 = Utf8 java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Utf8 Code
#8 = Methodref #3.#9 // java/lang/Object."<init>":()V
#9 = NameAndType #5:#6 // "<init>":()V
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lcom/suibibk/jvm/Math;
#14 = Utf8 compute
#15 = Utf8 ()I
#16 = Utf8 a
#17 = Utf8 I
#18 = Utf8 b
#19 = Utf8 c
#20 = Utf8 main
#21 = Utf8 ([Ljava/lang/String;)V
#22 = Methodref #1.#9 // com/suibibk/jvm/Math."<init>":()V
#23 = Methodref #1.#24 // com/suibibk/jvm/Math.compute:()I
#24 = NameAndType #14:#15 // compute:()I
#25 = Utf8 args
#26 = Utf8 [Ljava/lang/String;
#27 = Utf8 math
#28 = Utf8 SourceFile
#29 = Utf8 Math.java
{
public com.suibibk.jvm.Math();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/suibibk/jvm/Math;
public int compute();
descriptor: ()I
flags: ACC_PUBLIC
Code:
stack=2, locals=4, args_size=1
0: iconst_1
1: istore_1
2: iconst_2
3: istore_2
4: iload_1
5: iload_2
6: iadd
7: bipush 10
9: imul
10: istore_3
11: iload_3
12: ireturn
LineNumberTable:
line 5: 0
line 6: 2
line 7: 4
line 8: 11
LocalVariableTable:
Start Length Slot Name Signature
0 13 0 this Lcom/suibibk/jvm/Math;
2 11 1 a I
4 9 2 b I
11 2 3 c I
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=1
0: new #1 // class com/suibibk/jvm/Math
3: dup
4: invokespecial #22 // Method "<init>":()V
7: astore_1
8: aload_1
9: invokevirtual #23 // Method compute:()I
12: pop
13: return
LineNumberTable:
line 11: 0
line 12: 8
line 13: 13
LocalVariableTable:
Start Length Slot Name Signature
0 14 0 args [Ljava/lang/String;
8 6 1 math Lcom/suibibk/jvm/Math;
}
常量池中的就都是符号引用,那方法怎么找到。
符号引用转直接引用
我们再看一下我们的代码
public class Math {
private static Math math = new Math();
public int compute() {
int a = 1;
int b = 2;
int c = (a+b)*10;
return c;
}
public static void main(String[] args) {
Math math = new Math();
math.compute();
}
}
在JVM执行到map.compute()时,也就是对于上面的指令
9: invokevirtual #23 // Method compute:()I
时,怎么根据符号引用找到compute()的执行指令?
我们通过#23去常量池中找到对应的符号
#23 = Methodref #1.#24 // com/suibibk/jvm/Math.compute:()I
可以知道时方法引用,其实#23对应也有两个符号#1和#24,这两个符号分别如下
#1 = Class #2 // com/suibibk/jvm/Math
#24 = NameAndType #14:#15 // compute:()I
然后#24对应的时#14和#15
#14 = Utf8 compute
#15 = Utf8 ()I
所以#23对应的符号引用就是com/suibibk/jvm/Math.compute:()I
然后JVM会找到该符号引用对应的直接引用,放入栈帧的动态链接中。
静态链接
在类加载的解析阶段,会将符号引用替换为直接引用,该阶段会把一些
静态方法(符号引用比如main()方法)替换为指向数据所在内存的指针或句柄(直接引用),这就是所谓的
静态链接过程(在类的加载期间完成)
动态链接
动态链接就是指上面在程序运行期间把compute()方法的符号引用替换为直接引用的过程