Contents | Package | Class | Tree | Deprecated | Index | Help | Java 1.2 Beta 3 | ||
PREV | NEXT | SHOW LISTS | HIDE LISTS |
java.lang.Object | +----java.lang.ClassLoader
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.
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 extends ClassLoader { String host; int port; public Class findLocalClass(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 . . . } }
Constructor Summary | |
ClassLoader(ClassLoader parent)
|
|
ClassLoader()
ClassLoader
returned by the method getBaseClassLoader() as the
parent class loader.
|
Method Summary | |
void | checkPackageAccess(String name)
SecurityException
if the calling thread is not allowed to access the package specified
by the argument.
|
Class | defineClass(byte[] b,
int off,
int len)
Class .
Deprecated |
Class | defineClass(String name,
byte[] b,
int off,
int len)
Class .
|
Package | definePackage(String name,
String specTitle,
String specVersion,
String specVendor,
String implTitle,
String implVersion,
String implVendor,
URL sealBase)
|
Class | findLoadedClass(String name)
|
Class | findLocalClass(String name)
|
Class | findSystemClass(String name)
|
static ClassLoader | getBaseClassLoader()
|
URL | getLocalResource(String name)
|
Enumeration | getLocalResources(String name)
|
Package | getPackage(String name)
|
Package[] | getPackages()
|
ClassLoader | getParent()
null
if none, in which case ClassLoader will delegate to the
system class loader.
|
URL | getResource(String name)
|
InputStream | getResourceAsStream(String name)
|
Enumeration | getResources(String name)
|
static URL | getSystemResource(String name)
|
static InputStream | getSystemResourceAsStream(String name)
|
static Enumeration | getSystemResources(String name)
|
Class | loadClass(String name)
|
Class | loadClass(String name,
boolean resolve)
|
void | resolveClass(Class c)
|
void | setSigners(Class c,
Object[] signers)
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
protected ClassLoader(ClassLoader parent)
If there is a security manager, its checkCreateClassLoader
method is called. This may result in a security exception.
protected ClassLoader()
ClassLoader
returned by the method getBaseClassLoader()
as the
parent class loader.
If there is a security manager, its checkCreateClassLoader
method is called. This may result in a security exception.
Method Detail |
public Class loadClass(String name) throws ClassNotFoundException
resolveClass
method will be called on the resulting Class
object.
name
- the name of the class
Class
objectprotected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
loadClass
method is called by the Java Virtual Machine when a class loaded by
a class loader first references another class. The default
implementation of loadClass
will search for classes in
the following order:
findLoadedClass
to check if the class has
already been loaded.
loadClass
method of the parent class
loader to load the class, or findSystemClass
if no parent class loader was specified.
findLocalClass
method to find the class
locally.
resolve
flag is true, this method will then call
the resolveClass
method on the resulting class object.
Class loader implementations that use the new delegation model
introduced in 1.2 should override the findLocalClass
method rather than loadClass
.
name
- the name of the class
resolve
- if true
then resolve the class
Class
objectprotected void checkPackageAccess(String name) throws SecurityException
SecurityException
if the calling thread is not allowed to access the package specified
by the argument. By default, the calling thread is allowed to access
any package.
For example, in order to perform this check using the current
SecurityManager
:
SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPackageAccess(name.substring(0, i)); }
name
- the package name
protected Class findLocalClass(String name) throws ClassNotFoundException
loadClass
method after checking the parent class loader for the requested class.
The default implementation throws ClassNotFoundException
.
name
- the name of the class
Class
objectprotected final Class defineClass(byte[] b, int off, int len) throws ClassFormatError
Class
. Before the Class can be used it must be
resolved. This method is deprecated in favor of the version
that takes the class name as its first argument, and is more
secure.
b
- the bytes that make up the class data
off
- the start offset of the class data
len
- the length of the class data
Class
object that was created from the
specified class dataprotected final Class defineClass(String name, byte[] b, int off, int len) throws ClassFormatError
Class
.
Before the Class can be used it must be resolved.
This method assigns a default ProtectionDomain
to
the newly defined class. The ProtectionDomain
contains the set of permissions granted when
a call to Policy.getPolicy().evaluate()
is made with a
Codesource of null,null
. The default domain is
created on the first invocation of defineClass
, and
re-used on subsequent calls.
Note: There is a similar defineClass
method in
java.security.SecureClassLoader
(a ClassLoader
subclass) that assigns the new class to a passed-in
ProtectionDomain
. Also, a class can be assigned to
a new ProtectionDomain
(one with different permissions)
by a call to the java.lang.Class setProtectionDomain
method, if the caller has permission to
reset the domain.
name
- the expected name of the class, or null
if not known, using '.' and not '/' as the separator
and without a trailing ".class" suffix.
b
- the bytes that make up the class data
off
- the start offset of the class data
len
- the length of the class data
Class
object that was created from the
specified class dataprotected final void resolveClass(Class c)
loadClass
if the resolve flag is
true
.
c
- the class to be resolved
protected final Class findSystemClass(String name) throws ClassNotFoundException
A system class is a class loaded from the local file system in a platform-dependent manner, and has no associated class loader.
name
- the name of the system class
public ClassLoader getParent()
null
if none, in which case ClassLoader
will delegate to the
system class loader.RuntimePermission("getClassLoader")
permissionprotected final void setSigners(Class c, Object[] signers)
c
- the Class
object
signers
- the signers for the class
protected final Class findLoadedClass(String name)
name
- the class name
Class object, or null
if
the class has not been loaded
public URL getResource(String name)
The name of a resource is a "/"-separated path name that identifies the resource.
This method will first search the parent class loader for the
resource, then call getLocalResource
to find the
resource locally.
name
- resource name
null
if
the resource could not be found or the caller doesn't have
adequate privileges to get the resource.public final Enumeration getResources(String name) throws IOException
The name of a resource is a "/"-separated path name that identifies the resource.
This method will first search the parent class loader for the
resource, then call getLocalResource
to find the
resource locally.
name
- resource name
public Enumeration getLocalResources(String name) throws IOException
name
- the resource name
public URL getLocalResource(String name)
name
- the resource name
null
if the resource could not be foundpublic static URL getSystemResource(String name)
name
- the resource name
null
if
the resource could not be foundpublic static Enumeration getSystemResources(String name)
name
- the resource name
public InputStream getResourceAsStream(String name)
name
- the resource name
null
if the resource could not be foundpublic static InputStream getSystemResourceAsStream(String name)
name
- the resource name
null
if the resource could not be foundpublic static ClassLoader getBaseClassLoader()
ClassLoader
instances, and
is usually the ClassLoader
that was used to start the
application.ClassLoader
for delegation, or
null
if noneRuntimePermission("getClassLoader")
permissionprotected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) throws IllegalArgumentException
name
- the package name
specTitle
- the specification title
specVersion
- the specification version
specVendor
- the specification vendor
implTitle
- the implementation title
implVersion
- the implementation version
implVendor
- the implementation vendor
sealBase
- if specified, then this package is sealed with
respect to the given base URL
protected Package getPackage(String name)
name
- the package name
protected Package[] getPackages()
Contents | Package | Class | Tree | Deprecated | Index | Help | Java 1.2 Beta 3 | ||
PREV | NEXT | SHOW LISTS | HIDE LISTS |