- java.lang.Object
-
- java.lang.ClassLoader
-
- Direct Known Subclasses:
SecureClassLoader
public abstract class ClassLoader extends Object
ClassLoaders are used to dynamically load, link and install classes into a running image.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ClassLoader()
Constructs a new instance of this class with the system class loader as its parent.protected
ClassLoader(ClassLoader parentLoader)
Constructs a new instance of this class with the given class loader as its parent.protected
ClassLoader(String classLoaderName, ClassLoader parentLoader)
Constructs a class loader with the specified name and the given class loader as its parent.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
clearAssertionStatus()
Clears the default, package and class assertion status of a classloaderprotected Class<?>
defineClass(byte[] classRep, int offset, int length)
Deprecated.Use defineClass(String, byte[], int, int)protected Class<?>
defineClass(String className, byte[] classRep, int offset, int length)
Constructs a new class from an array of bytes containing a class definition in class file format.protected Class<?>
defineClass(String className, byte[] classRep, int offset, int length, ProtectionDomain protectionDomain)
Constructs a new class from an array of bytes containing a class definition in class file format and assigns the new class to the specified protection domain.protected Class<?>
defineClass(String name, ByteBuffer buffer, ProtectionDomain domain)
Constructs a new class from an array of bytes containing a class definition in class file format and assigns the new class to the specified protection domain.protected Package
definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase)
Define a new Package using the specified information.protected Class<?>
findClass(String className)
Overridden by subclasses, by default throws ClassNotFoundException.protected Class<?>
findClass(String moduleName, String className)
Overridden by subclasses that support the loading from modules.protected String
findLibrary(String libName)
Answers the absolute path of the file containing the library associated with the given name, or null.protected Class<?>
findLoadedClass(String className)
Attempts to find and return a class which has already been loaded by the virtual machine.protected URL
findResource(String resName)
Answers an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm.protected URL
findResource(String moduleName, String resName)
Answers an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm.protected Enumeration<URL>
findResources(String resName)
Answers an Enumeration of URL which can be used to access the resources described by resName, using the class loader's resource lookup algorithm.protected Class<?>
findSystemClass(String className)
Attempts to load a class using the system class loader.protected Object
getClassLoadingLock(String className)
Answers the lock object for class loading in parallel.Package
getDefinedPackage(String name)
Answers a package of given name defined by this ClassLoader.Package[]
getDefinedPackages()
Answers all the packages defined by this classloader.String
getName()
Get the name given when creating the ClassLoader instance, or null if none was provided.protected Package
getPackage(String name)
Deprecated.Use getDefinedPackage(String)protected Package[]
getPackages()
Answers all the packages known to this class loader.ClassLoader
getParent()
Returns the specified ClassLoader's parent.static ClassLoader
getPlatformClassLoader()
Return the Platform classloader.URL
getResource(String resName)
Answers an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm.InputStream
getResourceAsStream(String resName)
Answers a stream on a resource found by looking up resName using the class loader's resource lookup algorithm.Enumeration<URL>
getResources(String resName)
Answers an Enumeration of URL which can be used to access the resources described by resName, using the class loader's resource lookup algorithm.static ClassLoader
getSystemClassLoader()
Convenience operation to obtain a reference to the system class loader.static URL
getSystemResource(String resName)
Answers an URL specifying a resource which can be found by looking up resName using the system class loader's resource lookup algorithm.static InputStream
getSystemResourceAsStream(String resName)
Answers a stream on a resource found by looking up resName using the system class loader's resource lookup algorithm.static Enumeration<URL>
getSystemResources(String resName)
Answers an Enumeration of URL containing all resources which can be found by looking up resName using the system class loader's resource lookup algorithm.Module
getUnnamedModule()
Answers the unnamed Module of this class loader.boolean
isRegisteredAsParallelCapable()
Indicating if this class loader is registered as parallel capable or notClass<?>
loadClass(String className)
Invoked by the Virtual Machine when resolving class references.protected Class<?>
loadClass(String className, boolean resolveClass)
Attempts to load the typeclassName
in the running VM, optionally linking the type after a successful load.protected static boolean
registerAsParallelCapable()
Attempts to register the ClassLoader as being capable of parallel class loading.protected void
resolveClass(Class<?> clazz)
Forces a class to be linked (initialized).Stream<URL>
resources(String name)
Answers a stream of URL which can be used to access the resources described by name, using the class loader's resource lookup algorithm.void
setClassAssertionStatus(String cname, boolean enable)
Sets the assertion status of a class.void
setDefaultAssertionStatus(boolean enable)
Sets the default assertion status of a classloadervoid
setPackageAssertionStatus(String pname, boolean enable)
Sets the assertion status of a package.protected void
setSigners(Class<?> c, Object[] signers)
Sets the signers of a class.
-
-
-
Constructor Detail
-
ClassLoader
protected ClassLoader()
Constructs a new instance of this class with the system class loader as its parent.- Throws:
SecurityException
- if a security manager exists and it does not allow the creation of new ClassLoaders.
-
ClassLoader
protected ClassLoader(ClassLoader parentLoader)
Constructs a new instance of this class with the given class loader as its parent.- Parameters:
parentLoader
- ClassLoader the ClassLoader to use as the new class loaders parent.- Throws:
SecurityException
- if a security manager exists and it does not allow the creation of new ClassLoaders.
-
ClassLoader
protected ClassLoader(String classLoaderName, ClassLoader parentLoader)
Constructs a class loader with the specified name and the given class loader as its parent.- Parameters:
classLoaderName
- name of this ClassLoader or null if not named.parentLoader
- ClassLoader the ClassLoader to use as the new class loaders parent.- Throws:
IllegalArgumentException
- if the name of this class loader is empty.SecurityException
- if a security manager exists and it does not allow the creation of new ClassLoaders.- Since:
- 9
-
-
Method Detail
-
defineClass
@Deprecated(forRemoval=false, since="1.1") protected final Class<?> defineClass(byte[] classRep, int offset, int length) throws ClassFormatError
Deprecated.Use defineClass(String, byte[], int, int)Constructs a new class from an array of bytes containing a class definition in class file format.- Parameters:
classRep
- byte[] a memory image of a class file.offset
- int the offset into the classRep.length
- int the length of the class file.- Returns:
- the newly defined Class
- Throws:
ClassFormatError
- when the bytes are invalid
-
defineClass
protected final Class<?> defineClass(String className, byte[] classRep, int offset, int length) throws ClassFormatError
Constructs a new class from an array of bytes containing a class definition in class file format.- Parameters:
className
- java.lang.String the name of the new classclassRep
- byte[] a memory image of a class fileoffset
- int the offset into the classReplength
- int the length of the class file- Returns:
- the newly defined Class
- Throws:
ClassFormatError
- when the bytes are invalid
-
defineClass
protected final Class<?> defineClass(String className, byte[] classRep, int offset, int length, ProtectionDomain protectionDomain) throws ClassFormatError
Constructs a new class from an array of bytes containing a class definition in class file format and assigns the new class to the specified protection domain.- Parameters:
className
- java.lang.String the name of the new class.classRep
- byte[] a memory image of a class file.offset
- int the offset into the classRep.length
- int the length of the class file.protectionDomain
- ProtectionDomain the protection domain this class should belong to.- Returns:
- the newly defined Class
- Throws:
ClassFormatError
- when the bytes are invalid
-
findClass
protected Class<?> findClass(String className) throws ClassNotFoundException
Overridden by subclasses, by default throws ClassNotFoundException. This method is called by loadClass() after the parent ClassLoader has failed to find a loaded class of the same name.- Parameters:
className
- String the name of the class to search for.- Returns:
- java.lang.Class the class or null.
- Throws:
ClassNotFoundException
- always, unless overridden.
-
findClass
protected Class<?> findClass(String moduleName, String className)
Overridden by subclasses that support the loading from modules. When the moduleName is null, the default implementation invokes findClass(String), attempts to find the class and return it, or returns null in case of ClassNotFoundException. When the moduleName is not null, the default implementation returns null. This method is called by Class.forName(Module module, String name).- Parameters:
moduleName
- the name of the module from which the class is to be loaded.className
- String the name of the class to search for.- Returns:
- java.lang.Class the class or null.
-
findLoadedClass
protected final Class<?> findLoadedClass(String className)
Attempts to find and return a class which has already been loaded by the virtual machine. Note that the class may not have been linked and the caller should call resolveClass() on the result if necessary.- Parameters:
className
- String the name of the class to search for.- Returns:
- java.lang.Class the class or null.
-
findSystemClass
protected final Class<?> findSystemClass(String className) throws ClassNotFoundException
Attempts to load a class using the system class loader. Note that the class has already been linked.- Parameters:
className
- String the name of the class to search for.- Returns:
- java.lang.Class the class which was loaded.
- Throws:
ClassNotFoundException
- if the class can not be found.
-
getParent
public final ClassLoader getParent()
Returns the specified ClassLoader's parent.- Returns:
- java.lang.ClassLoader the class or null.
- Throws:
SecurityException
- if a security manager exists and it does not allow the parent loader to be retrieved.
-
getResource
public URL getResource(String resName)
Answers an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm. The default behavior is just to return null.- Parameters:
resName
- String the name of the resource to find.- Returns:
- URL the location of the resource.
- See Also:
Class.getResource(java.lang.String)
-
getResources
public Enumeration<URL> getResources(String resName) throws IOException
Answers an Enumeration of URL which can be used to access the resources described by resName, using the class loader's resource lookup algorithm.- Parameters:
resName
- String the name of the resource to find.- Returns:
- Enumeration the locations of the resources.
- Throws:
IOException
- when an error occurs
-
getResourceAsStream
public InputStream getResourceAsStream(String resName)
Answers a stream on a resource found by looking up resName using the class loader's resource lookup algorithm. The default behavior is just to return null.- Parameters:
resName
- String the name of the resource to find.- Returns:
- InputStream a stream on the resource or null.
- See Also:
Class.getResourceAsStream(java.lang.String)
-
getPlatformClassLoader
public static ClassLoader getPlatformClassLoader()
Return the Platform classloader. If a security manager exists and it does not allow access a SecurityException will be thrown.- Returns:
- the platformClassLoader
- Throws:
SecurityException
- if access to the platform classloader is denied
-
getName
public String getName()
Get the name given when creating the ClassLoader instance, or null if none was provided.- Returns:
- name of the class loader or null
- Since:
- 9
-
getSystemClassLoader
public static ClassLoader getSystemClassLoader()
Convenience operation to obtain a reference to the system class loader. The system class loader is the parent of any newClassLoader
objects created in the course of an application and will normally be the sameClassLoader
as that used to launch an application.- Returns:
- java.lang.ClassLoader the system classLoader.
- Throws:
SecurityException
- if a security manager exists and it does not permit the caller to access the system class loader.
-
getSystemResource
public static URL getSystemResource(String resName)
Answers an URL specifying a resource which can be found by looking up resName using the system class loader's resource lookup algorithm.- Parameters:
resName
- String the name of the resource to find.- Returns:
- URL a URL specifying a system resource or null.
- See Also:
Class.getResource(java.lang.String)
-
getSystemResources
public static Enumeration<URL> getSystemResources(String resName) throws IOException
Answers an Enumeration of URL containing all resources which can be found by looking up resName using the system class loader's resource lookup algorithm.- Parameters:
resName
- String the name of the resource to find.- Returns:
- Enumeration an Enumeration of URL containing the system resources
- Throws:
IOException
- when an error occurs
-
getSystemResourceAsStream
public static InputStream getSystemResourceAsStream(String resName)
Answers a stream on a resource found by looking up resName using the system class loader's resource lookup algorithm. Basically, the contents of the java.class.path are searched in order, looking for a path which matches the specified resource.- Parameters:
resName
- the name of the resource to find.- Returns:
- a stream on the resource or null.
- See Also:
Class.getResourceAsStream(java.lang.String)
-
getUnnamedModule
public final Module getUnnamedModule()
Answers the unnamed Module of this class loader.- Returns:
- the unnamed Module of this class loader
-
loadClass
public Class<?> loadClass(String className) throws ClassNotFoundException
Invoked by the Virtual Machine when resolving class references. Equivalent to loadClass(className, false);- Parameters:
className
- String the name of the class to search for.- Returns:
- java.lang.Class the Class object.
- Throws:
ClassNotFoundException
- If the class could not be found.
-
loadClass
protected Class<?> loadClass(String className, boolean resolveClass) throws ClassNotFoundException
Attempts to load the typeclassName
in the running VM, optionally linking the type after a successful load.- Parameters:
className
- String the name of the class to search for.resolveClass
- boolean indicates if class should be resolved after loading.- Returns:
- java.lang.Class the Class object.
- Throws:
ClassNotFoundException
- If the class could not be found.
-
registerAsParallelCapable
protected static boolean registerAsParallelCapable()
Attempts to register the ClassLoader as being capable of parallel class loading. This requires that all superclasses must also be parallel capable.- Returns:
- True if the ClassLoader successfully registers as parallel capable, false otherwise.
- See Also:
ClassLoader
-
getClassLoadingLock
protected Object getClassLoadingLock(String className)
Answers the lock object for class loading in parallel. If this ClassLoader object has been registered as parallel capable, a dedicated object associated with this specified class name is returned. Otherwise, current ClassLoader object is returned.- Parameters:
className
- String name of the to be loaded class- Returns:
- the lock for class loading operations
- Throws:
NullPointerException
- if registered as parallel capable and className is null- See Also:
ClassLoader
-
resolveClass
protected final void resolveClass(Class<?> clazz)
Forces a class to be linked (initialized). If the class has already been linked this operation has no effect.- Parameters:
clazz
- Class the Class to link.- Throws:
NullPointerException
- if clazz is null.- See Also:
Class.getResource(java.lang.String)
-
findResource
protected URL findResource(String resName)
Answers an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm. The default behavior is just to return null. This should be implemented by a ClassLoader.- Parameters:
resName
- String the name of the resource to find.- Returns:
- URL the location of the resource.
-
findResources
protected Enumeration<URL> findResources(String resName) throws IOException
Answers an Enumeration of URL which can be used to access the resources described by resName, using the class loader's resource lookup algorithm. The default behavior is just to return an empty Enumeration.- Parameters:
resName
- String the name of the resource to find.- Returns:
- Enumeration the locations of the resources.
- Throws:
IOException
- when an error occurs
-
findLibrary
protected String findLibrary(String libName)
Answers the absolute path of the file containing the library associated with the given name, or null. If null is answered, the system searches the directories specified by the system property "java.library.path".- Parameters:
libName
- String the name of the library to find.- Returns:
- String the library file name or null.
-
getDefinedPackage
public final Package getDefinedPackage(String name)
Answers a package of given name defined by this ClassLoader.- Parameters:
name
- The name of the package to find- Returns:
- The package requested
-
getDefinedPackages
public final Package[] getDefinedPackages()
Answers all the packages defined by this classloader.- Returns:
- Array of Package objects or zero length array if no package is defined
-
getPackage
@Deprecated(forRemoval=false, since="9") protected Package getPackage(String name)
Deprecated.Use getDefinedPackage(String)Attempt to locate the requested package. If no package information can be located, null is returned.- Parameters:
name
- The name of the package to find- Returns:
- The package requested, or null
-
getPackages
protected Package[] getPackages()
Answers all the packages known to this class loader.- Returns:
- All the packages known to this classloader
-
definePackage
protected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) throws IllegalArgumentException
Define a new Package using the specified information.- Parameters:
name
- The name of the packagespecTitle
- The title of the specification for the PackagespecVersion
- The version of the specification for the PackagespecVendor
- The vendor of the specification for the PackageimplTitle
- The implementation title of the PackageimplVersion
- The implementation version of the PackageimplVendor
- The specification vendor of the PackagesealBase
- The URL used to seal the Package, if null the Package is not sealed- Returns:
- The Package created
- Throws:
IllegalArgumentException
- if the Package already exists
-
setSigners
protected final void setSigners(Class<?> c, Object[] signers)
Sets the signers of a class.- Parameters:
c
- The Class objectsigners
- The signers for the class
-
setClassAssertionStatus
public void setClassAssertionStatus(String cname, boolean enable)
Sets the assertion status of a class.- Parameters:
cname
- Class nameenable
- Enable or disable assertion- Since:
- 1.4
-
setPackageAssertionStatus
public void setPackageAssertionStatus(String pname, boolean enable)
Sets the assertion status of a package.- Parameters:
pname
- Package nameenable
- Enable or disable assertion- Since:
- 1.4
-
setDefaultAssertionStatus
public void setDefaultAssertionStatus(boolean enable)
Sets the default assertion status of a classloader- Parameters:
enable
- Enable or disable assertion- Since:
- 1.4
-
clearAssertionStatus
public void clearAssertionStatus()
Clears the default, package and class assertion status of a classloader- Since:
- 1.4
-
defineClass
protected final Class<?> defineClass(String name, ByteBuffer buffer, ProtectionDomain domain) throws ClassFormatError
Constructs a new class from an array of bytes containing a class definition in class file format and assigns the new class to the specified protection domain.- Parameters:
name
- java.lang.String the name of the new class.buffer
- a memory image of a class file.domain
- the protection domain this class should belong to.- Returns:
- the newly defined Class
- Throws:
ClassFormatError
- when the bytes are invalid- Since:
- 1.5
-
findResource
protected URL findResource(String moduleName, String resName) throws IOException
Answers an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm. By default, return null, unless moduleName is null, in which case return findResource(resName). This should be implemented by a ClassLoader.- Parameters:
moduleName
- String the module nameresName
- String the name of the resource to find.- Returns:
- URL the location of the resource.
- Throws:
IOException
- when an error occurs
-
resources
public Stream<URL> resources(String name)
Answers a stream of URL which can be used to access the resources described by name, using the class loader's resource lookup algorithm.- Parameters:
name
- - the name of the resource to find- Returns:
- a stream of the resources
- Throws:
NullPointerException
- when name is null
-
isRegisteredAsParallelCapable
public final boolean isRegisteredAsParallelCapable()
Indicating if this class loader is registered as parallel capable or not- Returns:
- true if this is registered as parallel capable, otherwise false is returned
-
-