In depth analysis of java reflection mechanism

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 kind of dynamically obtained information and the function of dynamically calling object methods are called the reflection mechanism of Java language. The concept of reflection was first proposed by Smith in 1982. It mainly refers to the ability of a program to access, detect and modify its own state or behavior. The proposal of this concept soon triggered the research on applied reflexivity in the field of computer science. It was first adopted in the field of programming language design, and made achievements in LISP and object-oriented. Of course, reflection itself is not a new concept. It may remind us of the concept of reflection in optics. Although computer science gives the concept of reflection a new meaning, phenomenally speaking, they do have some similarities, which are helpful for our understanding.

Java reflection mechanism mainly provides the following purposes:

Determine the class of any object at run time

Construct an object of any class at run time

Judge the member variables and methods of any class at run time

Call the method of any object at run time

First look at a simple example to understand how Java's reflection mechanism works.

When the passed in parameter is Java Lang. string, the following results will be output

This lists Java All method names, their qualifiers, return types and thrown exceptions of lang.string class. This program loads the specified class using the Class class forName method, and then calls the getmethods method to return the list of methods for the specified class. java. lang.reflect. Method is used to express a single method in a class.

To use Java's reflection mechanism, you generally need to follow three steps:

Obtain the class object of the class you want to operate on. Obtain the method or property name of the operation class through the class object obtained in the first step. Operate the method or property obtained in the second step

When Java is running, no matter how many objects a class generates, they will correspond to the same class object, which represents the classes and interfaces in the running program. There are three common ways to obtain the class object of an operation class:

Call the static method forname of class, as in the above example;

Use the of the class Class syntax, such as class cls = String. class; Call the getClass method of the object, such as string STR = "ABC"; Class cls = str . getClass();

The following will describe how to execute a method of an object through the three steps described above through an example:

As mentioned earlier, each class of a java program will have a class object corresponding to it. The first step in java reflection is to get the class object, as shown in line 14 of the code. Of course, the method of each class must also have a method object corresponding to it. To call this method through reflection, first obtain the method object of this method, such as line 16 of code, and then call this method in turn with the method object, such as line 18 of code. Note that the first parameter of the 16 line getmethod method is the method name and the second is the parameter type of the method. If there are multiple parameters, then add the parameters, because getmethod is a variable parameter method. Execute the invoke method of 18 lines of code, which is actually executing the show method. Note that the first parameter of invoke is an object of the play class, that is, the show method of which object of the play class is called. The second parameter is the parameter passed to the show method. The type and number must be consistent with the 16 line getmethod method.

The above example describes how to call a class's method through reflection, and the following example describes how to assign a value to a class's attribute through reflection:

In Java's emission mechanism, classes have class correspondence, class methods have method correspondence, and of course, attributes also have field correspondence. Detailed comments have been made in the code and will not be repeated here. Note that field provides get and set methods to obtain and set the value of the property. However, since the property is a private type, you need to set the accessibility of the property to true, such as lines 50 ~ 51 of the code. You can also set accessibility for the entire fields. Under line 40, use the static method setaccessible of accessibleobject, such as accessibleobject setAccessible(fromFields,true);

Previously, we described how to use java reflection mechanism to operate the methods and properties of a class. Next, we describe how to create an object of a class at runtime through an example:

The results of this example and the previous example are the same. However, the object returned by the copybean method is no longer passed in from the outside, but generated inside the method, as shown in line 40. Note: the newinstance method of class can only create classes that contain only parameterless constructors. If a class has only parameterless constructors, another method should be used: fromclass getDeclaredConstructor(int.class,String.class). newInstance(24,"wanggc");

So far, the common functions of java reflection mechanism (calling methods of objects at runtime, using class properties, and creating class objects) have been introduced.

Supplement: when obtaining the methods, properties and constructors of the class, there will be two corresponding methods: getxxx and getgetdeclaredxxx. The difference is that the former returns methods and properties with access permission of public, including those in the parent class; However, the latter returns the methods and properties of all access permissions, excluding those of the parent class.

The above content is the Java launch mechanism introduced to you. I hope you like it.

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