“Inside the Java Virtual Machine - Chapter 8 The Linking Model - Resolution of CONSTANT_Fieldref_info Entries”的一个错误

JasonLaw:在Inside the Java Virtual Machine - Chapter 8 The Linking Model - Resolution of CONSTANT_Fieldref_info Entries中,它说:

If resolution to the CONSTANT_Class_info completes successfully, the virtual machine performs the field lookup process using these steps:

  1. The virtual machine checks the referenced type for a field of the specified name and type. If the virtual machine discovers such a field, that field is the result of the successful field lookup.
  2. Otherwise, the virtual machine checks any interfaces directly implemented or extended by the type, and recursively, any superinterfaces of interfaces directly implemented or extended by the type, for a field of the specified name and type. If the virtual machine discovers such a field, that field is the result of the successful field lookup.
  3. Otherwise, if the type has a direct superclass, the virtual machine checks the type's direct superclass, and recursively all the superclasses of the type, for a field of the specified name and type. If the virtual machine discovers such a field, that field is the result of the successful field lookup.
  4. Otherwise, field lookup fails.

关于第 3 点,它说“if the type has a direct superclass, the virtual machine checks the type's direct superclass, and recursively all the superclasses of the type, for a field of the specified name and type”,这里遗漏了“对 superclass 的 superinterfaces 的检查”。

下面的例子可以证明,JVM 会检查 superclass 的 superinterfaces 。

import java.lang.Math;

public class Test {
    public static void main(String[] args) {
        System.out.println(A.i);
    }
}

class A extends AParent {
}

class AParent implements B {
}

interface B {
    int i = (int) (Math.random() * 100);
}

javac Test.java之后,javap -c Test.class的输出如下:

Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: getstatic     #3                  // Field A.i:I
       6: invokevirtual #4                  // Method java/io/PrintStream.println:(I)V
       9: return
}

运行java Test没有问题,可以看出 JVM 解析下标为 3 的 constant pool entry 时,最后会找到class A,通过class A会找到class AParent最后会找class AParent的 superinterface interface B

关于“Inside the Java Virtual Machine - Chapter 7 The Lifetime of a Type - Initialization”的疑问

JasonLaw:在Inside the Java Virtual Machine - Chapter 7 The Lifetime of a Type - Initialization中,有这么一段: A use of a non-constant static field is an active use of only the class or int…

Java-向Servlet动态添加URL模式 - java

是否可以在运行时动态地将URL模式添加到Servlet?例如,当Servlet启动时,扫描文件夹中的注释,然后将这些URL模式注入到Servlet中?提供更多清晰度-在Servlet的init文件中,我要执行此操作(伪代码)// scan all the files in the package my.project.services // find all…

执行shell命令时永久挂起(Java) - java

令人讨厌的代码块如下。该代码几乎总是可以工作,但有时会永远挂起。该应用程序是一个EJB计时器bean。实际上,它只挂了一次,我无法复制它。它的工作已经有将近两年没有任何问题。但是,在测试应用程序的更新版本时,计时器仅在运行几天后冻结,而从未从上次运行时释放数据库锁。日志清楚地表明它冻结在下面的代码块中的某个位置。它正在运行的命令是“ chmod”。publi…

如何检索字符串的特定部分 - java

我有一个目录以字符串形式列出,我想检索字符串的特定部分,唯一的是,由于这是一个目录,它的长度可以更改我想从字符串中检索文件名"C:\projects\Compiler\Compiler\src\JUnit\ExampleTest.java" "C:\projects\ExampleTest.java" 因此,在这两种情…

Java:增加YoungGen大小以提高GC性能 - java

我正在阅读以下文章:http://java.sun.com/docs/hotspot/gc1.4.2/example.html,无法理解以下几行:Young generation size is too small The young generation heap size in this first example is about 4 Mbytes w…