JDK8中JVM对类的初始化探讨

时间:2023-03-08 19:07:59
JDK8中JVM对类的初始化探讨

在《深入理解Java虚拟机》(第二版,周志明著)中,作者介绍了JVM必须初始化类(或接口)的五种情况,但是是针对JDK7而言的。

那么,在JDK8中,这几种情况有没有变化呢?(我猜测应该会有扩展)

接下来我们探讨一下JDK8中JVM类的初始化这一部分内容。

官方文档为The Java® Virtual Machine Specification, Java SE 8 Edition, 2015-02-13

类的初始化部分在https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-5.html#jvms-5.5。

我们先看下《深入理解Java虚拟机》(第二版,周志明著)中对类初始化的介绍:

JDK8中JVM对类的初始化探讨

有且只有五种情况下,当发现相关的类没有进行过初始化,虚拟机会触发其初始化:

(1) 调用new, getstatic, putstatic, invokestatic这四条指令时,相关类没有初始化;

(2) 使用java.lang.reflect包进行反射时,相关类没有初始化;

(3) 初始化子类时,发现父类没有初始化;

(4) 主类(包含maini()方法的类)没有初始化;

(5) JDK7中使用动态语言支持时,如果一个java.lang.invoke.MethodHandle实例正好是对REF_getStatic, REF_putStatic, REF_invokeStatic进行方法句柄解析的结果时;

The Java® Virtual Machine Specification, Java SE 8 Edition中对类或接口进行初始化的内容:

Initialization of a class or interface consists of executing its class or interface initialization method (§2.9).

A class or interface C may be initialized only as a result of:

  • The execution of any one of the Java Virtual Machine instructions newgetstaticputstatic, or invokestatic that references C (§new§getstatic§putstatic§invokestatic). These instructions reference a class or interface directly or indirectly through either a field reference or a method reference.

    Upon execution of a new instruction, the referenced class is initialized if it has not been initialized already.

    Upon execution of a getstaticputstatic, or invokestatic instruction, the class or interface that declared the resolved field or method is initialized if it has not been initialized already.

  • The first invocation of a java.lang.invoke.MethodHandle instance which was the result of method handle resolution (§5.4.3.5) for a method handle of kind 2 (REF_getStatic), 4 (REF_putStatic), 6 (REF_invokeStatic), or 8 (REF_newInvokeSpecial).

    This implies that the class of a bootstrap method is initialized when the bootstrap method is invoked for an invokedynamic instruction (§invokedynamic), as part of the continuing resolution of the call site specifier.

  • Invocation of certain reflective methods in the class library (§2.12), for example, in class Class or in package java.lang.reflect.

  • If C is a class, the initialization of one of its subclasses.

  • If C is an interface that declares a non-abstract, non-static method, the initialization of a class that implements C directly or indirectly.

  • If C is a class, its designation as the initial class at Java Virtual Machine startup (§5.2).

从这段可以看到大部分和《深入理解Java虚拟机》(第二版,周志明著)中描述的相同,除了两点:

(1) 使用动态语言支持时,如果一个java.lang.invoke.MethodHandle 实例是REF_newInvokeSpecial的方法句柄解析结果时,也会触发类的初始化;

(2) 对于接口而言,如果这个接口申明了一个non-abstract, not-static方法,当实现了这个接口的类初始化时也会触发这个接口的初始化。

在初始化前,类/接口必须先经过连接阶段:验证,准备,解析(可选)。

Prior to initialization, a class or interface must be linked, that is, verified, prepared, and optionally resolved.

虚拟机在初始化类/接口时,要注意两个问题:

(1)因为虚拟机是多线程的,要求处理同步问题;

(2)递归初始化的问题。

由虚拟机的具体实现来负责处理以上两个问题。JDK8中使用了一个procedure来完成初始化。

当然这个procedure需要一些条件的满足,比如相关的Class object必须已经完成了验证和准备,并且必须是四种状态中的一种。

详情可以查看文档,有空再探讨。