JVMS(third edition) chapter 5 Loading, Linking ,and Initializing 类和接口的加载,链接和初始化 笔记

Loading is the process of finding the binary representation of a class or interface
type with a particular name and creating a class or interface from that binary
representation.

Linking is the process of taking a class or interface and combining it into the runtime state of the Java virtual machine so that it can be executed.

Initialization of a class or interface consists of executing the class or interface initialization method <clinit> (§3.9).

 

Runtime Constant Pool :

一个class文件里的constant_pool 表用来构造runtime constant pool。所有runtime constant pool的引用都被初始化 为符号引用。

类或接口的符号引用 从 CONSTANT_Class_info获得(类名的格式和Class.getName相同,分为数组类、普通类)。

类或接口的Field的符号引用从CONSTANT_Fieldref_info获得。

类的Method的符号引用从CONSTANT_Methodref_info获得。

接口的Method的符号应用从CONSTANT_InterfaceMethodref_info获得。

还有一些基本类型的数据和字符串:CONSTANT_String_info, CONSTANT_Integer_info, CONSTANT_Float_info, CONSTANT_Long_info, CONSTANT_Double_info。

Virtual Machine Start-up:

Java虚拟机通过使用Bstrap class loader 创建一个初始类,该类与虚拟机的具体实现有关。然后就是链接、初始化该类,并执行它的mian方法。 可以在启动虚拟机时,通过命令行参数直接提供初始化类。

Creation and Loading:

 defining classLoader , initiating classLoader, runtime package of a class(determined by the package name and defining classLoader of the class)

JVMS7中新添内容:

请注意:一个功能良好的类加载器应当保证下面三个属性:
 给定相同的名称,类加载器应当总是返回相同的Class对象。
 如果类加载器L1将加载类C的请求委托给另外的类加载器L2,那么对于下列的一种类型T:C的直接父类或直接接口、或是C中的字段类型、或是C中方法或构造函数的通用参数、或是C中方法的返回值,L1和L2都应当返回相同的Class对象。
 如果某个用户自定义的类加载器预先加载了某个类或接口的二进制表示,或是加载一组相关的类型,并在加载时出现异常,那它只能在一个特定点反映出加载时的错误,该点和没有预加载时发生错误的点必须相同。

  1. 使用boostrap classLoader 加载(JVM7中删除了bootstrap classLoader 可以让user-defined classLoader代理加载类的部分)
  2. 使用User-defined classLoader加载
  3. 创建数组类(如果数组类的Component type 是引用类型的,那么它的defining classLoader和Component的一样;如果是基本数据类型,它的defining classLoader就是Bootstrap ClassLoader)
  4. loading 时的限制(对于一个类或接口C=<N1,L1>中引用的其他类D=<N2,L2>中方法或域的情况,应该满足NL1=NL2 ,N是被引用的方法或域中使用的类或者接口
  5. 从class文件中获取类
Linking:

链接的时机在具体的实现中可以不一样(lazy or eager),但必须满足如下几点:链接之前,必须已经成功加载;初始化之前,必须已经成功验证和准备;链接过程中发生的错误应该在程序请求链接出抛出。

  1. verification 用于确保类或接口的二进制表示结构上是正确的
  2. preparation 分配静态字段,并初始化为默认值,注意显示初始化是在 initialization阶段,不是preparation阶段
  3. resolution 解析类中符号引用对应的class或者interface,method,field,interface method。JVM7新增:method type and method handle resolution和call site specifier resolution
  4. access control
Initialization:

 什么时候引发初始化······

JVM是多线程的,初始化过程中要注意线程的同步,具体可以使用下面的步骤。这些步骤假定要初始化的类已经被验证和准备过,并且处于以下四种状态之一:

 Class对象已经被验证和准备过,但还没有被初始化。
 Class对象正在被其它特定线程初始化。
 Class对象已经成功被初始化且可以使用。
 Class对象处于错误的状态,可能因为尝试初始化时失败过

具体初始化步骤如下:

······

 

 

 

 

 

 

posted @ 2013-10-22 19:33  ridox  阅读(246)  评论(0编辑  收藏  举报