JVM principle (9) ClassLoader source code analysis and instance analysis

This article is reproduced from: http://www.javaxxz.com/thread-359190-1-1.html

public abstract class ClassLoader extends Object

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.

A class loader is such an object used to load a class, ClassLoader is an abstract class, given a binary name, the class loader should try to locate (find the location of the class definition data) and generate (dynamic proxies are dynamically generated ) constitutes the data defined by this class, a typical strategy is to replace a name with a file name, and then read the bytecode contained in the file from the file system.  
Every Class object contains a 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. 
The class object for the array class is not created by the class loader, but automatically created by the java runtime when needed [Review: http ://blog.csdn.net/wzq6578702/article/details/79370662
For the class loader of the array, the type class loader returned by Class.getClassLoader() is the same as the type class loader of the array element, if the element of the array is a primitive type,  
then this array has no class loader.  
Example:

public class MyTest15 {
    public static void main(String[] args) {
        String[] strings = new String[2];
        System.out.println(strings.getClass().getClassLoader());

        MyTest15[] myTest15s = new MyTest15[2];
        System.out.println(myTest15s.getClass().getClassLoader());

        int [] ints = new int[2];
        System.out.println(ints.getClass().getClassLoader());

        HashMap[] maps = new HashMap[2];
        System.out.println(maps.getClass().getClassLoader());
    }
}

output:

null [This null refers to the root class loader]
sun.misc.Launcher$AppClassLoader@18b4aac2 [The array loader is the same as the array element loader]
null [refers to no class loader]
null [This null refers to is the root 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. Classloaders 
generally use a security manager to ensure 
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. 
ClassLoader uses a The delegation model is used to find classes and resources. Each instance of ClassLoader has a parent class loader associated with it. When a request is to find a class or resource, the instance of ClassLoader finds the class or resource in itself. It will delegate its parent class loader to load classes or discover resources before. The built-in class loader of the virtual machine becomes the "startup class loader", which itself has no parent, but it can act as a parent of a class loader. 
Class loaders that support concurrent loading of classes are known as parallel capable class loaders and are required to register themselves at their class initialization time by invoking the ClassLoader.registerAsParallelCapable 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. 
If the class loader supports concurrency, it is a concurrent class loader. The concurrent class loader requires registration through the ClassLoader.registerAsParallelCapable method during the initialization of the class. The current ClassLoader is registered by default. It is parallel, but its subclasses also need to be registered if they can be loaded in parallel.  
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 methods). 
In an environment where the delegation model is not strictly hierarchical, the class loaders need to be parallel, otherwise the class loading process will deadlock, because the class loading process is holding a lock (see the getClass method) 
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. 
Typically the Java virtual machine loads classes in a platform-dependent manner. The form loads classes from the local file system. For example, on UNIX systems, the virtual machine loads classes through the path of 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 converts an array of bytes into an instance of class Class. Instances of this newly defined class can be created using Class.newInstance. 
Then, some classes do not come from a file, they may come from the network or they are built by the application itself (dynamic proxy), the defineClass method will convert a byte array into an instance of the Class class, this newly defined class can be Create an object of a class through Class.newInstance.  
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 method of the class loader that originally created the class 
. The constructor or method of the object may refer to other classes. In order to determine the other classes it refers to, the Java virtual machine resolves it by calling loadClass.  For example
, an application could create a network class loader to download class files from a server. Sample code might look like: 
: 
ClassLoader loader = new NetworkClassLoader(host, port); 
Object main = loader.loadClass("Main", true).newInstance(); 

The network class loader subclass must define the methods findClass 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 to create a class instance. A sample implementation is: 
network The subclass of the loader must define the loadClassData and findClass methods to load the class from the network. Once the bytecode of the class is downloaded, the class will be built. It needs to use the defineClass method to create an instance of the class, an instance of the implementation: 
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
          . . .
     }
}

Binary names  Any class name provided as a String parameter to methods in ClassLoader
must be a binary name as defined by The Java™ Language Specification.  , called the binary name, which is formulated by the Java Virtual Machine specification. Examples of valid class names include:  binary name example "java.lang.String" [String class] 
 
 

 

"javax.swing.JSpinner<span class="MathJax" id="MathJax-Element-4-Frame" tabindex="0" data-mathml="DefaultEditor" [DefaultEditor is an inner class in JSpinner] "java.security. KeyStore" role="presentation" style="outline: 0px; position: relative;">DefaultEditor" [DefaultEditor is an inner class in JSpinner] "java.security.KeyStore Default E di tor" [Default E ditor is in JS pinner's inner class] " ​​java . security . Key Store Builder<span class="MathJax" id="MathJax-Element-5-Frame" tabindex="0" data-mathml="FileBuilder" role="presentation " style="outline: 0px; position: relative;">FileBuilder File Builder 1" [the first anonymous inner class in the inner class of the inner class Builder in the KeyStore] 
"java.net.URLClassLoader<span class="MathJax" id="MathJax-Element-6-Frame" tabindex="0" data-mathml="3" role="presentation" style="outline: 0px; position: relative;"> 3 3 1" [the first anonymous inner class in the third anonymous inner class in URLClassLoader]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325561043&siteId=291194637