gpt4 book ai didi

java - 为什么Java接口(interface)用javac -g编译出来的class文件里没有LocalVariableTable?

转载 作者:行者123 更新时间:2023-12-05 08:29:20 25 4
gpt4 key购买 nike

当Java文件为接口(interface)时,如TestInterface.java:

public interface TestInterface {

void method(String parameterName);

}

我用javac -g TestInterface.java编译,然后用javap -v TestInterface反汇编,输出如下:

Classfile /private/tmp/TestInterface.class
Last modified Mar 17, 2022; size 148 bytes
MD5 checksum da2f58afc0eaf77badc94c90de385198
Compiled from "TestInterface.java"
public interface TestInterface
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT
Constant pool:
#1 = Class #7 // TestInterface
#2 = Class #8 // java/lang/Object
#3 = Utf8 method
#4 = Utf8 (Ljava/lang/String;)V
#5 = Utf8 SourceFile
#6 = Utf8 TestInterface.java
#7 = Utf8 TestInterface
#8 = Utf8 java/lang/Object
{
public abstract void method(java.lang.String);
descriptor: (Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_ABSTRACT
}
SourceFile: "TestInterface.java"

而当Java文件是类时,比如TestClass.java:

public class TestClass {

public void method(String parameterName) {

}

}

javac -g TestClass.java编译,然后用javap -v TestClass反汇编,输出结果如下:

Classfile /private/tmp/TestClass.class
Last modified Mar 17, 2022; size 389 bytes
MD5 checksum 8e124ecce6632ad6e1a5bb45888a3168
Compiled from "TestClass.java"
public class TestClass
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #3.#17 // java/lang/Object."<init>":()V
#2 = Class #18 // TestClass
#3 = Class #19 // java/lang/Object
#4 = Utf8 <init>
#5 = Utf8 ()V
#6 = Utf8 Code
#7 = Utf8 LineNumberTable
#8 = Utf8 LocalVariableTable
#9 = Utf8 this
#10 = Utf8 LTestClass;
#11 = Utf8 method
#12 = Utf8 (Ljava/lang/String;)V
#13 = Utf8 parameterName
#14 = Utf8 Ljava/lang/String;
#15 = Utf8 SourceFile
#16 = Utf8 TestClass.java
#17 = NameAndType #4:#5 // "<init>":()V
#18 = Utf8 TestClass
#19 = Utf8 java/lang/Object
{
public TestClass();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this LTestClass;

public void method(java.lang.String);
descriptor: (Ljava/lang/String;)V
flags: ACC_PUBLIC
Code:
stack=0, locals=2, args_size=2
0: return
LineNumberTable:
line 5: 0
LocalVariableTable:
Start Length Slot Name Signature
0 1 0 this LTestClass;
0 1 1 parameterName Ljava/lang/String;
}
SourceFile: "TestClass.java"

为什么接口(interface)的类文件中没有包含LocalVariableTable

JDK 版本:1.8.0_311

最佳答案

因为这不是类文件结构的工作方式。

根据JVM spec , LocalVariableTableCode 属性的一部分:

The LocalVariableTable attribute is an optional variable-length attribute in the attributes table of a Code attribute (§4.7.3). It may be used by debuggers to determine the value of a given local variable during the execution of a method.

但是,接口(interface)方法是抽象的(即具有标志 ACC_ABSTRACT),因此它们首先不能具有 Code 属性(§4.7.3)。

The Code attribute is a variable-length attribute in the attributes table of a method_info structure (§4.6). A Code attribute contains the Java Virtual Machine instructions and auxiliary information for a method [...]

If the method is either native or abstract, and is not a class or interface initialization method, then its method_info structure must not have a Code attribute in its attributes table.

因此 javac 不可能为您的抽象接口(interface)方法生成 LocalVariableTable,即使它想要生成,因为那样不会生成正确的类文件。

另一种思考方法是,在您具体实现它之前,方法的参数实际上并不“存在”。毕竟,抽象方法应该只是一个没有“实现”的“要求”。我真的不明白为什么您会期望有局部变量,因为它们是实现细节。

关于java - 为什么Java接口(interface)用javac -g编译出来的class文件里没有LocalVariableTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71515303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com