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"
1. What is reflection?
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.
2. What can reflection do?
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:
- Determine the class to which any object belongs at run time;
- Construct an object of any class at run time;
- Determine the member variables and methods of any class at run time;
- Obtain generic information at run time;
- Call the member variables and methods of any object at run time ;
- Annotations are processed at runtime;
- Generate dynamic proxy;
3. Reflection related API
- ##java .lang.Class
:Source of reflection
##java.lang.reflect.Method - :Method
- : Attribute
- : Constructor
…
- 4. Understanding of Class class
After the program passes the javac.exe command, it will generate one or more bytecode files (ending with .class). Then we use the java.exe command to interpret and run a certain bytecode file. Equivalent to loading a certain bytecode file into memory. This process is called
class loading. 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
.Loading: In our new object or use
Class.forName("Package name. Class")When the class loader (ClassLoader)
will load the class into memory and create a Class object
- Class.
- class
- getClass()
- ("Package name.Class");
The work done by the link is mainly to verify whether the bytecode is legal, allocate memory space for static and initialize it (not real initialization, just Give the corresponding type of variable a default value, such as int to 0, double to 0.0)
Unload:Unload from memory ( We don’t need to care about when to uninstall, it will be done by the JVM)
(3) Class loader
Class (CLASS) can only run after it is loaded into the JVM . When running the specified program, the JVM will load the compiled .class file into memory according to requirements and certain rules, and organize it into a complete Java application. This loading process is completed by the class loader, specifically, it is implemented by ClassLoader and its subclasses. The class loader itself is also a class. Its essence is to read the class file from the hard disk into the memory!
Classification of class loaders:-
BootStrap:
is mainly responsible for loading the core class library (java.lang.*
, etc.) and constructingExtClassLoader
andAPPClassLoader
; -
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
