Detailed explanation of Java method reflection implementation principle

Blogger said: the java reflection mechanism is to know all the properties and methods of any class in the running state; For any object, you can call any of its methods and properties; This function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of Java language. In this article, Zhan Xiaolang analyzes the implementation principle (source code) of java reflection mechanism. Interested students can spend a few minutes reading this article.

text

Method reflection instance

Through Java's reflection mechanism, you can call any method of an object during runtime. If you use this method extensively, will there be performance or memory risks? In order to thoroughly understand the reflection mechanism of the method, we can only start with the underlying code!

Method get

Call getdeclaraedmethod of class class to get the method object method with specified method name and parameters.

getDeclaredMethod

The privategetdeclaredmethods method obtains the method list declared in the class from the cache or JVM, and the searchmethods method will find a method object matching the name and parameters from the returned method list.

searchMethods

If a matching method is found, a new copy will be copied back, that is, method Copy() method.

The method object returned by each call of getdeclaredmethod method is actually a new object, and the root attribute of the new object points to the original method object. If you need to call frequently, you'd better cache the method object.

privateGetDeclaredMethods

Get the list of methods declared in the class from the cache or JVM. The implementation is as follows:

The reflectiondata () method is implemented as follows:

Here is an important data structure reflectiondata, which is used to cache the following attribute data of classes read from the JVM:

It can be seen from the implementation of the reflectiondata () method that the reflectiondata object is of softreference type, which means that it may be recycled when memory is tight. However, the timing of recycling can also be controlled through the - XX: softreflupolicymspermb parameter. It will be recycled as soon as GC occurs. If the reflectiondata is recycled and the reflection method is executed, You can only recreate such an object through the newreflectiondata method. The implementation of the newreflectiondata method is as follows:

Through unsafe The compareandswapobject method resets the reflectiondata field; In the privategetdeclaredmethods method, if the reflectiondata object obtained through reflectiondata() is not empty, try to obtain the declaredmethods property from the reflectiondata object. If it is the first time or the class property after reinitialization is empty after being recycled by GC, you need to obtain it in the JVM again and assign it to reflectiondata, The cached data will be available for the next call.

Method call

After obtaining the specified method object method, you can call its invoke method. The invoke implementation is as follows:

It should be noted that the methodaccessor object here is the key to the implementation of the invoke method. At first, the methodaccessor is empty. You need to call acquiremethodaccessor to generate a new methodaccessor object. The methodaccessor itself is an interface. The implementation is as follows:

In the acquiremethodaccessor method, an object that implements the methodaccessor interface will be created through the newmethodaccessor of the reflectionfactory class. The implementation is as follows:

In the reflectionfactory class, There are two important fields: noinflation (default false) and inflationthreshold (default 15). In the checkinitiated method, you can reset these two fields through - dsun. Reflect. Inflationthreshold = XXX and - dsun. Reflect. Noinflation = true, and they will only be set once; if noinflation is false, the method newmethodaccessor will return the delegatingmethodaccessorimpl object, which is implemented by the delegatingmethodaccessorimpl class:

In fact, the delegatingmethodaccessorimpl object is a proxy object, which is responsible for calling the invoke method of the delegate object. The delegate parameter is currently the nativemethodaccessorimpl object, so the invoke method of the final method calls the invoke method of the nativemethodaccessorimpl object. The implementation is as follows:

Here, the inflationthreshold in the reflectionfactory class is used. After the delegate calls the invoke method 15 times, if the call continues, the methodaccessorimpl object will be generated through the generatemethod method of the methodaccessorgenerator class and set as the delegate object, so that the method.exe will be executed next time When invoking, the invoke () method of the newly created methodaccessor object is called. It should be noted here that when the generatemethod method generates the methodaccessorimpl object, it will generate the corresponding bytecode in memory and call classdefiner Defineclass creates the corresponding class object. The implementation is as follows:

In classdefiner In the implementation of the defineclass method, a delegatingclassloader class loader object will be generated every time it is called:

New class loaders are generated every time for performance reasons. In some cases, these generated classes can be unloaded, because class unloading can only be recycled when the class loader can be recycled. If the original class loader is used, these newly created classes may not be unloaded, From its design point of view, I don't want these classes to exist in memory all the time. Just have them when you need them!

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.

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
分享
二维码
< <上一篇
下一篇>>