Home >Java >javaTutorial >Let you understand the JAVA reflection mechanism (summary sharing)
This article brings you relevant knowledge about java, which mainly introduces issues related to the reflection mechanism, including what reflection is, what reflection can do, reflection-related APIs, etc., I hope everyone has to help.
Recommended study: "java Learning Tutorial"
A very important concept in Java development is the Java reflection mechanism, which is also one of the important features of Java.
The concept of reflection was first proposed by Smith in 1982. It mainly refers to the ability of a program to access, detect and modify its own state or behavior. Through reflection, private methods and private properties can be called. Some frameworks also use the principle of reflection.
Reflection (reflection) is regarded as the key to dynamic language. The reflection mechanism allows the program to obtain the internal information of any
class during execution with the help of the Reflection API, and can directly operate it Internal properties and methods of any object.
A class has multiple components, such as member variables, methods, constructors, etc. Reflection is to load the class and dissect the various components of the class.
Java's reflection mechanism knows the basic structure of the class. This ability to detect the structure of the Java class is called the "self-examination" of the Java class. For example, in eclipse, with one click, the compilation tool will automatically list all the methods and properties that can be used by the object for the user to choose. This uses the principle of Java reflection to detect and self-examine the objects we create.
Reflection can do:
:Source of reflection
…. A class loaded into memory is called a runtime class, and this runtime class is used as an instance of Class. In other words, an instance of Class corresponds to a runtime class.
The runtime classes loaded into memory will be cached for a certain period of time. Within this time, we can obtain this runtime class in different ways
.When the class loader (ClassLoader)
will load the class into memory and create a Class object
(3) Class loader
BootStrap:
is mainly responsible for loading the core class library (java.lang.*
, etc.) and constructing ExtClassLoader
and APPClassLoader
; ExtClassLoader:
Mainly responsible for loading some extended jar packages in the jre/lib/ext directory; AppClassLoader:
Mainly responsible for Load the main function class of the application (the java file written by yourself is loaded by this class loader); System.out.println("app:" + System.getProperty("java.class.path")); System.out.println("ext:" + System.getProperty("java.ext.dirs")); System.out.println("----bootstrap---"); String[] str = System.getProperty("sun.boot.class.path").split(";"); for (String s : str) { System.out.println(s); }
Parental delegation (dispatch) mechanism:
When a file like Hello.class is to be loaded. Regardless of our custom class loader, we will first check whether it has been loaded in AppClassLoader. If so, there is no need to load it again. If not, the parent loader will be obtained, and then the loadClass method of the parent loader will be called. In the same way, the parent class will first check whether it has been loaded, and if not, go up. Pay attention to this recursive process. Until it reaches the Bootstrap classLoader, it is checking whether it has been loaded and will not choose to load it by itself. Until BootstrapClassLoader, there is no parent loader. At this time, I start to consider whether I can load it. If I can't load it, I will sink to the child loader to load it, all the way to the bottom layer. If no loader can load it, it will Throws ClassNotFoundException. So does anyone have the following questions?
Why should we design this mechanism?
One advantage of this design is that if someone wants to replace the system-level class: String.java. Tamper with its implementation. Under this mechanism, these system classes have been loaded by Bootstrap classLoader (why? Because when a class needs to be loaded, the first thing to try to load is BootstrapClassLoader), so other class loaders have not Loading again when the opportunity arises prevents the implantation of dangerous code to a certain extent.
Recommended study: "java tutorial"
The above is the detailed content of Let you understand the JAVA reflection mechanism (summary sharing). For more information, please follow other related articles on the PHP Chinese website!