Additional top-level class in compilation unit: CompoundEnumeration.
ClassLoader
is an abstract class. Given the binary name of a class, a class loader should attempt to
locate or generate data that constitutes a definition for the class. A
typical strategy is to transform the name into a file name and then read a
"class file" of that name from a file system.
Every Class
object contains a reference
to the ClassLoader
that defined
it.
Class
objects for array classes are not created by class
loaders, but are created automatically as required by the Java runtime.
The class loader for an array class, as returned by Class#getClassLoader()
is the same as the class loader for its element
type; if the element type is a primitive type, then the array class has no
class loader.
Applications implement subclasses of ClassLoader
in order to
extend the manner in which the Java virtual machine dynamically loads
classes.
Class loaders may typically be used by security managers to indicate security domains.
In addition to loading classes, a class loader is also responsible for
locating resources. A resource is some data (a ".class
" file,
configuration data, or an image for example) that is identified with an
abstract '/'-separated path name. Resources are typically packaged with an
application or library so that they can be located by code in the
application or library. In some cases, the resources are included so that
they can be located by other libraries.
The ClassLoader
class uses a delegation model to search for
classes and resources. Each instance of ClassLoader
has an
associated parent class loader. When requested to find a class or
resource, a ClassLoader
instance will usually delegate the search
for the class or resource to its parent class loader before attempting to
find the class or resource itself.
Class loaders that support concurrent loading of classes are known as
parallel capable class
loaders and are required to register themselves at their class initialization
time by invoking the ClassLoader.
method. Note that the ClassLoader
class is registered as parallel
capable by default. However, its subclasses still need to register themselves
if they are parallel capable.
In environments in which the delegation model is not strictly
hierarchical, class loaders need to be parallel capable, otherwise class
loading can lead to deadlocks because the loader lock is held for the
duration of the class loading process (see loadClass
methods).
Bootstrap class loader.
It is the virtual machine's built-in class loader, typically represented
as null
, and does not have a parent.
Platform class loader.
The platform class loader is responsible for loading the
platform classes. Platform classes include Java SE platform APIs,
their implementation classes and JDK-specific run-time classes that are
defined by the platform class loader or its ancestors.
The platform class loader can be used as the parent of a ClassLoader
instance.
To allow for upgrading/overriding of modules defined to the platform class loader, and where upgraded modules read modules defined to class loaders other than the platform class loader and its ancestors, then the platform class loader may have to delegate to other class loaders, the application class loader for example. In other words, classes in named modules defined to class loaders other than the platform class loader and its ancestors may be visible to the platform class loader.
System class loader. It is also known as application class loader and is distinct from the platform class loader. The system class loader is typically used to define classes on the application class path, module path, and JDK-specific tools. The platform class loader is the parent or an ancestor of the system class loader, so the system class loader can load platform classes by delegating to its parent.
Normally, the Java virtual machine loads classes from the local file
system in a platform-dependent manner.
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
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 invokes the loadClass
method of
the class loader that originally created the class.
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 methods findClass
and loadClassData
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 findClass(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 . . . } }
Any class name provided as a String
parameter to methods in
ClassLoader
must be a binary name as defined by
The Java Language Specification.
Examples of valid class names include:
"java.lang.String" "javax.swing.JSpinner$DefaultEditor" "java.security.KeyStore$Builder$FileBuilder$1" "java.net.URLClassLoader$3$1"
Any package name provided as a String
parameter to methods in
ClassLoader
must be either the empty string (denoting an unnamed package)
or a fully qualified name as defined by
The Java Language Specification.
resolveClass(Class)
Modifier and Type | Class and Description |
---|---|
private static class | ClassLoader.
Encapsulates the set of parallel capable loader types. |
Modifier and Type | Field and Description |
---|---|
pack-priv final Object | |
pack-priv Map | |
private final ArrayList | |
private volatile ConcurrentHashMap | |
private boolean | |
private final ProtectionDomain | |
private final NativeLibraries | |
private final String | |
private final String | |
private static final Certificate[] | |
private final ConcurrentHashMap | |
private Map | |
private final ConcurrentHashMap | |
private final ConcurrentHashMap | |
private final ClassLoader | |
private static volatile ClassLoader | |
private final Module |
Access | Constructor and Description |
---|---|
private | |
protected | ClassLoader(String
class loader name; or name, ClassLoader null if not namedthe parent class loader parent)Creates a new class loader of the specified name and using the specified parent class loader for delegation. |
protected | ClassLoader(ClassLoader
The parent class loader parent)Creates a new class loader using the specified parent class loader for delegation. |
protected | ClassLoader()
Creates a new class loader using the |
Modifier and Type | Method and Description |
---|---|
pack-priv void | |
private void | |
pack-priv static void | |
private static Void | |
private static Void | |
private static boolean | |
private void | |
public void | clearAssertionStatus()
Sets the default assertion status for this class loader to
|
private boolean | compareCerts(Certificate[] pcerts, Certificate[] certs)
check to make sure the certs for the new class (certs) are the same as the certs for the first class inserted in the package (pcerts) |
pack-priv ConcurrentHashMap | createOrGetClassLoaderValueMap()
Returns the ConcurrentHashMap used as a storage for ClassLoaderValue(s) associated with this ClassLoader, creating it if it doesn't already exist. |
protected final Class | Returns: TheClass object that was created from the specified
class dataThe bytes that make up the class data. The bytes in positions
b, int off through off+len-1 should have the format
of a valid class file as defined by
The Java Virtual Machine Specification.The start offset in off, int b of the class dataThe length of the class data len)
Deprecated
since 1.1. Replaced by
defineClass(String, byte[], int, int)
Converts an array of bytes into an instance of class |
protected final Class | Returns: TheClass object that was created from the specified
class data.The expected binary name of the class, or
name, byte[] null if not knownThe bytes that make up the class data. The bytes in positions
b, int off through off+len-1 should have the format
of a valid class file as defined by
The Java Virtual Machine Specification.The start offset in off, int b of the class dataThe length of the class data len)Converts an array of bytes into an instance of class |
protected final Class | Returns: TheClass object created from the data,
and ProtectionDomain .The expected binary name of the class, or
name, byte[] null if not knownThe bytes that make up the class data. The bytes in positions
b, int off through off+len-1 should have the format
of a valid class file as defined by
The Java Virtual Machine Specification.The start offset in off, int b of the class dataThe length of the class data len, ProtectionDomain The protectionDomain)ProtectionDomain of the classConverts an array of bytes into an instance of class |
protected final Class | Returns: TheClass object created from the data,
and ProtectionDomain .The expected binary name. of the class, or
name, ByteBuffer null if not knownThe bytes that make up the class data. The bytes from positions
b, ProtectionDomain b.position() through b.position() + b.limit() -1
should have the format of a valid class file as defined by
The Java Virtual Machine Specification.The protectionDomain)ProtectionDomain of the class, or null .Converts a |
pack-priv static native Class | defineClass0(ClassLoader
the defining loader loader, Class<?> nest host of the Class to be defined lookup, String the binary name or name, byte[] null if not findableclass bytes b, int the start offset in off, int b of the class bytesthe length of the class bytes len, ProtectionDomain protection domain pd, boolean initialize the class initialize, int flags flags, Object class data classData)Defines a class of the given flags via Lookup.defineClass. |
pack-priv static native Class | defineClass1(ClassLoader loader, String name, byte[] b, int off, int len, ProtectionDomain pd, String source)
|
pack-priv static native Class | defineClass2(ClassLoader loader, String name, ByteBuffer b, int off, int len, ProtectionDomain pd, String source)
|
private String | |
pack-priv Package | |
pack-priv Package | definePackage(String
package name name, Module module m)Defines a Package of the given name and module This method does not throw IllegalArgumentException. |
protected Package | Returns: The newly definedPackage objectThe package name name, String The specification title specTitle, String The specification version specVersion, String The specification vendor specVendor, String The implementation title implTitle, String The implementation version implVersion, String The implementation vendor implVendor, URL If not sealBase)null , then this package is sealed with
respect to the given code source URL
object. Otherwise, the package is not sealed.Defines a package by name in this |
pack-priv boolean | Returns: The desired assertion status of the specified class.The fully qualified class name of the class whose desired
assertion status is being queried. className)Returns the assertion status that would be assigned to the specified class if it were to be initialized at the time this method is invoked. |
private static native Class | |
pack-priv static Class | findBootstrapClassOrNull(String name)
Returns a class loaded by the bootstrap class loader; or return null if not found. |
protected Class | Returns: The resultingClass objectThe binary name of the class name)Finds the class with the specified binary name. |
protected Class | Returns: The resultingClass object, or null
if the class could not be found.The module name; or moduleName, String null to find the class in the
unnamed module for this
class loaderThe binary name of the class name)Finds the class with the given binary name in a module defined to this class loader. |
protected String | Returns: The absolute path of the native libraryThe library name libname)Returns the absolute path name of a native library. |
protected final Class | Returns: TheClass object, or null if the class has
not been loadedThe binary name of the class name)Returns the class with the given binary name if this loader has been recorded by the Java virtual machine as an initiating loader of a class with that binary name. |
private final native Class | |
pack-priv static long | |
protected URL | Returns: A URL to the resource;null if the resource could not be
found, a URL could not be constructed to locate the resource,
access to the resource is denied by the security manager, or
there isn't a module of the given name defined to the class
loader.The module name; or moduleName, String null to find a resource in the
unnamed module for this
class loaderThe resource name name)Returns a URL to a resource in a module defined to this class loader. |
protected URL | Returns: URL object for reading the resource; null if
the resource could not be found, a URL could not be
constructed to locate the resource, the resource is in a package
that is not opened unconditionally, or access to the resource is
denied by the security manager.The resource name name)Finds the resource with the given name. |
protected Enumeration | Returns: An enumeration ofURL objects for
the resource. If no resources could be found, the enumeration
will be empty. Resources for which a URL cannot be
constructed, are in a package that is not opened unconditionally,
or access to the resource is denied by the security manager,
are not returned in the enumeration.The resource name name)Returns an enumeration of |
protected final Class | Returns: TheClass object for the specified name The binary name of the class name)Finds a class with the specified binary name, loading it if necessary. |
pack-priv static ClassLoader | |
pack-priv static ClassLoader | |
pack-priv static ClassLoader | |
protected Object | Returns: the lock for class loading operationsThe name of the to-be-loaded class className)Returns the lock object for class loading operations. |
public final Package | Returns: ThePackage of the given name that has been defined
by this class loader, or null if not foundThe package name name)Returns a |
public final Package[] | Returns: The array ofPackage objects that have been defined by
this class loader; or a zero length array if no package has been
defined by this class loader.Returns all of the |
public String | Returns: name of this class loader; ornull if
this class loader is not named.Returns the name of this class loader or |
private NamedPackage | |
protected Package | Returns: ThePackage of the given name that has been defined by
this class loader or its ancestors, or null if not found.The package name name)
Deprecated
since 9. If multiple class loaders delegate to each other and define classes
with the same package name, and one such loader relies on the lookup
behavior of
getPackage to return a Package from
a parent loader, then the properties exposed by the Package
may not be as expected in the rest of the program.
Finds a package by name in this class loader and its ancestors. |
protected Package[] | Returns: The array ofPackage objects that have been defined by
this class loader and its ancestorsReturns all of the |
public final ClassLoader | |
public static ClassLoader | |
public URL | Returns: URL object for reading the resource; null if
the resource could not be found, a URL could not be
constructed to locate the resource, the resource is in a package
that is not opened unconditionally, or access to the resource is
denied by the security manager.The resource name name)Finds the resource with the given name. |
public InputStream | Returns: An input stream for reading the resource;null if the
resource could not be found, the resource is in a package that
is not opened unconditionally, or access to the resource is
denied by the security manager.The resource name name)Returns an input stream for reading the specified resource. |
public Enumeration | Returns: An enumeration ofURL objects for the
resource. If no resources could be found, the enumeration will
be empty. Resources for which a URL cannot be
constructed, are in a package that is not opened
unconditionally, or access to the resource is denied by the
security manager, are not returned in the enumeration.The resource name name)Finds all the resources with the given name. |
public static ClassLoader | |
public static URL | Returns: AURL to the resource; null if the resource could not be found, a URL could not be
constructed to locate the resource, the resource is in a package
that is not opened unconditionally or access to the resource is
denied by the security manager.The resource name name)Find a resource of the specified name from the search path used to load classes. |
public static InputStream | Returns: An input stream for reading the resource;null if the
resource could not be found, the resource is in a package that
is not opened unconditionally, or access to the resource is
denied by the security manager.The resource name name)Open for reading, a resource of the specified name from the search path used to load classes. |
public static Enumeration | Returns: An enumeration ofURL objects for
the resource. If no resources could be found, the enumeration
will be empty. Resources for which a URL cannot be
constructed, are in a package that is not opened unconditionally,
or access to the resource is denied by the security manager,
are not returned in the enumeration.The resource name name)Finds all resources of the specified name from the search path used to load classes. |
public final Module | Returns: The unnamed Module for this class loaderReturns the unnamed |
private void | |
pack-priv static synchronized ClassLoader | |
pack-priv boolean | |
public final boolean | Returns: true if this class loader is parallel capable,
otherwise false .Returns |
public Class | Returns: The resultingClass objectThe binary name of the class name)Loads the class with the specified binary name. |
protected Class | Returns: The resultingClass objectThe binary name of the class name, boolean If resolve)true then resolve the classLoads the class with the specified binary name. |
pack-priv final Class | Returns: The resultingClass object in a module defined by
this class loader, or null if the class could not be found.The module module, String The binary name of the class name)Loads the class with the specified binary name in a module defined to this class loader. |
pack-priv static NativeLibrary | |
pack-priv static NativeLibrary | |
pack-priv final String | |
private static String | nameAndId(ClassLoader ld)
If the defining loader has a name explicitly set then '<loader-name>' @<id> If the defining loader has no name then <qualified-class-name> @<id> If it's built-in loader then omit `@<id>` as there is only one instance. |
pack-priv String | |
private static boolean | |
pack-priv Stream | |
private void | |
private ProtectionDomain | |
protected static boolean | Returns: true if the caller is successfully registered as
parallel capable and false if otherwise.Registers the caller as parallel capable. |
private static boolean | |
private static native void | |
private void | |
protected final void | |
public Stream | Returns: A stream of resourceURL objects. If no
resources could be found, the stream will be empty. Resources
for which a URL cannot be constructed, are in a package
that is not opened unconditionally, or access to the resource
is denied by the security manager, will not be in the stream.The resource name name)Returns a stream whose elements are the URLs of all the resources with the given name. |
private static native AssertionStatusDirectives | |
public void | setClassAssertionStatus(String
The fully qualified class name of the top-level class whose
assertion status is to be set. className, boolean true if the named class is to have assertions
enabled when (and if) it is initialized, false if the
class is to have assertions disabled.Sets the desired assertion status for the named top-level class in this class loader and any nested classes contained therein. |
public void | setDefaultAssertionStatus(boolean
true if classes loaded by this class loader will
henceforth have assertions enabled by default, false
if they will have assertions disabled by default.Sets the default assertion status for this class loader. |
public void | setPackageAssertionStatus(String
The name of the package whose package default assertion status
is to be set. A packageName, boolean null value indicates the unnamed
package that is "current"
(see section 7.4.2 of
The Java Language Specification.)true if classes loaded by this classloader and
belonging to the named package or any of its subpackages will
have assertions enabled by default, false if they will
have assertions disabled by default.Sets the package default assertion status for the named package. |
protected final void | setSigners(Class<?>
The c, Object[] Class objectThe signers for the class signers)Sets the signers of a class. |
private Package | |
private boolean | trySetObjectField(String name, Object obj)
Attempts to atomically set a volatile field in this object. |