Class ClassLoader
- Direct Known Subclasses:
SecureClassLoader
public abstract class ClassLoader extends Object
ClassLoader is an abstract class that implements the common
infrastructure required by all class loaders. Android provides several
concrete implementations of the class, with
dalvik.system.PathClassLoader being the one typically used. Other
applications may implement subclasses of ClassLoader to provide
special ways for loading classes.
- See Also:
Class
-
Constructor Summary
Constructors Modifier Constructor Description protectedClassLoader()Constructs a new instance of this class with the system class loader as its parent.protectedClassLoader(ClassLoader parentLoader)Constructs a new instance of this class with the specified class loader as its parent. -
Method Summary
Modifier and Type Method Description voidclearAssertionStatus()Sets the default assertion status for this class loader tofalseand removes any package default and class assertion status settings.protected Class<?>defineClass(byte[] classRep, int offset, int length)Deprecated.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 specified protection domain to the new class.protected Class<?>defineClass(String name, ByteBuffer b, ProtectionDomain protectionDomain)Defines a new class with the specified name, byte code from the byte buffer and the optional protection domain.protected PackagedefinePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase)Defines and returns a newPackageusing the specified information.protected Class<?>findClass(String className)Overridden by subclasses, throws aClassNotFoundExceptionby default.protected StringfindLibrary(String libName)Returns the absolute path of the native library with the specified name, ornull.protected Class<?>findLoadedClass(String className)Returns the class with the specified name if it has already been loaded by the VM ornullif it has not yet been loaded.protected URLfindResource(String resName)Finds the URL of the resource with the specified name.protected Enumeration<URL>findResources(String resName)Finds an enumeration of URLs for the resource with the specified name.protected Class<?>findSystemClass(String className)Finds the class with the specified name, loading it using the system class loader if necessary.protected PackagegetPackage(String name)Returns the package with the specified name.protected Package[]getPackages()Returns all the packages known to this class loader.ClassLoadergetParent()Returns this class loader's parent.URLgetResource(String resName)Returns the URL of the resource with the specified name.InputStreamgetResourceAsStream(String resName)Returns a stream for the resource with the specified name.Enumeration<URL>getResources(String resName)Returns an enumeration of URLs for the resource with the specified name.static ClassLoadergetSystemClassLoader()Returns the system class loader.static URLgetSystemResource(String resName)Finds the URL of the resource with the specified name.static InputStreamgetSystemResourceAsStream(String resName)Returns a stream for the resource with the specified name.static Enumeration<URL>getSystemResources(String resName)Returns an enumeration of URLs for the resource with the specified name.Class<?>loadClass(String className)Loads the class with the specified name.protected Class<?>loadClass(String className, boolean resolve)Loads the class with the specified name, optionally linking it after loading.protected voidresolveClass(Class<?> clazz)Forces a class to be linked (initialized).voidsetClassAssertionStatus(String cname, boolean enable)Sets the assertion status of the class with the specified name.voidsetDefaultAssertionStatus(boolean enable)Sets the default assertion status for this class loader.voidsetPackageAssertionStatus(String pname, boolean enable)Sets the assertion status of the package with the specified name.protected voidsetSigners(Class<?> c, Object[] signers)Sets the signers of the specified class.
-
Constructor Details
-
ClassLoader
protected ClassLoader()Constructs a new instance of this class with the system class loader as its parent. -
ClassLoader
Constructs a new instance of this class with the specified class loader as its parent.- Parameters:
parentLoader- TheClassLoaderto use as the new class loader's parent.
-
-
Method Details
-
getSystemClassLoader
Returns the system class loader. This is the parent for newClassLoaderinstances and is typically the class loader used to start the application. -
getSystemResource
Finds the URL of the resource with the specified name. The system class loader's resource lookup algorithm is used to find the resource.- Parameters:
resName- the name of the resource to find.- Returns:
- the
URLobject for the requested resource ornullif the resource can not be found. - See Also:
Class.getResource(java.lang.String)
-
getSystemResources
Returns an enumeration of URLs for the resource with the specified name. The system class loader's resource lookup algorithm is used to find the resource.- Parameters:
resName- the name of the resource to find.- Returns:
- an enumeration of
URLobjects containing the requested resources. - Throws:
IOException- if an I/O error occurs.
-
getSystemResourceAsStream
Returns a stream for the resource with the specified name. The system class loader's resource lookup algorithm is used to find the resource. 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 for the resource or
null. - See Also:
Class.getResourceAsStream(java.lang.String)
-
defineClass
@Deprecated protected final Class<?> defineClass(byte[] classRep, int offset, int length) throws ClassFormatErrorDeprecated.UsedefineClass(String, byte[], int, int)instead.Constructs a new class from an array of bytes containing a class definition in class file format.- Parameters:
classRep- the memory image of a class file.offset- the offset intoclassRep.length- the length of the class file.- Returns:
- the
Classobject created from the specified subset of data inclassRep. - Throws:
ClassFormatError- ifclassRepdoes not contain a valid class.IndexOutOfBoundsException- ifoffset < 0,length < 0or ifoffset + lengthis greater than the length ofclassRep.
-
defineClass
protected final Class<?> defineClass(String className, byte[] classRep, int offset, int length) throws ClassFormatErrorConstructs a new class from an array of bytes containing a class definition in class file format.- Parameters:
className- the expected name of the new class, may benullif not known.classRep- the memory image of a class file.offset- the offset intoclassRep.length- the length of the class file.- Returns:
- the
Classobject created from the specified subset of data inclassRep. - Throws:
ClassFormatError- ifclassRepdoes not contain a valid class.IndexOutOfBoundsException- ifoffset < 0,length < 0or ifoffset + lengthis greater than the length ofclassRep.
-
defineClass
protected final Class<?> defineClass(String className, byte[] classRep, int offset, int length, ProtectionDomain protectionDomain) throws ClassFormatErrorConstructs a new class from an array of bytes containing a class definition in class file format and assigns the specified protection domain to the new class. If the provided protection domain isnullthen a default protection domain is assigned to the class.- Parameters:
className- the expected name of the new class, may benullif not known.classRep- the memory image of a class file.offset- the offset intoclassRep.length- the length of the class file.protectionDomain- the protection domain to assign to the loaded class, may benull.- Returns:
- the
Classobject created from the specified subset of data inclassRep. - Throws:
ClassFormatError- ifclassRepdoes not contain a valid class.IndexOutOfBoundsException- ifoffset < 0,length < 0or ifoffset + lengthis greater than the length ofclassRep.NoClassDefFoundError- ifclassNameis not equal to the name of the class contained inclassRep.
-
defineClass
protected final Class<?> defineClass(String name, ByteBuffer b, ProtectionDomain protectionDomain) throws ClassFormatErrorDefines a new class with the specified name, byte code from the byte buffer and the optional protection domain. If the provided protection domain isnullthen a default protection domain is assigned to the class.- Parameters:
name- the expected name of the new class, may benullif not known.b- the byte buffer containing the byte code of the new class.protectionDomain- the protection domain to assign to the loaded class, may benull.- Returns:
- the
Classobject created from the data inb. - Throws:
ClassFormatError- ifbdoes not contain a valid class.NoClassDefFoundError- ifclassNameis not equal to the name of the class contained inb.
-
findClass
Overridden by subclasses, throws aClassNotFoundExceptionby default. This method is called byloadClassafter the parentClassLoaderhas failed to find a loaded class of the same name.- Parameters:
className- the name of the class to look for.- Returns:
- the
Classobject that is found. - Throws:
ClassNotFoundException- if the class cannot be found.
-
findLoadedClass
Returns the class with the specified name if it has already been loaded by the VM ornullif it has not yet been loaded.- Parameters:
className- the name of the class to look for.- Returns:
- the
Classobject ornullif the requested class has not been loaded.
-
findSystemClass
Finds the class with the specified name, loading it using the system class loader if necessary.- Parameters:
className- the name of the class to look for.- Returns:
- the
Classobject with the requestedclassName. - Throws:
ClassNotFoundException- if the class can not be found.
-
getParent
Returns this class loader's parent.- Returns:
- this class loader's parent or
null.
-
getResource
Returns the URL of the resource with the specified name. This implementation first tries to use the parent class loader to find the resource; if this fails thenfindResource(String)is called to find the requested resource.- Parameters:
resName- the name of the resource to find.- Returns:
- the
URLobject for the requested resource ornullif the resource can not be found - See Also:
Class.getResource(java.lang.String)
-
getResources
Returns an enumeration of URLs for the resource with the specified name. This implementation first uses this class loader's parent to find the resource, then it callsfindResources(String)to get additional URLs. The returned enumeration contains theURLobjects of both find operations.- Parameters:
resName- the name of the resource to find.- Returns:
- an enumeration of
URLobjects for the requested resource. - Throws:
IOException- if an I/O error occurs.
-
getResourceAsStream
Returns a stream for the resource with the specified name. SeegetResource(String)for a description of the lookup algorithm used to find the resource.- Parameters:
resName- the name of the resource to find.- Returns:
- a stream for the resource or
nullif the resource can not be found - See Also:
Class.getResourceAsStream(java.lang.String)
-
loadClass
Loads the class with the specified name. Invoking this method is equivalent to callingloadClass(className, false).Note: In the Android reference implementation, the second parameter of
loadClass(String, boolean)is ignored anyway.- Parameters:
className- the name of the class to look for.- Returns:
- the
Classobject. - Throws:
ClassNotFoundException- if the class can not be found.
-
loadClass
Loads the class with the specified name, optionally linking it after loading. The following steps are performed:- Call
findLoadedClass(String)to determine if the requested class has already been loaded. - If the class has not yet been loaded: Invoke this method on the parent class loader.
- If the class has still not been loaded: Call
findClass(String)to find the class.
Note: In the Android reference implementation, the
resolveparameter is ignored; classes are never linked.- Parameters:
className- the name of the class to look for.resolve- Indicates if the class should be resolved after loading. This parameter is ignored on the Android reference implementation; classes are not resolved.- Returns:
- the
Classobject. - Throws:
ClassNotFoundException- if the class can not be found.
- Call
-
resolveClass
Forces a class to be linked (initialized). If the class has already been linked this operation has no effect.Note: In the Android reference implementation, this method has no effect.
- Parameters:
clazz- the class to link.
-
findResource
Finds the URL of the resource with the specified name. This implementation just returnsnull; it should be overridden in subclasses.- Parameters:
resName- the name of the resource to find.- Returns:
- the
URLobject for the requested resource.
-
findResources
Finds an enumeration of URLs for the resource with the specified name. This implementation just returns an emptyEnumeration; it should be overridden in subclasses.- Parameters:
resName- the name of the resource to find.- Returns:
- an enumeration of
URLobjects for the requested resource. - Throws:
IOException- if an I/O error occurs.
-
findLibrary
Returns the absolute path of the native library with the specified name, ornull. If this method returnsnullthen the virtual machine searches the directories specified by the system property "java.library.path".This implementation always returns
null.- Parameters:
libName- the name of the library to find.- Returns:
- the absolute path of the library.
-
getPackage
Returns the package with the specified name. Package information is searched in this class loader.- Parameters:
name- the name of the package to find.- Returns:
- the package with the requested name;
nullif the package can not be found.
-
getPackages
Returns all the packages known to this class loader.- Returns:
- an array with all packages known to this class loader.
-
definePackage
protected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) throws IllegalArgumentExceptionDefines and returns a newPackageusing the specified information. IfsealBaseisnull, the package is left unsealed. Otherwise, the package is sealed using this URL.- Parameters:
name- the name of the package.specTitle- the title of the specification.specVersion- the version of the specification.specVendor- the vendor of the specification.implTitle- the implementation title.implVersion- the implementation version.implVendor- the specification vendor.sealBase- the URL used to seal this package ornullto leave the package unsealed.- Returns:
- the
Packageobject that has been created. - Throws:
IllegalArgumentException- if a package with the specified name already exists.
-
setSigners
Sets the signers of the specified class. This implementation does nothing.- Parameters:
c- theClassobject for which to set the signers.signers- the signers forc.
-
setClassAssertionStatus
Sets the assertion status of the class with the specified name.Note: This method does nothing in the Android reference implementation.
- Parameters:
cname- the name of the class for which to set the assertion status.enable- the new assertion status.
-
setPackageAssertionStatus
Sets the assertion status of the package with the specified name.Note: This method does nothing in the Android reference implementation.
- Parameters:
pname- the name of the package for which to set the assertion status.enable- the new assertion status.
-
setDefaultAssertionStatus
public void setDefaultAssertionStatus(boolean enable)Sets the default assertion status for this class loader.Note: This method does nothing in the Android reference implementation.
- Parameters:
enable- the new assertion status.
-
clearAssertionStatus
public void clearAssertionStatus()Sets the default assertion status for this class loader tofalseand removes any package default and class assertion status settings.Note: This method does nothing in the Android reference implementation.
-
defineClass(String, byte[], int, int)instead.