|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.ClassLoader
ClassLoader is an abstract Class that can be used to define a policy for loading Java classes into the runtime environment. By default, the runtime system loads classes that originate as files by reading them from the directory defined by the CLASSPATH environment variable (this is platform dependent). The default mechanism does not involve a Class loader.
However, some classes may not originate from a file; they could be loaded from some other source, e.g., the network. Classes loaded from the network are an array of bytes. A ClassLoader can be used to tell the runtime system to convert an array of bytes into an instance of class Class. This conversion information is passed to the runtime using the defineClass() method.
Classes that are created through the defineClass() mechanism can reference other classes by name. To resolve those names, the runtime system calls the ClassLoader that originally created the Class. The runtime system calls the abstract method loadClass() to load the referenced classes.
ClassLoader loader = new NetworkClassLoader(host, port); Object main = loader.loadClass("Main").newInstance(); ....The NetworkClassLoader 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 could be:
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) { Class c = cache.get(name); if (c == null) { byte data[] = loadClassData(name); cache.put(name, defineClass(data, 0, data.length)); } return c; } }
Class
Constructor Summary | |
protected |
ClassLoader()
Constructs a new Class loader and initializes it. |
Method Summary | |
protected Class |
defineClass(byte[] data,
int offset,
int length)
Converts an array of bytes to an instance of class Class. |
protected Class |
findSystemClass(String name)
Loads a system Class. |
protected abstract Class |
loadClass(String name,
boolean resolve)
Resolves the specified name to a Class. |
protected void |
resolveClass(Class c)
Resolves classes referenced by this Class. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
protected ClassLoader()
Method Detail |
protected abstract Class loadClass(String name, boolean resolve) throws ClassNotFoundException
name
- the name of the desired Classresolve
- true if the Class needs to be resolved
ClassNotFoundException
- Cannot find a definition for the classHashtable
protected final Class defineClass(byte[] data, int offset, int length)
data
- the bytes that make up the Classoffset
- the start offset of the Class datalength
- the length of the Class data
ClassFormatError
- If the data does not contain a valid
Class.loadClass(java.lang.String, boolean)
,
resolveClass(java.lang.Class)
protected final void resolveClass(Class c)
c
- the Class to be resolveddefineClass(byte[], int, int)
protected final Class findSystemClass(String name) throws ClassNotFoundException
name
- the name of the system Class
NoClassDefFoundError
- If the Class is not found.
ClassNotFoundException
- Cannot find a definition for the class
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |