为什么 invokevirtual 的目标类型从 javac target 1.1 更改为 1.2?
Why did the target type for invokevirtual change from javac target 1.1 to 1.2?
我知道这些是古老的 Java 版本,但我仍然很好奇。给定以下代码段:
public void test(java.awt.event.MouseEvent e)
{
System.out.println(e.getID());
}
使用 javac -source 1.3 -target 1.1
编译时生成以下内容:
public void test(java.awt.event.MouseEvent);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: aload_1
4: invokevirtual #3 // Method java/awt/AWTEvent.getID:()I
7: invokevirtual #4 // Method java/io/PrintStream.println:(I)V
10: return
使用 javac -source 1.3 -target 1.2
编译时生成以下内容:
public void test(java.awt.event.MouseEvent);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: aload_1
4: invokevirtual #3 // Method java/awt/event/MouseEvent.getID:()I
7: invokevirtual #4 // Method java/io/PrintStream.println:(I)V
10: return
为什么 invokevirtual 的目标类型(参见第 4 行)从目标 1.1 更改为目标 1.2?
经过一些研究,似乎从 Java 1.2 开始在 javac 中引入了此更改,以符合 JLS 二进制兼容性规则。以下引用直接来自 the javac source code:
Beginning with -target 1.2 we obey the JLS rules for binary
compatibility, emitting as the qualifying type of a reference to a
method or field the type of the qualifier. In earlier targets we use
as the qualifying type the class in which the member was found.
我知道这些是古老的 Java 版本,但我仍然很好奇。给定以下代码段:
public void test(java.awt.event.MouseEvent e)
{
System.out.println(e.getID());
}
使用 javac -source 1.3 -target 1.1
编译时生成以下内容:
public void test(java.awt.event.MouseEvent);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: aload_1
4: invokevirtual #3 // Method java/awt/AWTEvent.getID:()I
7: invokevirtual #4 // Method java/io/PrintStream.println:(I)V
10: return
使用 javac -source 1.3 -target 1.2
编译时生成以下内容:
public void test(java.awt.event.MouseEvent);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: aload_1
4: invokevirtual #3 // Method java/awt/event/MouseEvent.getID:()I
7: invokevirtual #4 // Method java/io/PrintStream.println:(I)V
10: return
为什么 invokevirtual 的目标类型(参见第 4 行)从目标 1.1 更改为目标 1.2?
经过一些研究,似乎从 Java 1.2 开始在 javac 中引入了此更改,以符合 JLS 二进制兼容性规则。以下引用直接来自 the javac source code:
Beginning with -target 1.2 we obey the JLS rules for binary compatibility, emitting as the qualifying type of a reference to a method or field the type of the qualifier. In earlier targets we use as the qualifying type the class in which the member was found.