The principle and significance of java reflection

table of Contents

Preface

1. The specific functions of java reflection

2. Class class, Class object


Preface

The key to reflection : dynamically learn (load) uncompiled classes at runtime to achieve java dynamics .

Basics : First of all, we need to know how those classes can be used when java is running? It is because the JVM class loads the bytecode file of the class, forms the relevant information (attributes, methods, reference objects, etc.) of the class and saves it in the corresponding method area. This process can be referred to as " class compilation" . That is, most of the classes that Java can use at runtime are compiled by classes that have passed the compilation period.

Question : Sometimes, java programs need to use classes that have not been compiled locally when they are running. What should I do? Answer: Use the tool provided by java developers --- reflection. What effect can be achieved? We can load, detect, and use classes that are completely unknown during compilation at runtime.


1. The specific functions of java reflection

The following content comes from Baidu Encyclopedia, but it is quite easy to understand.

The Java reflection mechanism mainly provides the following functions: 

  • Judge the class to which any object belongs at runtime;
  • Construct an object of any class at runtime;
  • Judge the member variables and methods of any class at runtime;
  • Call any object's method at runtime; generate dynamic proxy.

Reflection realizes the dynamics of Java

Sometimes we say that a language is very dynamic, and sometimes we distinguish between dynamic and static techniques and practices. We are catchy dynamic binding, dynamic linking, dynamic loading, etc. However, the term "dynamic" does not have an absolute and universally applicable strict definition. Sometimes it is even like when object-oriented was introduced into the field of programming, each person has his own trumpet and each has his own tune.
  Generally speaking, when the developer community talks about dynamic languages, one of the definitions generally agreed is: "When the program is running, it is allowed to change the program structure or variable types. This language is called a dynamic language." From this point of view, Perl, Python, and Ruby are dynamic languages, while C++, Java, and C# are not dynamic languages.
  Although Java is not a dynamic language under this definition and classification , it has a very prominent dynamic correlation mechanism : Reflection . This word means "reflection, reflection, reflection". When used in Java, it means that we can load, detect, and use classes that are completely unknown during compilation. In other words, a Java program can load a class whose name is only known at runtime, learn its complete structure (but not including the definition of methods), and generate its object entities, or set values ​​for its fields, or call up its methods. This ability to "see through the class" (the ability of the program to examine itself) is called introspection (introspection, introspection, introspection). Reflection and introspection are two terms that are often mentioned together.
  How can Java make the above-mentioned dynamic characteristics? This is a far-reaching topic, and this article only briefly introduces some concepts. The most important part of the entire page is to introduce Reflection APIs, that is, to let readers know how to explore the structure of the class, how to generate an entity for a "class whose name is only known at runtime", set values ​​for its fields, and call its methods. This article will talk about java.lang.Class, as well as the Method, Field, Constructor and other classes in java.lang.reflect.


2. Class class, Class object

The classes required by the reflection mechanism mainly include the Class class in the java.lang package and the Constructor, Field, Method and Parameter classes in the java.lang.reflect package.

The Class class is a special class, which is the basis of the reflection mechanism. The object of the Class class represents the class or interface in the running Java program, that is, when any class is loaded, the class file (byte Code file) is read into memory at the same time, a java.lang.Class object is automatically created for it . The Class class has no public construction method, and its object is created by calling the defineClass() method in the class loader when the JVM loads the class. Therefore , a Class object cannot be created explicitly . Through this Class object, other information about the object can be obtained.

Guess you like

Origin blog.csdn.net/Longtermevolution/article/details/107906115