Home  >  Article  >  Java  >  Detailed explanation of the principles of Java reflection implementation method

Detailed explanation of the principles of Java reflection implementation method

黄舟
黄舟Original
2017-10-17 09:36:141899browse

This article mainly introduces the implementation principle of Java method reflection in detail, which has certain reference value. Interested friends can refer to it

The blogger said: The Java reflection mechanism is that in the running state, for any class, you can know all the properties and methods of this class; for any object, you can call any of its methods and properties; this dynamic acquisition of information and dynamic call of object methods The feature is called the reflection mechanism of the Java language. In this article, Zhan Xiaolang analyzes the implementation principle (source code) of the Java reflection mechanism. Interested students can spend a few minutes reading this article to understand.

Text

Detailed explanation of the principles of Java reflection implementation method

Method Reflection Example


public class ReflectCase {

  public static void main(String[] args) throws Exception {
    Proxy target = new Proxy();
    Method method = Proxy.class.Detailed explanation of the principles of Java reflection implementation method("run");
    method.Detailed explanation of the principles of Java reflection implementation method(target);
  }

  static class Proxy {
    public void run() {
      System.out.println("run");
    }
  }
}

Through Java The reflection mechanism can call any method of an object during runtime. If a large number of calls are made in this way, will there be any performance or memory risks? In order to fully understand the reflection mechanism of the method, we can only start from the underlying code!

Method Get

Call Detailed explanation of the principles of Java reflection implementation method of the Class class to obtain the method object Method with the specified method name and parameters.

Detailed explanation of the principles of Java reflection implementation method

Detailed explanation of the principles of Java reflection implementation method

The Detailed explanation of the principles of Java reflection implementation method method obtains the list of methods declared in the Class from the cache or Detailed explanation of the principles of Java reflection implementation method, and the Detailed explanation of the principles of Java reflection implementation method method will Detailed explanation of the principles of Java reflection implementation method Find a method object matching the name and parameters in the method list.

Detailed explanation of the principles of Java reflection implementation method

Detailed explanation of the principles of Java reflection implementation method

If a matching Method is found, make a new copy and Detailed explanation of the principles of Java reflection implementation method it, that is, Method.copy( )method.

Detailed explanation of the principles of Java reflection implementation method

The Method object Detailed explanation of the principles of Java reflection implementation methoded each time the Detailed explanation of the principles of Java reflection implementation method method is called is actually a new object, and the root attribute of the new object points to the original Method object. If needed frequently When calling, it is best to cache the Method object.

Detailed explanation of the principles of Java reflection implementation method

Get the list of methods declared in the Class from the cache or Detailed explanation of the principles of Java reflection implementation method. The implementation is as follows:

Detailed explanation of the principles of Java reflection implementation method

The Detailed explanation of the principles of Java reflection implementation method() method is implemented as follows:

Detailed explanation of the principles of Java reflection implementation method

There is a more important data structure ReflectionData, which is used to cache the following attribute data of the class read from the Detailed explanation of the principles of Java reflection implementation method:

Detailed explanation of the principles of Java reflection implementation method

It can be seen from the Detailed explanation of the principles of Java reflection implementation method() method implementation that the Detailed explanation of the principles of Java reflection implementation method object is of SoftReference type, indicating that it may be recycled when memory is tight, but it can also be controlled through the -XX:SoftRefLRUPolicyMSPerMB parameter At the time of recycling, as long as GC occurs, it will be recycled. If Detailed explanation of the principles of Java reflection implementation method is recycled and the reflection method is executed, then such an object can only be re-created through the Detailed explanation of the principles of Java reflection implementation method method. The Detailed explanation of the principles of Java reflection implementation method method is implemented as follows:

Detailed explanation of the principles of Java reflection implementation method

Reset the Detailed explanation of the principles of Java reflection implementation method field through the unsafe.compareAndSwapObject method; in the Detailed explanation of the principles of Java reflection implementation method method, if the ReflectionData object obtained through Detailed explanation of the principles of Java reflection implementation method() is not empty, try to obtain the declaredMethods property from the ReflectionData object. If it is the Once, or after being recycled by GC, if the reinitialized class attribute is empty, you need to obtain it from the Detailed explanation of the principles of Java reflection implementation method again and assign it to ReflectionData. The cached data can be used next time it is called.

Method calls

After obtaining the specified method object Method, you can call its Detailed explanation of the principles of Java reflection implementation method method. The Detailed explanation of the principles of Java reflection implementation method implementation is as follows:

Detailed explanation of the principles of Java reflection implementation method

It should be noted that the Detailed explanation of the principles of Java reflection implementation method object here is the key to the implementation of the Detailed explanation of the principles of Java reflection implementation method method. At first, the methodAccessor is empty, and acquireDetailed explanation of the principles of Java reflection implementation method needs to be called to generate a new Detailed explanation of the principles of Java reflection implementation method object. Detailed explanation of the principles of Java reflection implementation method itself is an interface, and the implementation is as follows:

Detailed explanation of the principles of Java reflection implementation method

In the acquireDetailed explanation of the principles of Java reflection implementation method method, an object that implements the Detailed explanation of the principles of Java reflection implementation method interface will be created through newDetailed explanation of the principles of Java reflection implementation method of the ReflectionFactory class. The implementation is as follows:

newDetailed explanation of the principles of Java reflection implementation method

In the ReflectionFactory class, there are two important fields: noInflation (default false) and inflationThreshold (default 15). In the checkInitted method, you can pass -Dsun.reflect.inflationThreshold=xxx and -Dsun.reflect.noInflation=true Reset these two fields and only set them once; if noInflation is false, the method newDetailed explanation of the principles of Java reflection implementation method will Detailed explanation of the principles of Java reflection implementation method the DelegatingDetailed explanation of the principles of Java reflection implementation methodImpl object. The class implementation of DelegatingDetailed explanation of the principles of Java reflection implementation methodImpl:

DelegatingDetailed explanation of the principles of Java reflection implementation methodImpl

In fact, the DelegatingDetailed explanation of the principles of Java reflection implementation methodImpl object It is a proxy object that is responsible for calling the Detailed explanation of the principles of Java reflection implementation method method of the delegate of the delegated object. The delegate parameter is currently a NativeDetailed explanation of the principles of Java reflection implementation methodImpl object, so the final method's Detailed explanation of the principles of Java reflection implementation method method calls the NativeDetailed explanation of the principles of Java reflection implementation methodImpl object's Detailed explanation of the principles of Java reflection implementation method method. The implementation is as follows:

The inflationThreshold in the ReflectionFactory class is used here. After the delegate calls the Detailed explanation of the principles of Java reflection implementation method method 15 times, if the call continues, the Detailed explanation of the principles of Java reflection implementation methodImpl object will be generated through the generateMethod method of the Detailed explanation of the principles of Java reflection implementation methodGenerator class and set as the delegate object, so that the Method will be executed next time. When Detailed explanation of the principles of Java reflection implementation methodd, the Detailed explanation of the principles of Java reflection implementation method() method of the newly created Detailed explanation of the principles of Java reflection implementation method object is called. What needs to be noted here is that when the generateMethod method generates a Detailed explanation of the principles of Java reflection implementation methodImpl object, it will generate the corresponding bytecode in the memory and call ClassDefiner.defineClass to create the corresponding Class object. The implementation is as follows:

Detailed explanation of the principles of Java reflection implementation method

In the implementation of the ClassDefiner.defineClass method, a Detailed explanation of the principles of Java reflection implementation method class loader object is generated every time it is called:

Detailed explanation of the principles of Java reflection implementation method

A new class loader is generated every time. , for performance reasons, these generated classes can be unloaded in some cases, because the unloading of classes will only be recycled when the class loader can be recycled. If the original class loader is used, then This may cause these newly created classes to never be unloaded. From the design point of view, we do not want these classes to always exist in the memory. Just have them when needed!

The above is the detailed content of Detailed explanation of the principles of Java reflection implementation method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn