ClassLoader
is an abstract class.
Applications implement subclasses of ClassLoader
in
order to extend the manner in which the Java Virtual Machine
dynamically loads classes.
package java.lang; import java.io.InputStream; import java.util.Hashtable; (>JDK1.0)public abstract class ClassLoader { // Constructeur protégé (>JDK1.0) protected ClassLoader(); // Méthodes de classe publiques (>JDK1.1) public static final java.net.URL getSystemResource(String name); (>JDK1.1) public static final InputStream getSystemResourceAsStream(String name); // Méthodes d'instance publiques (>JDK1.1) public java.net.URL getResource(String name); (>JDK1.1) public InputStream getResourceAsStream(String name); (>JDK1.1) public Class loadClass(String name) throws ClassNotFoundException; // Méthodes d'instance protégées // obsolète Remplacé par defineClass(String, byte[], int, int) (>JDK1.0) protected final Class defineClass(byte data[], int offset, int length); (>JDK1.1) protected final Class defineClass(String name, byte data[], int offset, int length); (>JDK1.0) protected final Class findSystemClass(String name) throws ClassNotFoundException; (>JDK1.0) protected abstract Class loadClass(String name, boolean resolve) throws ClassNotFoundException; (>JDK1.0) protected final void resolveClass(Class c); (>JDK1.1) protected final void setSigners(Class cl, Object[] signers); } |
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.
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 the newInstance
method in class Class
.
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 calls the loadClass
method of the class loader that originally created the class. If
the Java Virtual Machine only needs to determine if the class
exists and if it does exist to know its superclass, the
resolve
flag is set to false
. However,
if an instance of the class is being created or any of its methods
are being called, the class must also be resolved. In this case
the resolve
flag is set to true
, and the
resolveClass
method should be called.
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 method
loadClass
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:
class NetworkClassLoader { String host; int port; Hashtable cache = new Hashtable(); private byte loadClassData(String name)[] { // load the class data from the connection . . . } public synchronized Class loadClass(String name, boolean resolve) { Class c = cache.get(name); if (c == null) { byte data[] = loadClassData(name); c = defineClass(data, 0, data.length); cache.put(name, c); } if (resolve) resolveClass(c); return c; } }
Christophe Merlet redfox@redfoxcenter.org | ©Tous droits réservés
11 septembre 1998 |