Java reflection mechanism analysis (2)-Class Loader, classloader

Source: Internet
Author: User

Java reflection mechanism analysis (2)-Class Loader, classloader
The previous blog briefly introduced some knowledge related to the java reflection mechanism, so ClassLoader is one of them. This blog provides a more in-depth understanding of ClassLoader. To understand this, you need to know that the Class is inseparable from the ClassLoader, because the information required by the ClassLoader is provided by it. Class types will be introduced in the next blog.
Introduction ClassLoader is the object responsible for loading classes. It is used to load the requested classes into the memory or into the Jvm based on the class information provided by the Jvm request. In addition, the Class Object of each Class (note that the Class is of the Class type) holds a reference of the corresponding ClassLoader. You can use the getClassLoader () method of the Class Object. The class corresponds to its ClassLoader, so the class can only be loaded through its corresponding ClassLoader.
Note:: The Class Object of the array Class is not created by the Class loader, but automatically created as needed during Java runtime. The Class loader of the array Class is composed of Class. getClassLoader () returns the same class loader as its element type. If the element type is of the basic type, no class loader exists for the array class.
Classification JVM generates three ClassLoader, Bootstrap ClassLoader, Extension ClassLoader, and App ClassLoader at runtime.
Bootstrap ClassLoader: it is written in C ++ and is the built-in loader of JVM. Its name is null. It is used to load the core class library, that is, the class library under lib. First of all, the String class must be the core class of java. Let's take it as an example:

public static void main(String[] args){String a="x";System.out.println(a.getClass().getClassLoader());}
The code is used to obtain the name output of the ClassLoader corresponding to String loading. The result is NULL.
Extension ClassLoader: load the class library under lib/ext.
App ClassLoader: load the class library in Classpath.
As we have said before, each Class object will hold a reference to the corresponding ClassLoader. Each ClassLoader object also holds a reference to the Parent ClassLoader. Note that the Parent ClassLoader mentioned here is not a familiar inheritance relation, not a Parent class !! First, we need to know that the ClassLoader object is mentioned here, that is, the Parent ClassLoader is actually a reference to an object. The following figure shows the hierarchical relationship between ClassLoader objects:


Here we can do an experiment to understand this level of relationship. The Code is as follows:
public static void main(String[] args){ClassLoader c =TestClassLoader.class.getClassLoader();do {System.out.println(c.getClass().getName());c=c.getParent();}while(c!=null);}}
The output result is:
Sun. misc. Launcher $ AppClassLoader
Sun. misc. Launcher $ ExtClassLoader
We have learned an important point in the hierarchical relationship of the parent-child load mechanism: the reference relationship between the loader objects. The referenced object is called the parent loader of the referenced object, which can be obtained through the getParent () method. The Parent-parent load mechanism is based on the reference hierarchy. That is, when a ClassLoader receives a request, it does not directly load the corresponding class, but asks whether the referenced ClassLoader can be loaded, the parent ClassLoader will ask whether the referenced ClassLoader has loaded the class. Only when none of the parent ClassLoader has loaded the applied class can the original ClassLoader load the applied class.
The text is unclear!


The Parent-parent loading mechanism ensures security to some extent, because as long as the top-level ClassLoader can load things, it will not give the lower-level ClassLoader a chance to load. This ensures that some custom destructive classes are not loaded into the Jvm core.
Conclusion: ClassLoader is hard to understand. It refers to the hierarchy of objects and the parent loader. The other is the parent-parent load mechanism. Here is a video for your reference.
Java reflection mechanism

During Java runtime, can I know the attribute methods of a class and call and modify them? Can an object know its class and call its method? The answer is yes. This kind of dynamic mechanism for obtaining information and calling methods is called "reflection" in Java ).
The Java reflection mechanism mainly provides the following functions:
Determine the class to which any object belongs at runtime;
Construct any class object at runtime;
Judge the member variables and methods of any class at runtime;
Call the methods of any object at runtime.
Reflection is a key feature of Java as a dynamic (or quasi-dynamic) language. This mechanism allows the program to obtain the internal information of any class with a known name through Reflection APIs at runtime, including its modifiers (such as public and static), superclass (such as Object), implements interfaces (such as Serializable), also includes all information about fields and methods, and can change fields content or call methods at runtime.
Generally speaking, when talking about dynamic languages, the developer community roughly agrees with the definition: "When a program is running, the program structure or variable type can be changed. This language is called dynamic language ".
In JDK, the following classes are used to implement the Java reflection mechanism. These classes are located in the java. lang. reflect package:
Class: represents a Class;
Field Class: represents the member variables of the class (member variables are also called the attributes of the class );
Method class: indicates the Method of the class;
Constructor class: Constructor class;
Array class: provides static methods for dynamically creating arrays and accessing Array elements;

For more information, see references. I think this document is good.
Reference: 2.16.hi.baidu.com/#/detail/24992875

Java Reflection (JAVA Reflection) Mechanism

I started to score again. Is it interesting?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.