ClassLoader javaDoc 解读

 

java.lang

public abstract class ClassLoader

extends Object

ClassLoader 类继承的Object类,位于java.lang 包下,它是由根类加载器进行加载的,因为它位于rt.jar 包中

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 是一个抽象类。类加载器通过给定的class 文件名,尝试进行定位或者加载生成构成定义该类的数据。通常的做法是在文件系统中将名称转化为文件名,并从文件系统中根据名称进行读取。

Every Class object contains a reference to the ClassLoader that defined it.

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 对象都包含了一个定义它自身的ClassLoader 的引用。

关于数组的class对象,它并不是被类加载器进行创建的,而是在java 运行过程中被要求自动进行创建的。对于一个数组类的类加载器来说,调用Class.getClassLoader() 会返回与数组元素类型中的类加载器。如果数组元素中是原生的数据类型(8种基本类型),那么它没有对应的类加载器。

Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.

Class loaders may typically be used by security managers to indicate security domains.

应用可以通过实现ClassLoader 的子类去获取 java虚拟机的动态加载器的方式。类加载器通常通过安全管理去表示安全域。

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.

类加载器可以通过使用委托模型去查找class 或者资源。每一个实例的类加载器都有一个与之关联的父类加载器。当请求加载class 和资源时,一个类加载器的实例在自身加载类时,首先会委托给它的父类加载器进行加载。java 虚拟机中嵌入了一个 根类加载器,它没有父加载器,但是它是可以做为类加载器的父加载器的。

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.registerAsParallelCapable method. 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. 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).

支持并发加载类的类加载器被称为并行类加载器。而且它被要求在它们初始化的时候调用ClassLoader.registerAsParallelCapable 方法 去注册它们自身。注意:默认情况下,类加载器被注册为支持并行的。然而如果它的子类们需要并行能力,那么它们仍需要注册它们自身。在委托模型并不严格分层的环境中,类加载器需要支持并行加载,这是因为类在加载的过程中会持续的持有加载锁,这可能会导致死锁(可以查阅 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.

通常,java 虚拟机从本地文件系统中,以平台相关的方式进行加载类。例如:在unix 操作系统中,java 虚拟机直接从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:

例如,一个应用中创建了一个网络类加载器去从服务器中下载了class 文件,代码样本如下:

ClassLoader loader = new NetworkClassLoader(host, port);

Object main = loader.loadClass("Main", true).newInstance();

. . .

The network class loader subclass must define the methods findClass and loadClassData to load a class from the network. Once it has downloaded the bytes that make up the class, it should use the method defineClass to create a class instance. A sample implementation is:

网络类加载器必须定义 findClass 方法和 loadClassData 方法去加载来源于网络的类。一旦,下载完成组成类的所有字节数据后,它应该调用defineClass 方法去创建一个类实例。具体实现如下:

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

. . .

}

}

Binary names

Any class name provided as a String parameter to methods in ClassLoader must be a binary name as defined by The Java™ Language Specification.

关于二进制名称,在类加载器中作为字符串参数提供方法的任何类名必须符合java语言规范的二进制名。

Examples of valid class names include:

"java.lang.String"

"javax.swing.JSpinner$DefaultEditor"

"java.security.KeyStore$Builder$FileBuilder$1"

"java.net.URLClassLoader$3$1"

Since:

1.0

See Also:

resolveClass(Class)

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值