DelegateLastClassLoader

public final class DelegateLastClassLoader
extends PathClassLoader

java.lang.Object
   ↳ java.lang.ClassLoader
     ↳ dalvik.system.BaseDexClassLoader
       ↳ dalvik.system.PathClassLoader
         ↳ dalvik.system.DelegateLastClassLoader


A ClassLoader implementation that implements a delegate last lookup policy. For every class or resource this loader is requested to load, the following lookup order is employed:

  • The boot classpath is always searched first
  • Then, the list of dex files associated with this classloaders's dexPath is searched.
  • Finally, this classloader will delegate to the specified parent.

Summary

Public constructors

DelegateLastClassLoader(String dexPath, ClassLoader parent)

Equivalent to calling DelegateLastClassLoader(java.lang.String, java.lang.String, java.lang.ClassLoader, boolean) with librarySearchPath = null, delegateResourceLoading = true.

DelegateLastClassLoader(String dexPath, String librarySearchPath, ClassLoader parent)

Equivalent to calling DelegateLastClassLoader(java.lang.String, java.lang.String, java.lang.ClassLoader, boolean) with delegateResourceLoading = true.

DelegateLastClassLoader(String dexPath, String librarySearchPath, ClassLoader parent, boolean delegateResourceLoading)

Creates a DelegateLastClassLoader that operates on a given dexPath and a librarySearchPath.

Public methods

URL getResource(String name)

Finds the resource with the given name.

Enumeration<URL> getResources(String name)

Finds all the resources with the given name.

Protected methods

Class<?> loadClass(String name, boolean resolve)

Loads the class with the specified binary name.

Inherited methods

Class<?> findClass(String name)

Finds the class with the specified binary name.

String findLibrary(String name)

Returns the absolute path name of a native library.

URL findResource(String name)

Finds the resource with the given name.

Enumeration<URL> findResources(String name)

Returns an enumeration of URL objects representing all the resources with the given name.

Package getPackage(String name)

This method is deprecated. See ClassLoader.getPackage(String)

String toString()

Returns a string representation of the object.

void clearAssertionStatus()

Sets the default assertion status for this class loader to false and discards any package defaults or class assertion status settings associated with the class loader.

final Class<?> defineClass(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain)

Converts an array of bytes into an instance of class Class, with a given ProtectionDomain.

final Class<?> defineClass(String name, ByteBuffer b, ProtectionDomain protectionDomain)

Converts a ByteBuffer into an instance of class Class, with the given ProtectionDomain.

final Class<?> defineClass(byte[] b, int off, int len)

This method was deprecated in API level 15. Replaced by defineClass(String, byte[], int, int)

final Class<?> defineClass(String name, byte[] b, int off, int len)

Converts an array of bytes into an instance of class Class.

Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase)

Defines a package by name in this ClassLoader.

Class<?> findClass(String name)

Finds the class with the specified binary name.

String findLibrary(String libname)

Returns the absolute path name of a native library.

final Class<?> findLoadedClass(String 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.

URL findResource(String name)

Finds the resource with the given name.

Enumeration<URL> findResources(String name)

Returns an enumeration of URL objects representing all the resources with the given name.

final Class<?> findSystemClass(String name)

Finds a class with the specified binary name, loading it if necessary.

Package getPackage(String name)

This method was deprecated in API level 35. 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. For example, the Package will only expose annotations from the package-info.class file defined by the parent loader, even if annotations exist in a package-info.class file defined by a child loader.

Package[] getPackages()

Returns all of the Packages that have been defined by this class loader and its ancestors.

final ClassLoader getParent()

Returns the parent class loader for delegation.

URL getResource(String name)

Finds the resource with the given name.

InputStream getResourceAsStream(String name)

Returns an input stream for reading the specified resource.

Enumeration<URL> getResources(String name)

Finds all the resources with the given name.

static ClassLoader getSystemClassLoader()

Returns the system class loader.

static URL getSystemResource(String name)

Find a resource of the specified name from the search path used to load classes.

static InputStream getSystemResourceAsStream(String name)

Open for reading, a resource of the specified name from the search path used to load classes.

static Enumeration<URL> getSystemResources(String name)

Finds all resources of the specified name from the search path used to load classes.

Class<?> loadClass(String name, boolean resolve)

Loads the class with the specified binary name.

Class<?> loadClass(String name)

Loads the class with the specified binary name.

static boolean registerAsParallelCapable()

Registers the caller as #isRegisteredAsParallelCapable() parallel capable.

final void resolveClass(Class<?> c)

Links the specified class.

void setClassAssertionStatus(String className, boolean enabled)

Sets the desired assertion status for the named top-level class in this class loader and any nested classes contained therein.

void setDefaultAssertionStatus(boolean enabled)

Sets the default assertion status for this class loader.

void setPackageAssertionStatus(String packageName, boolean enabled)

Sets the package default assertion status for the named package.

final void setSigners(Class<?> c, Object[] signers)

Sets the signers of a class.

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public constructors

DelegateLastClassLoader

Added in API level 27
public DelegateLastClassLoader (String dexPath, 
                ClassLoader parent)

Equivalent to calling DelegateLastClassLoader(java.lang.String, java.lang.String, java.lang.ClassLoader, boolean) with librarySearchPath = null, delegateResourceLoading = true.

Parameters
dexPath String

parent ClassLoader

DelegateLastClassLoader

Added in API level 27
public DelegateLastClassLoader (String dexPath, 
                String librarySearchPath, 
                ClassLoader parent)

Equivalent to calling DelegateLastClassLoader(java.lang.String, java.lang.String, java.lang.ClassLoader, boolean) with delegateResourceLoading = true.

Parameters
dexPath String

librarySearchPath String

parent ClassLoader

DelegateLastClassLoader

Added in API level 29
public DelegateLastClassLoader (String dexPath, 
                String librarySearchPath, 
                ClassLoader parent, 
                boolean delegateResourceLoading)

Creates a DelegateLastClassLoader that operates on a given dexPath and a librarySearchPath. The dexPath should consist of one or more of the following, separated by File.pathSeparator, which is ":" on Android.

  • JAR/ZIP/APK files, possibly containing a "classes.dex" file as well as arbitrary resources.
  • Raw ".dex" files (not inside a zip file).
Unlike PathClassLoader, this classloader will attempt to locate classes (or resources) using the following lookup order.
  • The boot classpath is always searched first.
  • Then, the list of dex files contained in dexPath is searched./li>
  • Lastly, this classloader will delegate to the specified parent.
Note that this is in contrast to other standard classloaders that follow the delegation model. In those classloaders, the parent is always searched first. librarySearchPath specifies one more directories containing native library files, separated by File.pathSeparator.

Parameters
dexPath String: the list of jar/apk files containing classes and resources, delimited by File.pathSeparator, which defaults to ":" on Android. This value cannot be null.

librarySearchPath String: the list of directories containing native libraries, delimited by File.pathSeparator; may be null.

parent ClassLoader: the parent class loader. May be null for the boot classloader.

delegateResourceLoading boolean: whether to delegate resource loading to the parent if the resource is not found. This does not affect class loading delegation.

Public methods

getResource

Added in API level 27
public URL getResource (String name)

Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a '/'-separated path name thatf identifies the resource.

Parameters
name String: The resource name

Returns
URL 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.

getResources

Added in API level 27
public Enumeration<URL> getResources (String name)

Finds all the resources with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a /-separated path name that identifies the resource.

Parameters
name String: The resource name

Returns
Enumeration<URL> An enumeration of URL 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.

Throws
IOException

Protected methods

loadClass

Added in API level 27
protected Class<?> loadClass (String name, 
                boolean resolve)

Loads the class with the specified binary name. The default implementation of this method searches for classes in the following order:

  1. Invoke findLoadedClass(java.lang.String) to check if the class has already been loaded.

  2. Invoke the loadClass method on the parent class loader. If the parent is null the class loader built into the virtual machine is used, instead.

  3. Invoke the findClass(java.lang.String) method to find the class.

If the class was found using the above steps, and the resolve flag is true, this method will then invoke the resolveClass(java.lang.Class) method on the resulting Class object.

Subclasses of ClassLoader are encouraged to override findClass(java.lang.String), rather than this method.

Parameters
name String: The binary name of the class

resolve boolean: If true then resolve the class

Returns
Class<?> The resulting Class object