类说明
ClassLoader 类加载器是 JVM 的核心组件之一,负责在运行时将.class文件加载到 JVM 中。实现了 Java 的按需加载机制,即只有当类被真正使用时才加载,这有助于优化内存使用和提高程序性能。
ClassLoader 是一个抽象类,定义了类加载的基本行为和接口。实现了双亲委派模型,是 Java 类加载机制的核心。
ClassLoader 类上的注释
A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a “class file” of that name from a file system.
类加载器是一个负责加载类的对象。类 ClassLoader 是一个抽象类。给定二进制类名,类加载器应该尝试定位或生成构成类定义的数据。一个典型的策略是将名称转换为文件名,然后从文件系统中读取该名称的“类文件”
Every Class object contains a
Class#getClassLoader()reference to the ClassLoader that defined it.每个 Class 对象都包含对定义它的 ClassLoader 的
Class#getClassLoader()引用。Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by
Class#getClassLoader()is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.数组类的 Class 对象不是由类加载器创建的,而是根据 Java 运行时的要求自动创建的。通过
Class#getClassLoader()返回的数组类的类加载器与其元素类型的类加载器相同。如果元素类型是基元类型,则数据类没有类加载器Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.
应用程序通过实现 ClassLoader 的,来扩展 JVM 动态加载类的方式
Class loaders may typically be used by security managers to indicate security domains.
类加载器通常可由安全管理器用来标识安全域
The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When requested to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. The virtual machine’s built-in class loader, called the “bootstrap class loader”, does not itself have a parent but may serve as the parent of a ClassLoader instance.
ClassLoader 类使用一种委托模型来搜索类和资源。每个 ClassLoader 实例都有一个关联的父类加载器。当被请求查找某个类或资源时,ClassLoader 实例会先将该搜索任务委托给其父类加载器,仅在父类加载器无法完成时,才会自行查找。虚拟机内置的类加载器称为“启动类加载器 bootstrap class loader”,它本身没有父加载器,但可以作为其他 ClassLoader 实例的父加载器
Class loaders that support concurrent loading of classes are known as parallel capable class loaders and are required to register themselves at their class initialization time by invoking the
ClassLoader.registerAsParallelCapablemethod. Note that the ClassLoader class is registered as parallel capable by default. However, its subclasses still need to register themselves if they are parallel capable.支持并发加载类的类加载器被称为具备并行能力的类加载器,它们需要再类初始化时通过调用
ClassLoader.registerAsParallelCapable方法来完成自我注册。需要注意的是,ClassLoader 类本身默认已注册为具备并行能力。然而,其子类若想支持并行加载,仍需主动注册In environments in which the delegation model is not strictly hierarchical, class loaders need to be parallel capable, otherwise class loading can lead to deadlocks because the loader lock is held for the duration of the class loading process (see loadClass methods).
在委托模型并非严格层次化的环境下(例如,类加载器之间存在复杂的委托关系,而不是简单的父子层级),类加载器必须具备并行能力。否则,类加载过程可能导致死锁。这是因为在整个类加载期间,加载器锁会被持续持有。(可以参考loadClass 方法)
Normally, the Java virtual machine loads classes from the local file system in a platform-dependent manner. For example, on UNIX systems, the virtual machine loads classes from the directory defined by the CLASSPATH environment variable.
通常,JVM 以一种平台相关的方式从本地文件系统加载类。例如,在 UNIX 系统上,虚拟机会从由 classpath 环境变量所定义的目录中加载类
However, some classes may not originate from a file; they may originate from other sources, such as the network, or they could be constructed by an application. The method defineClass converts an array of bytes into an instance of class Class. Instances of this newly defined class can be created using
Class.newInstance.然而,有些类并非源自文件;他们可能来自其他来源,例如网络,或者由应用程序动态构建。defineClass 方法将一个字节数组转换成 Class 类的一个实例。这个新定义类的实例可以通过
Class.newInstance来创建。The methods and constructors of objects created by a class loader may reference other classes. To determine the class(es) referred to, the Java virtual machine invokes the loadClass method of the class loader that originally created the class.
由类加载器创建的对象,其方法和构造方法可能会引用其他类。为了确定这些被引用的类,JVM 会调用最初创建该类的类加载器的loadClass 方法
For example, an application could create a network class loader to download class files from a server. Sample code might look like:
例如,应用程序可以创建一个网络类加载器,用于从服务器下载类文件。示例代码可能如下所示:
ClassLoader loader = new NetworkClassLoader(host, port);
Object main = loader.loadClass("Main", true).newInstance();
......
The network class loader subclass must define the methods
findClassandloadClassDatato load a class from the network. Once it has downloaded the bytes that make up the class, it should use the methoddefineClassto create a class instance. A sample implementation is:网络类加载子类必须定义
findClass和loadClassData方法,以实现从网络加载类。一旦下载了构成该类的字节码,就应该调用defineClass方法来创建对应的 Class 实例。一个示例实现如下:
class NetworkClassLoader extends ClassLoader {
String host;
int port;
public Class findClass(String name) {
byte[] b = loadClassData(name);
return defineClass(name, b, 0, b.length);
}
private byte[] loadClassData(String name) {
// load the class data from the connection
. . .
}
}
二进制名示例
“java.lang.String”
“javax.swing.JSpinner$DefaultEditor”
“java.security.KeyStoreBuilderBuilderBui


721

被折叠的 条评论
为什么被折叠?



