Reflection mechanism and application scenario of java learning
preface:
*** Version x or retrofit, and the early view annotation framework all use java reflection mechanism more or less.
What is the java reflection mechanism?
Java reflection mechanism is to know all the properties and methods of any class in the running state; Any method of any object can be called; This function of dynamically obtaining and dynamically calling object methods is called java reflection mechanism.
What functions does the reflection mechanism provide?
Java reflection mechanism class:
Implementation of java reflection mechanism: 1.) Get class object
2.) get the summary information of the class object
3.) get the properties, methods, constructors, etc. of the class object
4.) dynamic generation of class objects
5.) dynamic call function
6.) get generic types through reflection mechanism
For example, the following structure
Get generic type
Getcomponenttype concrete implementation
6.) obtain annotation information through reflection mechanism
Here, we focus on obtaining the annotation information of method as an example
Application scenario of reflection mechanism:
Advantages and disadvantages of reflection mechanism:
Advantages: runtime type judgment, dynamic class loading, and dynamic proxy using reflection.
Disadvantages: performance is a problem. Reflection is equivalent to a series of interpretation operations to tell the JVM what to do. The performance is much slower than that of direct java code.
Summary:
The reflection mechanism of Java is rarely used in the normal business development process, but it is widely used in the construction of some basic frameworks. After a brief summary today, there are still many unknown knowledge to be used and supplemented in the future.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.