java.lang.Object | +--java.lang.ClassLoader
ClassLoader() Create a new ClassLoader with as parent the system classloader. |
ClassLoader(java.lang.ClassLoader parent) Create a new ClassLoader with the specified parent. |
synchronized void | clearAssertionStatus() Resets the default assertion status of this classloader, its packages and classes, all to false. |
java.lang.Class | defineClass(byte[] data, int offset, int len) Helper to define a class using a string of bytes. |
java.lang.Class | defineClass(java.lang.String name, byte[] data, int offset, int len) Helper to define a class using a string of bytes without a ProtectionDomain. |
synchronized java.lang.Class | defineClass(java.lang.String name, byte[] data, int offset, int len, java.security.ProtectionDomain domain) Helper to define a class using a string of bytes. |
java.lang.Package | definePackage(java.lang.String name, java.lang.String specTitle, java.lang.String specVendor, java.lang.String specVersion, java.lang.String implTitle, java.lang.String implVendor, java.lang.String implVersion, java.net.URL sealed) Defines a new package and creates a Package object. |
java.lang.Class | findClass(java.lang.String name) Called for every class name that is needed but has not yet been defined by this classloader or one of its parents. |
java.lang.String | findLibrary(java.lang.String name) Called by |
synchronized java.lang.Class | findLoadedClass(java.lang.String name) Helper to find an already-loaded class in this ClassLoader. |
java.net.URL | findResource(java.lang.String name) Called whenever a resource is needed that could not be provided by one of the parents of this classloader. |
java.util.Enumeration | findResources(java.lang.String name) Called whenever all locations of a named resource are needed. |
java.lang.Class | findSystemClass(java.lang.String name) Helper to find a Class using the system classloader, possibly loading it. |
java.lang.Package | getPackage(java.lang.String name) Returns the Package object for the requested package name. |
java.lang.Package[] | getPackages() Returns all Package objects defined by this classloader and its parents. |
java.lang.ClassLoader | getParent() Returns the parent of this classloader. |
java.net.URL | getResource(java.lang.String name) Get the URL to a resource using this classloader or one of its parents. |
java.io.InputStream | getResourceAsStream(java.lang.String name) Get a resource as stream using this classloader or one of its parents. |
java.util.Enumeration | getResources(java.lang.String name) Returns an Enumeration of all resources with a given name that can be found by this classloader and its parents. |
static java.lang.ClassLoader | getSystemClassLoader() Returns the system classloader. |
static java.net.URL | getSystemResource(java.lang.String name) Get the URL to a resource using the system classloader. |
static java.io.InputStream | getSystemResourceAsStream(java.lang.String name) Get a resource using the system classloader. |
static java.util.Enumeration | getSystemResources(java.lang.String name) Get an Enumeration of URLs to resources with a given name using the the system classloader. |
java.lang.Class | loadClass(java.lang.String name) Load a class using this ClassLoader or its parent, without resolving it. |
synchronized java.lang.Class | loadClass(java.lang.String name, boolean resolve) Load a class using this ClassLoader or its parent, possibly resolving
it as well using |
void | resolveClass(java.lang.Class c) Links the class, if that has not already been done. |
synchronized void | setClassAssertionStatus(java.lang.String name, boolean enabled) Set the default assertion status for a class. |
void | setDefaultAssertionStatus(boolean enabled) Set the default assertion status for classes loaded by this classloader, used unless overridden by a package or class request. |
synchronized void | setPackageAssertionStatus(java.lang.String name, boolean enabled) Set the default assertion status for packages, used unless overridden by a class request. |
void | setSigners(java.lang.Class c, java.lang.Object signers) Helper to set the signers of a class. |
protected ClassLoader()
checkCreateClassLoader
.
SecurityException
- if the security check failsprotected ClassLoader(java.lang.ClassLoader parent)
loadClass()
or getResource()
. Only when the
parent classloader cannot provide the requested class or resource the
findClass()
or findResource()
method
of this classloader will be called. There may be a security check for
checkCreateClassLoader
.
parent
- the classloader's parent, or null for the bootstrap
classloaderSecurityException
- if the security check failspublic synchronized void clearAssertionStatus()
setDefaultAssertionStatus(boolean)
setClassAssertionStatus(String, boolean)
setPackageAssertionStatus(String, boolean)
protected final Class defineClass(byte[] data, int offset, int len)
data
- the data representing the classfile, in classfile formatoffset
- the offset into the data where the classfile startslen
- the length of the classfile data in the arrayClassFormatError
- if data is not in proper classfile formatIndexOutOfBoundsException
- if offset or len is negative, or
offset + len exceeds dataprotected final Class defineClass(java.lang.String name, byte[] data, int offset, int len)
findClass()
implementation. The name should use '.'
separators, and discard the trailing ".class". The default protection
domain has the permissions of
Policy.getPolicy().getPermissions(new CodeSource(null, null)).
name
- the name to give the class, or null if unknowndata
- the data representing the classfile, in classfile formatoffset
- the offset into the data where the classfile startslen
- the length of the classfile data in the arrayClassFormatError
- if data is not in proper classfile formatIndexOutOfBoundsException
- if offset or len is negative, or
offset + len exceeds dataSecurityException
- if name starts with "java."protected final synchronized Class defineClass(java.lang.String name, byte[] data, int offset, int len, java.security.ProtectionDomain domain)
findClass()
implementation. If the
domain is null, the default of
Policy.getPolicy().getPermissions(new CodeSource(null, null))
is used. Once a class has been defined in a package, all further classes
in that package must have the same set of certificates or a
SecurityException is thrown.
name
- the name to give the class.data
- the data representing the classfile, in classfile formatoffset
- the offset into the data where the classfile startslen
- the length of the classfile data in the arraydomain
- the ProtectionDomain to give to the class, null for the
default protection domainClassFormatError
- if data is not in proper classfile formatIndexOutOfBoundsException
- if offset or len is negative, or
offset + len exceeds dataSecurityException
- if name starts with "java.", or if certificates
do not match upprotected Package definePackage(java.lang.String name, java.lang.String specTitle, java.lang.String specVendor, java.lang.String specVersion, java.lang.String implTitle, java.lang.String implVendor, java.lang.String implVersion, java.net.URL sealed)
defineClass()
. The package should not yet be defined
before in this classloader or in one of its parents (which means that
getPackage()
should return null
). All
parameters except the name
of the package may be
null
.
Subclasses should call this method from their findClass()
implementation before calling defineClass()
on a Class
in a not yet defined Package (which can be checked by calling
getPackage()
).
name
- the name of the PackagespecTitle
- the name of the specificationspecVendor
- the name of the specification designerspecVersion
- the version of this specificationimplTitle
- the name of the implementationimplVendor
- the vendor that wrote this implementationimplVersion
- the version of this implementationsealed
- if sealed the origin of the package classesIllegalArgumentException
- if the package name is null or it
was already defined by this classloader or one of its parentsprotected Class findClass(java.lang.String name)
loadClass()
after both findLoadedClass()
and
parent.loadClass()
couldn't provide the requested class.
The default implementation throws a
ClassNotFoundException
. Subclasses should override this
method. An implementation of this method in a subclass should get the
class bytes of the class (if it can find them), if the package of the
requested class doesn't exist it should define the package and finally
it should call define the actual class. It does not have to resolve the
class. It should look something like the following:
// Get the bytes that describe the requested class byte[] classBytes = classLoaderSpecificWayToFindClassBytes(name); // Get the package name int lastDot = name.lastIndexOf('.'); if (lastDot != -1) { String packageName = name.substring(0, lastDot); // Look if the package already exists if (getPackage(pkg) == null) { // define the package definePackage(packageName, ...); } } // Define and return the class return defineClass(name, classBytes, 0, classBytes.length);
loadClass()
makes sure that the Class
returned by findClass()
will later be returned by
findLoadedClass()
when the same class name is requested.
name
- class name to find (including the package name)ClassNotFoundException
- when the class can not be foundprotected String findLibrary(java.lang.String name)
Runtime.loadLibrary()
to get an absolute path
to a (system specific) library that was requested by a class loaded
by this classloader. The default implementation returns
null
. It should be implemented by subclasses when they
have a way to find the absolute path to a library. If this method
returns null the library is searched for in the default locations
(the directories listed in the java.library.path
system
property).
name
- the (system specific) name of the requested libraryRuntime#loadLibrary()
protected final synchronized Class findLoadedClass(java.lang.String name)
name
- the name of the class to findprotected URL findResource(java.lang.String name)
getResource()
after parent.getResource()
couldn't provide the requested resource.
The default implementation always returns null. Subclasses should override this method when they can provide a way to return a URL to a named resource.
name
- the name of the resource to be foundprotected Enumeration findResources(java.lang.String name)
getResources()
after it has called
parent.getResources()
. The results are combined by
the getResources()
method.
The default implementation always returns an empty Enumeration.
Subclasses should override it when they can provide an Enumeration of
URLs (possibly just one element) to the named resource.
The first URL of the Enumeration should be the same as the one
returned by findResource
.
name
- the name of the resource to be foundIOException
- if I/O errors occur in the processprotected final Class findSystemClass(java.lang.String name)
findClass(String)
.
name
- the name of the class to findClassNotFoundException
- if the class cannot be foundprotected Package getPackage(java.lang.String name)
name
- the package name to findprotected Package[] getPackages()
public final ClassLoader getParent()
null
. A security check may be performed on
RuntimePermission("getClassLoader")
.
SecurityException
- if the security check failspublic URL getResource(java.lang.String name)
getResource()
on the parent classloader. If the parent classloader returns null then
it tries finding the resource by calling findResource()
on
this classloader. The resource name should be separated by '/' for path
elements.
Subclasses should not override this method but should override
findResource()
which is called by this method.
name
- the name of the resource relative to this classloaderpublic InputStream getResourceAsStream(java.lang.String name)
getResource()
and if that returns a URL to
the resource then it calls and returns the InputStream given by
URL.openStream()
.
Subclasses should not override this method but should override
findResource()
which is called by this method.
name
- the name of the resource relative to this classloaderpublic final Enumeration getResources(java.lang.String name)
getResource() only offers the first resource (of the
parent) with a given name. This method lists all resources with the
same name. The name should use '/' as path separators.
The Enumeration is created by first calling getResources()
on the parent classloader and then calling findResources()
on this classloader.
name
- the resource nameIOException
- if I/O errors occur in the processpublic static ClassLoader getSystemClassLoader()
java.class.path
. This is set as the context
class loader for a thread. The system property
java.system.class.loader
, if defined, is taken to be the
name of the class to use as the system class loader, which must have
a public constructor which takes a ClassLoader as a parent; otherwise this
uses gnu.java.lang.SystemClassLoader.
Note that this is different from the bootstrap classloader that actually loads all the real "system" classes (the bootstrap classloader is the parent of the returned system classloader).
A security check will be performed for
RuntimePermission("getClassLoader")
if the calling class
is not a parent of the system class loader.
SecurityException
- if the security check failsIllegalStateException
- if this is called recursivelyError
- if java.system.class.loader
fails to loadpublic static final URL getSystemResource(java.lang.String name)
name
- the name of the resource relative to the system classloaderpublic static final InputStream getSystemResourceAsStream(java.lang.String name)
name
- the name of the resource relative to the system classloaderpublic static Enumeration getSystemResources(java.lang.String name)
name
- the name of the resource relative to the system classloaderIOException
- if I/O errors occur in the processpublic Class loadClass(java.lang.String name)
loadClass(name, false)
.
Subclasses should not override this method but should override
findClass()
which is called by this method.
name
- the name of the class relative to this ClassLoaderClassNotFoundException
- if the class cannot be foundprotected synchronized Class loadClass(java.lang.String name, boolean resolve)
resolveClass()
. It first tries to find
out if the class has already been loaded through this classloader by
calling findLoadedClass()
. Then it calls
loadClass()
on the parent classloader (or when there is
no parent it uses the VM bootclassloader)). If the class is still
not loaded it tries to create a new class by calling
findClass()
. Finally when resolve
is
true
it also calls resolveClass()
on the
newly loaded class.
Subclasses should not override this method but should override
findClass()
which is called by this method.
name
- the fully qualified name of the class to loadresolve
- whether or not to resolve the classClassNotFoundException
- if the class cannot be foundprotected final void resolveClass(java.lang.Class c)
c
- the class to resolveNullPointerException
- if c is nullLinkageError
- if linking failspublic synchronized void setClassAssertionStatus(java.lang.String name, boolean enabled)
name
- the class to affectenabled
- true to set the default to enabledNullPointerException
- if name is nullsetDefaultAssertionStatus(String, boolean)
setPackageAssertionStatus(String, boolean)
clearAssertionStatus()
public void setDefaultAssertionStatus(boolean enabled)
enabled
- true to set the default to enabledsetClassAssertionStatus(String, boolean)
setPackageAssertionStatus(String, boolean)
clearAssertionStatus()
public synchronized void setPackageAssertionStatus(java.lang.String name, boolean enabled)
name
- the package (and subpackages) to affectenabled
- true to set the default to enabledsetDefaultAssertionStatus(String, boolean)
setClassAssertionStatus(String, boolean)
clearAssertionStatus()
protected final void setSigners(java.lang.Class c, java.lang.Object signers)
c
- the Class to set signers ofsigners
- the signers to set
Every classloader has a parent classloader that is consulted before the 'child' classloader when classes or resources should be loaded. This is done to make sure that classes can be loaded from an hierarchy of multiple classloaders and classloaders do not accidentially redefine already loaded classes by classloaders higher in the hierarchy.
The grandparent of all classloaders is the bootstrap classloader, which loads all the standard system classes as implemented by GNU Classpath. The other special classloader is the system classloader (also called application classloader) that loads all classes from the CLASSPATH (
java.class.path
system property). The system classloader is responsible for finding the application classes from the classpath, and delegates all requests for the standard library classes to its parent the bootstrap classloader. Most programs will load all their classes through the system classloaders.The bootstrap classloader in GNU Classpath is implemented as a couple of static (native) methods on the package private class
java.lang.VMClassLoader
, the system classloader is an instance ofgnu.java.lang.SystemClassLoader
(which is a subclass ofjava.net.URLClassLoader
).Users of a
ClassLoader
will normally just use the methodsloadClass()
to load a class.getResource()
orgetResourceAsStream()
to access a resource.getResources()
to get an Enumeration of URLs to all the resources provided by the classloader and its parents with the same name.Subclasses should implement the methods
findClass()
which is called byloadClass()
when the parent classloader cannot provide a named class.findResource()
which is called bygetResource()
when the parent classloader cannot provide a named resource.findResources()
which is called bygetResource()
to combine all the resources with the same name from the classloader and its parents.findLibrary()
which is called byRuntime.loadLibrary()
when a class defined by the classloader wants to load a native library.