Partager via


ClassLoader Class

Definition

A class loader is an object that is responsible for loading classes.

[Android.Runtime.Register("java/lang/ClassLoader", DoNotGenerateAcw=true)]
public abstract class ClassLoader : Java.Lang.Object
[<Android.Runtime.Register("java/lang/ClassLoader", DoNotGenerateAcw=true)>]
type ClassLoader = class
    inherit Object
Inheritance
ClassLoader
Derived
Attributes

Remarks

A class loader is an object that is responsible for loading classes. The class 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 <tt>Class</tt> object contains a Class#getClassLoader() 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.

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 delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. The virtual machine's built-in class loader, called the "bootstrap class loader", does not itself have a parent but may serve as the parent of a ClassLoader instance.

Class loaders that support concurrent loading of classes are known as <em>parallel capable</em> class loaders and are required to register themselves at their class initialization time by invoking the #registerAsParallelCapable <tt>ClassLoader.registerAsParallelCapable</tt> 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. <br> 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 <tt>loadClass</tt> methods).

Normally, the Java virtual machine loads classes from the local file system in a platform-dependent manner. For example, on UNIX systems, the virtual machine loads classes from the directory defined by the CLASSPATH environment variable.

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(String, byte[], int, int) <tt>defineClass</tt> converts an array of bytes into an instance of class Class. Instances of this newly defined class can be created using Class#newInstance <tt>Class.newInstance</tt>.

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 <tt>loadClass</tt> 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:

<blockquote>

ClassLoader loader&nbsp;= new NetworkClassLoader(host,&nbsp;port);
              Object main&nbsp;= loader.loadClass("Main", true).newInstance();
                  &nbsp;.&nbsp;.&nbsp;.

</blockquote>

The network class loader subclass must define the methods #findClass <tt>findClass</tt> 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 <tt>defineClass</tt> to create a class instance. A sample implementation is:

<blockquote>

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
                        &nbsp;.&nbsp;.&nbsp;.
                    }
                }

</blockquote>

<h3> "name">Binary names</h3>

Any class name provided as a String parameter to methods in ClassLoader must be a binary name as defined by <cite>The Java&trade; Language Specification</cite>.

Examples of valid class names include: <blockquote>

"java.lang.String"
              "javax.swing.JSpinner$DefaultEditor"
              "java.security.KeyStore$Builder$FileBuilder$1"
              "java.net.URLClassLoader$3$1"

</blockquote>

Added in 1.0.

Java documentation for java.lang.ClassLoader.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

ClassLoader()

Creates a new class loader using the ClassLoader returned by the method #getSystemClassLoader() <tt>getSystemClassLoader()</tt> as the parent class loader.

ClassLoader(ClassLoader)

Creates a new class loader using the specified parent class loader for delegation.

ClassLoader(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Parent

Returns the parent class loader for delegation.

PeerReference (Inherited from Object)
SystemClassLoader

Returns the system class loader for delegation.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

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.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
DefineClass(Byte[], Int32, Int32)
Obsolete.

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

DefineClass(String, Byte[], Int32, Int32, ProtectionDomain)

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

DefineClass(String, Byte[], Int32, Int32)

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

DefineClass(String, ByteBuffer, ProtectionDomain)

Converts a java.nio.ByteBuffer <tt>ByteBuffer</tt> into an instance of class Class, with an optional ProtectionDomain.

DefinePackage(String, String, String, String, String, String, String, URL)

Defines a package by name in this ClassLoader.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

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

(Inherited from Object)
FindClass(String)

Finds the class with the specified binary name.

FindLibrary(String)

Returns the absolute path name of a native library.

FindLoadedClass(String)

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.

FindResource(String)

Finds the resource with the given name.

FindResources(String)

Returns an enumeration of java.net.URL <tt>URL</tt> objects representing all the resources with the given name.

FindSystemClass(String)

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

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetPackage(String)

Returns a Package that has been defined by this class loader or any of its ancestors.

GetPackages()

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

GetResource(String)

Finds the resource with the given name.

GetResourceAsStream(String)

Returns an input stream for reading the specified resource.

GetResources(String)

Finds all the resources with the given name.

GetSystemResource(String)

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

GetSystemResourceAsStream(String)

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

GetSystemResources(String)

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

JavaFinalize()

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

(Inherited from Object)
LoadClass(String, Boolean)

Loads the class with the specified binary name.

LoadClass(String)

Loads the class with the specified binary name.

LoadClassAsync(String, Boolean)
LoadClassAsync(String)
Notify()

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

(Inherited from Object)
NotifyAll()

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

(Inherited from Object)
RegisterAsParallelCapable()

Registers the caller as parallel capable.

ResolveClass(Class)

Links the specified class.

SetClassAssertionStatus(String, Boolean)

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

SetDefaultAssertionStatus(Boolean)

Sets the default assertion status for this class loader.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetPackageAssertionStatus(String, Boolean)

Sets the package default assertion status for the named package.

SetSigners(Class, Object[])

Sets the signers of a class.

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

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

(Inherited from Object)
Wait(Int64, Int32)

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

(Inherited from Object)
Wait(Int64)

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

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to