Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
java.lang.ClassLoader
public abstract class ClassLoader
extends Object
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 of gnu.java.lang.SystemClassLoader
(which is a subclass of java.net.URLClassLoader
).
Users of a ClassLoader
will normally just use the methods
loadClass()
to load a class.getResource()
or getResourceAsStream()
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 by loadClass()
when the parent classloader cannot provide a named class.findResource()
which is called by
getResource()
when the parent classloader cannot provide
a named resource.findResources()
which is called by
getResource()
to combine all the resources with the
same name from the classloader and its parents.findLibrary()
which is called by
Runtime.loadLibrary()
when a class defined by the
classloader wants to load a native library.Class
Constructor Summary | |
| |
|
Method Summary | |
void |
|
protected Class |
|
protected Class |
|
protected Class |
|
protected Class |
|
protected Package | |
protected Class | |
protected String |
|
protected Class |
|
protected URL |
|
protected Enumeration |
|
protected Class |
|
protected Package |
|
protected Package[] |
|
ClassLoader |
|
URL |
|
InputStream |
|
Enumeration |
|
static ClassLoader |
|
static URL |
|
static InputStream |
|
static Enumeration |
|
Class | |
protected Class | |
protected void |
|
void |
|
void |
|
void |
|
protected void |
|
Methods inherited from class java.lang.Object | |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
protected ClassLoader() throws SecurityException
Create a new ClassLoader with as parent the system classloader. There may be a security check forcheckCreateClassLoader
.
- Throws:
SecurityException
- if the security check fails
protected ClassLoader(ClassLoader parent)
Create a new ClassLoader with the specified parent. The parent will be consulted when a class or resource is requested throughloadClass()
orgetResource()
. Only when the parent classloader cannot provide the requested class or resource thefindClass()
orfindResource()
method of this classloader will be called. There may be a security check forcheckCreateClassLoader
.
- Parameters:
parent
- the classloader's parent, or null for the bootstrap classloader
- Throws:
SecurityException
- if the security check fails
- Since:
- 1.2
public void clearAssertionStatus()
Resets the default assertion status of this classloader, its packages and classes, all to false. This allows overriding defaults inherited from the command line.
- Since:
- 1.4
protected final Class defineClass(byte[] data, int offset, int len) throws ClassFormatError
Deprecated. use
defineClass(String,byte[],int,int)
insteadHelper to define a class using a string of bytes. This version is not secure.
- Parameters:
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 array
- Returns:
- the class that was defined
- Throws:
ClassFormatError
- if data is not in proper classfile formatIndexOutOfBoundsException
- if offset or len is negative, or offset + len exceeds data
protected final Class defineClass(String name, byte[] data, int offset, int len) throws ClassFormatError
Helper to define a class using a string of bytes without a ProtectionDomain. Subclasses should call this method from theirfindClass()
implementation. The name should use '.' separators, and discard the trailing ".class". The default protection domain has the permissions ofPolicy.getPolicy().getPermissions(new CodeSource(null, null))
.
- Parameters:
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 array
- Returns:
- the class that was defined
- Throws:
ClassFormatError
- if data is not in proper classfile formatIndexOutOfBoundsException
- if offset or len is negative, or offset + len exceeds dataSecurityException
- if name starts with "java."
- Since:
- 1.1
protected final Class defineClass(String name, byte[] data, int offset, int len, ProtectionDomain domain) throws ClassFormatError
Helper to define a class using a string of bytes. Subclasses should call this method from theirfindClass()
implementation. If the domain is null, the default ofPolicy.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.
- Parameters:
name
- the name to give the class. 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 arraydomain
- the ProtectionDomain to give to the class, null for the default protection domain
- Returns:
- the class that was defined
- Throws:
ClassFormatError
- 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 up
- Since:
- 1.2
protected final Class defineClass(String name, ByteBuffer buf, ProtectionDomain domain) throws ClassFormatError
Helper to define a class using the contents of a byte buffer. If the domain is null, the default ofPolicy.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.
- Parameters:
name
- the name to give the class. null if unknownbuf
- a byte buffer containing bytes that form a class.domain
- the ProtectionDomain to give to the class, null for the default protection domain
- Returns:
- the class that was defined
- Throws:
ClassFormatError
- if data is not in proper classfile formatSecurityException
- if name starts with "java.", or if certificates do not match up
- Since:
- 1.5
protected Package definePackage(String name, String specTitle, String specVendor, String specVersion, String implTitle, String implVendor, String implVersion, URL sealed)
Defines a new package and creates a Package object. The package should be defined before any class in the package is defined withdefineClass()
. The package should not yet be defined before in this classloader or in one of its parents (which means thatgetPackage()
should returnnull
). All parameters except thename
of the package may benull
.Subclasses should call this method from their
findClass()
implementation before callingdefineClass()
on a Class in a not yet defined Package (which can be checked by callinggetPackage()
).
- Parameters:
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 classes
- Returns:
- the Package object for the specified package
- Throws:
IllegalArgumentException
- if the package name is null or it was already defined by this classloader or one of its parents
- Since:
- 1.2
- See Also:
Package
protected Class findClass(String name) throws ClassNotFoundException
Called for every class name that is needed but has not yet been defined by this classloader or one of its parents. It is called byloadClass()
after bothfindLoadedClass()
andparent.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(packageName) == null) { // define the package definePackage(packageName, ...); } } // Define and return the class return defineClass(name, classBytes, 0, classBytes.length);
loadClass()
makes sure that theClass
returned byfindClass()
will later be returned byfindLoadedClass()
when the same class name is requested.
- Parameters:
name
- class name to find (including the package name)
- Returns:
- the requested Class
- Throws:
ClassNotFoundException
- when the class can not be found
- Since:
- 1.2
protected String findLibrary(String name)
Called byRuntime.loadLibrary()
to get an absolute path to a (system specific) library that was requested by a class loaded by this classloader. The default implementation returnsnull
. 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 thejava.library.path
system property).
- Parameters:
name
- the (system specific) name of the requested library
- Returns:
- the full pathname to the requested library, or null
- Since:
- 1.2
- See Also:
Runtime.loadLibrary(String)
protected final Class findLoadedClass(String name)
Helper to find an already-loaded class in this ClassLoader.
- Parameters:
name
- the name of the class to find
- Returns:
- the found Class, or null if it is not found
- Since:
- 1.1
protected URL findResource(String name)
Called whenever a resource is needed that could not be provided by one of the parents of this classloader. It is called bygetResource()
afterparent.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.
- Parameters:
name
- the name of the resource to be found
- Returns:
- a URL to the named resource or null when not found
- Since:
- 1.2
protected EnumerationfindResources(String name) throws IOException
Called whenever all locations of a named resource are needed. It is called bygetResources()
after it has calledparent.getResources()
. The results are combined by thegetResources()
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
.
- Parameters:
name
- the name of the resource to be found
- Returns:
- a possibly empty Enumeration of URLs to the named resource
- Throws:
IOException
- if I/O errors occur in the process
- Since:
- 1.2
protected final Class findSystemClass(String name) throws ClassNotFoundException
Helper to find a Class using the system classloader, possibly loading it. A subclass usually does not need to call this, if it correctly overridesfindClass(String)
.
- Parameters:
name
- the name of the class to find
- Returns:
- the found class
- Throws:
ClassNotFoundException
- if the class cannot be found
protected Package getPackage(String name)
Returns the Package object for the requested package name. It returns null when the package is not defined by this classloader or one of its parents.
- Parameters:
name
- the package name to find
- Returns:
- the package, if defined
- Since:
- 1.2
protected Package[] getPackages()
Returns all Package objects defined by this classloader and its parents.
- Returns:
- an array of all defined packages
- Since:
- 1.2
public final ClassLoader getParent()
Returns the parent of this classloader. If the parent of this classloader is the bootstrap classloader then this method returnsnull
. A security check may be performed onRuntimePermission("getClassLoader")
.
- Returns:
- the parent
ClassLoader
- Throws:
SecurityException
- if the security check fails
- Since:
- 1.2
public URL getResource(String name)
Get the URL to a resource using this classloader or one of its parents. First tries to get the resource by callinggetResource()
on the parent classloader. If the parent classloader returns null then it tries finding the resource by callingfindResource()
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.
- Parameters:
name
- the name of the resource relative to this classloader
- Returns:
- the URL to the resource or null when not found
public InputStream getResourceAsStream(String name)
Get a resource as stream using this classloader or one of its parents. First callsgetResource()
and if that returns a URL to the resource then it calls and returns the InputStream given byURL.openStream()
.Subclasses should not override this method but should override
findResource()
which is called by this method.
- Parameters:
name
- the name of the resource relative to this classloader
- Returns:
- an InputStream to the resource, or null
- Since:
- 1.1
public EnumerationgetResources(String name) throws IOException
Returns an Enumeration of all resources with a given name that can be found by this classloader and its parents. Certain classloaders (such as the URLClassLoader when given multiple jar files) can have multiple resources with the same name that come from multiple locations. It can also occur that a parent classloader offers a resource with a certain name and the child classloader also offers a resource with that same 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 callingfindResources()
on this classloader.
- Parameters:
name
- the resource name
- Returns:
- an enumaration of all resources found
- Throws:
IOException
- if I/O errors occur in the process
- Since:
- 1.2
public static ClassLoader getSystemClassLoader()
Returns the system classloader. The system classloader (also called the application classloader) is the classloader that is used to load the application classes on the classpath (given by the system propertyjava.class.path
. This is set as the context class loader for a thread. The system propertyjava.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. The parent class loader passed in the constructor is the default system class loader.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.
- Returns:
- the system class loader
- Throws:
SecurityException
- if the security check failsIllegalStateException
- if this is called recursively
- Since:
- 1.2
public static final URL getSystemResource(String name)
Get the URL to a resource using the system classloader.
- Parameters:
name
- the name of the resource relative to the system classloader
- Returns:
- the URL to the resource
- Since:
- 1.1
public static final InputStream getSystemResourceAsStream(String name)
Get a resource using the system classloader.
- Parameters:
name
- the name of the resource relative to the system classloader
- Returns:
- an input stream for the resource, or null
- Since:
- 1.1
public static EnumerationgetSystemResources(String name) throws IOException
Get an Enumeration of URLs to resources with a given name using the the system classloader. The enumeration firsts lists the resources with the given name that can be found by the bootstrap classloader followed by the resources with the given name that can be found on the classpath.
- Parameters:
name
- the name of the resource relative to the system classloader
- Returns:
- an Enumeration of URLs to the resources
- Throws:
IOException
- if I/O errors occur in the process
- Since:
- 1.2
public Class loadClass(String name) throws ClassNotFoundException
Load a class using this ClassLoader or its parent, without resolving it. CallsloadClass(name, false)
.Subclasses should not override this method but should override
findClass()
which is called by this method.
- Parameters:
name
- the name of the class relative to this ClassLoader
- Returns:
- the loaded class
- Throws:
ClassNotFoundException
- if the class cannot be found
protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
Load a class using this ClassLoader or its parent, possibly resolving it as well usingresolveClass()
. It first tries to find out if the class has already been loaded through this classloader by callingfindLoadedClass()
. Then it callsloadClass()
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 callingfindClass()
. Finally whenresolve
istrue
it also callsresolveClass()
on the newly loaded class.Subclasses should not override this method but should override
findClass()
which is called by this method.
- Parameters:
name
- the fully qualified name of the class to loadresolve
- whether or not to resolve the class
- Returns:
- the loaded class
- Throws:
ClassNotFoundException
- if the class cannot be found
protected final void resolveClass(Class c)
Links the class, if that has not already been done. Linking basically resolves all references to other classes made by this class.
- Parameters:
c
- the class to resolve
- Throws:
NullPointerException
- if c is null
public void setClassAssertionStatus(String name, boolean enabled)
Set the default assertion status for a class. This only affects the status of top-level classes, any other string is harmless.
- Parameters:
name
- the class to affectenabled
- true to set the default to enabled
- Throws:
NullPointerException
- if name is null
- Since:
- 1.4
public void setDefaultAssertionStatus(boolean enabled)
Set the default assertion status for classes loaded by this classloader, used unless overridden by a package or class request.
- Parameters:
enabled
- true to set the default to enabled
- Since:
- 1.4
public void setPackageAssertionStatus(String name, boolean enabled)
Set the default assertion status for packages, used unless overridden by a class request. This default also covers subpackages, unless they are also specified. The unnamed package should use null for the name.
- Parameters:
name
- the package (and subpackages) to affectenabled
- true to set the default to enabled
- Since:
- 1.4
protected final void setSigners(Class c, Object[] signers)
Helper to set the signers of a class. This should be called after defining the class.
- Parameters:
c
- the Class to set signers ofsigners
- the signers to set
- Since:
- 1.1