search
HomeJavajavaTutorialDetailed explanation of the principles of Java reflection implementation method

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
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

一文掌握Java8新特性Stream流的概念和使用一文掌握Java8新特性Stream流的概念和使用Jun 23, 2022 pm 12:03 PM

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor