Deep understanding of java reflection

To understand the principle of reflection, we must first understand what type information is. Java allows us to identify the information of objects and classes at runtime in two ways: one is the traditional RTTI, which assumes that we already know all the type information at compile time; The other is the reflection mechanism, which allows us to discover and use class information at run time.

1. Class object

To understand the working principle of RTTI in Java, you first need to know how type information is represented at run time. This is completed by class object, which contains class related information. Class object is used to create all "general" objects. Java uses class object to execute RTTI, even if you are performing operations like type conversion.

Each class will generate a corresponding class object, that is, it is saved in Class file. All classes are dynamically loaded into the JVM when they are used for the first time. When the program creates a reference to the static member of the class, the class will be loaded. Class objects are loaded only when needed, and static initialization is performed when the class is loaded.

The output result is:

The class loader will first check whether the class object of this class has been loaded. If it has not been loaded, the default class loader will find the corresponding class object according to the class name Class file.

If you want to use type information at runtime, you must obtain the reference of the class object of the object (such as the class base object) and use the function class Forname ("base") can do this, or use base class。 Note that it's interesting to use the function " Class "to create a reference to a class object, the class object will not be initialized automatically. Using forname() will initialize the class object automatically. The preparation for using classes generally includes the following three steps:

2. Check before type conversion

The compiler will check whether the type downward transformation is legal, and throw an exception if it is not. You can use instanceof to judge before downcasting the type.

3. Reflection: runtime class information

If you don't know the exact type of an object, RTTI can tell you, but there is a premise: this type must be known at compile time, so that you can use RTTI to identify it. Class and Java Lang.reflect class library supports reflection together. The class library contains field, method and constructor classes. The objects of these classes are created by the JVM at startup to represent the corresponding members in the unknown class. In this way, you can use the constructor to create a new object, use the get () and set () methods to obtain and modify the fields associated with the field object in the class, and use the invoke () method to call the methods associated with the method object. In addition, many convenient methods such as getfields (), getmethods (), and getconstructors () can be called to return arrays representing fields, methods, and constructor objects. In this way, the object information can be completely determined at run time without knowing anything about the class at compile time.

There is nothing magical about the reflection mechanism. When dealing with an object of unknown type through reflection, the JVM simply checks the object to see which specific class it belongs to. Therefore, that kind of Class must be available to the JVM, either on the local machine or from the network. So the real difference between RTTI and reflection is:

In the above, the get function of the class is called through the getreadmethod () method, and the set method of the class can be called through the getwritemethod () method. Generally speaking, we don't need reflection tools, but they are more useful in creating dynamic code. Reflection is used in Java to support other features, such as object serialization and JavaBeans.

4. Dynamic agent

Proxy mode is an object inserted to replace the "actual" object in order to provide additional or different operations. These operations involve communication with the "actual" object, so the proxy usually acts as an intermediary. Java's dynamic proxy is a step forward than the idea of proxy. It can dynamically create and process the calls to the proxy methods. All calls made on the dynamic agent will be redirected to a single call processor. Its job is to reveal the type of call and determine the corresponding strategy. The following is an example of a dynamic proxy:

Interface and implementation classes:

Dynamic proxy object processor:

Test class:

The output results are as follows:

By calling the proxy static method proxy Newproxyinstance () can create a dynamic proxy. This method needs to get a class loader, a list of interfaces you want the proxy to implement (not classes or abstract classes), and an implementation class of invocationhandler. The dynamic proxy can redirect all calls to the calling processor, so it usually calls the constructor of the processor to pass a reference to the "actual" object, so that the calling processor forwards the request when executing the mediation task.

reference resources:

1. Type information chapter of Java programming ideas - 4th Edition

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>