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

What is the reflection mechanism of reflect Java?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the reflection mechanism of reflect Java? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

First, there are two ways to identify types:

First of all, take a look at the two main ways of "runtime type identification" (Run-time Type Identification, RTTI).

* *: we already know all the types at compile time and run time.

The second is our sub-module compilation of the whole project, which can dynamically compile the newly added modules at run time. (the type of code being compiled is not known until the module is compiled dynamically.) This is the powerful "reflection" mechanism to be accepted below.

Second, understand the "Class object":

To understand how RTTI (runtime type recognition) works in Java, you must first know how type information is represented at run time, which is done by the "Class object", which contains information about the class.

A class is an important part of a program (properties, methods, and some of its features, which I won't repeat here.) Each class has a Class object, and every time a new class is written and compiled, a Class object is generated, which is saved in a .class file with the same name as the new class you created. So when the program is running, when we want to generate the object of this class (instantiate the class), the Java virtual machine (JVM) running the program will do this:

First, the .class file of the innovative class is loaded

Then verify that the Class object of the new class has been loaded, and if not, JVM looks for the .class file based on the class name and loads it. Once the Class object of the class is loaded into memory, it is used to create all the objects of the class.

There are three general forms of RTTI:

1. Traditional type conversion. For example, "(Apple) Fruit", RTTI ensures the correctness of the type conversion, and if an incorrect type conversion is performed, a ClassCastException exception is thrown.

two。 Get the type of the object through the Class object. Such as

[code= "java"] Class c = Class.forName ("Apple")

Object o = c.newInstance ()

3. Use the keyword instanceof or Class.isInstance () method to determine whether an object belongs to an instance of a particular type. To be exact, instanceof / Class.isInstance () can be used to determine whether an object belongs to an instance of a particular class and all its base classes, unlike equals () / = =. They are used to compare whether two objects belong to instances of the same class, regardless of inheritance.

III. Reflection

If you don't know the type of an object, you can get it through RTTI, but only if the type is known at compile time so that it can be identified using RTTI. That is, at compile time, the compiler must know all the classes that are processed through RTTI.

This limitation can be avoided by using the reflection mechanism, which is mainly applied in two situations:

In the case of component-based programming, some kind of application building tool based on rapid application development (RAD) will be used to build the project. This is now the most common visual programming method, creating programs by dragging icons representing different components onto the drawing board and then setting the property values of the components (components) to configure them. To do this kind of configuration programming, it is necessary to require that the components are instantiable and expose some of their information, so that the programmer can read and set the values and states of the components. When dealing with artifacts of GUI time, you must also expose the event handling details of the relevant methods so that the RAD environment helps programmers override these methods of handling events. Here, the reflection mechanism is used to check the available methods and return the method entity object. Java provides a component-based programming architecture through JavaBeans.

In the second case, another motivation for getting information about a class at run time is to provide the ability to create and run objects on remote platforms across networks. This is called remote invocation (RMI), which allows a Java program to step objects on multiple machines. This step-by-step ability will help developers perform tasks that require a lot of computing, make full use of computer resources, and improve running speed.

The Class class supports reflection and includes the Field/Method/Constructor class in java.lang.reflect, each of which implements the Member interface. These types of objects are created by JVM at run time to represent the corresponding members of the unknown class. For example, you can create a new object with the Constructor class, read and modify the fields associated with the Field object with the get () and set () methods, and call the methods associated with the Method object with the invoke () method. At the same time, you can call getFields (), getMethods (), getConstructors (), and other methods to return an array of objects that represent fields, methods, and constructors. In this way, the class information of unknown objects can be fully determined at run time, and no information is needed at compile time.

In addition, RTTI can sometimes solve efficiency problems. When the use of polymorphism in a program puts a burden on the running of the program, you can use RTTI to write a piece of code to improve efficiency.

After reading the above, have you mastered the reflection mechanism of reflect Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Development

Wechat

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

12
Report