public class

PathClassLoader

extends ClassLoader
java.lang.Object
   ↳ java.lang.ClassLoader
     ↳ dalvik.system.PathClassLoader

Class Overview

Provides a simple ClassLoader implementation that operates on a list of files and directories in the local file system, but does not attempt to load classes from the network. Android uses this class for its system class loader and for its application class loader(s).

Summary

Public Constructors
PathClassLoader(String path, ClassLoader parent)
Creates a PathClassLoader that operates on a given list of files and directories.
PathClassLoader(String path, String libPath, ClassLoader parent)
Creates a PathClassLoader that operates on two given lists of files and directories.
Protected Methods
Class<?> findClass(String name)
Finds a class.
String findLibrary(String libname)
Finds a native library.
URL findResource(String name)
Finds a resource.
Enumeration<URL> findResources(String resName)
Finds an enumeration of URLs for the resource with the specified name.
Package getPackage(String name)
Returns package information for the given package.
[Expand]
Inherited Methods
From class java.lang.ClassLoader
From class java.lang.Object

Public Constructors

public PathClassLoader (String path, ClassLoader parent)

Creates a PathClassLoader that operates on a given list of files and directories. This method is equivalent to calling with a {@code null value for the second argument (see description there).

Parameters
path the list of files and directories
parent the parent class loader

public PathClassLoader (String path, String libPath, ClassLoader parent)

Creates a PathClassLoader that operates on two given lists of files and directories. The entries of the first list should be one of the following:

  • Directories containing classes or resources.
  • JAR/ZIP/APK files, possibly containing a "classes.dex" file.
  • "classes.dex" files.
The entries of the second list should be directories containing native library files. Both lists are separated using the character specified by the "path.separator" system property, which, on Android, defaults to ":".

Parameters
path the list of files and directories containing classes and resources
libPath the list of directories containing native libraries
parent the parent class loader

Protected Methods

protected Class<?> findClass (String name)

Finds a class. This method is called by loadClass() after the parent ClassLoader has failed to find a loaded class of the same name.

Parameters
name The name of the class to search for, in a human-readable form like "java.lang.String" or "java.net.URLClassLoader$3$1".
Returns
  • the Class object representing the class
Throws
ClassNotFoundException if the class cannot be found

protected String findLibrary (String libname)

Finds a native library. This method is called after the parent ClassLoader has failed to find a native library of the same name.

Parameters
libname The name of the library to find
Returns
  • the complete path of the library, or null if the library is not found.

protected URL findResource (String name)

Finds a resource. This method is called by getResource() after the parent ClassLoader has failed to find a loaded resource of the same name.

Parameters
name The name of the resource to find
Returns
  • the location of the resource as a URL, or null if the resource is not found.

protected Enumeration<URL> findResources (String resName)

Finds an enumeration of URLs for the resource with the specified name.

Parameters
resName the name of the resource to find.
Returns
  • an enumeration of URL objects for the requested resource.

protected Package getPackage (String name)

Returns package information for the given package. Unfortunately, the PathClassLoader doesn't really have this information, and as a non-secure ClassLoader, it isn't even required to, according to the spec. Yet, we want to provide it, in order to make all those hopeful callers of myClass.getPackage().getName() happy. Thus we construct a Package object the first time it is being requested and fill most of the fields with dummy values. The Package object is then put into the ClassLoader's Package cache, so we see the same one next time. We don't create Package objects for null arguments or for the default package.

There a limited chance that we end up with multiple Package objects representing the same package: It can happen when when a package is scattered across different JAR files being loaded by different ClassLoaders. Rather unlikely, and given that this whole thing is more or less a workaround, probably not worth the effort.

Parameters
name the name of the class
Returns
  • the package information for the class, or null if there is not package information available for it