5.3.2 使用用户定义的类加载器加载

以下步骤用于使用用户定义的类加载器L创建N中定义的非数组类或接口C。 首先,Java虚拟机确定L是否已经被记录为由N表示的类或接口的初始化加载器。如果是,则该类或接口是C,并且不需要创建类。 否则,Java虚拟机调用L.loadClass(n)。返回的值是创建的类或接口C。然后,Java虚拟机记录L是C的起始加载器(参见5.3.4节)。本节的其余部分将更详细地描述这个过程。 When the loadClass method of the class loader L is invoked with the name N of a class or interface C to be loaded, L must perform one of the following two operations in order to load C: 1. The class loader L can create an array of bytes representing C as the bytes of a ClassFile structure (§4.1); it then must invoke the method defineClass of class ClassLoader. Invoking defineClass causes the Java Virtual Machine to derive a class or interface denoted by N using L from the array of bytes using the algorithm found in §5.3.5. 2. The class loader L can delegate the loading of C to some other class loader L'. This is accomplished by passing the argument N directly or indirectly to an invocation of a method on L' (typically the loadClass method). The result of the invocation is C. In either (1) or (2), if the class loader L is unable to load a class or interface denoted by N for any reason, it must throw an instance of ClassNotFoundException. Since JDK release 1.1, Oracle’s Java Virtual Machine implementation has invoked the loadClass method of a class loader in order to cause it to load a class or interface. The argument to loadClass is the name of the class or interface to be loaded. There is also a two-argument version of the loadClass method, where the second argument is a boolean that indicates whether the class or interface is to be linked or not. Only the two-argument version was supplied in JDK release 1.0.2, and Oracle’s Java Virtual Machine implementation relied on it to link the loaded class or interface. 从JDK版本1.1开始,Oracle的Java虚拟机实现直接链接类或接口,而不依赖于类加载器。

贡献翻译,请加 QQ: 840750575    点击这里给我发消息
数码
沪ICP备19006215号-4