Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the Java reflection mechanism

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to understand the Java reflection mechanism", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to understand the Java reflection mechanism.

1. Brief introduction

Definition: a dynamic (runtime) ability in the Java language to access, detect, and modify itself.

Function: get the complete structure information of the class dynamically (run time) & call the method of the object

The structure information of the class includes: variables, methods, etc.

Normally, Java classes are loaded into JVM before they are compiled, and the reflection mechanism enables the program to dynamically manipulate variables, methods, and other information of the class.

two。 Characteristics

2.1 benefits

High flexibility. Because reflection is a dynamic compilation, that is, it is not created dynamically until run time & the object instance is obtained.

Description of compilation method:

1. Static compilation: determines the type at compile time & the binding object. Such as the common use of the new keyword to create objects

two。 Dynamic compilation: the runtime determines the type & the binding object. Dynamic compilation embodies the flexibility and polymorphism of Java & reducing the coupling between classes.

2.2 shortcomings

Low efficiency of execution

Because reflection operations are mainly performed through JVM, the time cost is higher than performing the same operation directly

Because of the versatility of the interface, the invoke method of Java passes the object and object [] arrays. Basic type parameters need to be boxed and unboxed, resulting in a lot of additional object and memory overhead, frequently promoting GC.

It is difficult for the compiler to optimize dynamically invoked code in advance, such as method inlining.

Reflection needs to retrieve classes and methods by name, which has some time overhead.

Easy to destroy class structure

Because the reflection operation excuses the source code, it is easy to interfere with the original internal logic of the class.

3. Application scenario

Dynamically obtain class file structure information (such as variables, methods, etc.) & methods that call objects

Common requirement scenarios are: dynamic agent, factory schema optimization, Java JDBC database operation, etc.

The following will be explained in detail with practical examples

4. Specific use

4.1 functions provided by the Java reflection mechanism

4.2 means of realization

The reflection mechanism is mainly realized by manipulating the java.lang.Class class.

The following will focus on the java.lang.Class class

4.2.1 java.lang.Class Class

Definition: the java.lang.Class class is the basis of the reflection mechanism

Function: stores the runtime information of the corresponding type of object

Only 1 for each type of Class object = only 1 address

The implementation of the Java reflection mechanism depends not only on the Java.lang.Class class, but also on: the Constructor class, the Field class, and the method class, which act on each component of the class:

4.3 use steps

When using the Java reflection mechanism, the main steps include:

1. Get the Class object of the target type

two。 Get Constructor class object and method class object & Field class object through Class object respectively

3. Obtain the specific information of the constructor and method-attribute of the class through the Constructor class object and the method class object & Field class object respectively, and carry on the subsequent operation

Next, I will explain in detail how to use each step.

Step 1: get the Class object of the target type

A little bit more about the java.lang.reflect.Type class here.

Java.lang.reflect.Type is the parent interface of all types in Java

These types include:

The relationship between them is as follows

Step 2: get Constructor class object, method class object & Field class object through Class object.

Step 3: obtain the specific information of the constructor and method-attribute of the class through the Constructor class object and the method class object & Field class object respectively.

4.4 Special attention: access rights issues

Background

The default behavior of the reflection mechanism is limited by the access control of Java

For example, cannot access (private) private methods and fields

Conflict

The Java security mechanism only allows you to see which fields any object has, but not to read their values.

If a read is forced, an exception will be thrown

Solution

Break away from the control of the security manager in the Java program, block the access check of Java language, and thus break away from the access control.

Specific implementation means: using Field class, method class & setAccessible () of Constructor class object

At this point, I believe you have a deeper understanding of "how to understand the Java reflection mechanism". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report