In depth understanding of java reflection mechanism

In depth understanding of java reflection mechanism

I Concept

Reflection is to map various components of Java into corresponding Java classes.

The construction method of class is private, which is created by the JVM.

Reflection is a feature of the Java language, It allows programs to run For example, it allows a Java class to obtain all its member variables and methods and display them. This capability of Java may not be used much in practical applications, but it does not exist in other programming languages. For example, Pascal, C or C ++There is no way to obtain the information related to the function definition in the program. (from sun)

JavaBean is one of the practical applications of reflection. It allows some tools to visually operate software components. These tools dynamically load and obtain the properties of Java components (classes) through reflection.

Reflection has existed since 1.2. The following three frameworks will use the reflection mechanism, involving the class "class". It is impossible to directly new class(), whose object is a bytecode in memory   

An instance of the class represents classes and interfaces in a running Java application. Enumeration is a kind and annotation is an interface. Each array belongs to a class mapped as a class object, which is shared by all arrays with the same element type and dimension. The basic Java types (Boolean, byte, char, short, int, long, float and double) and the keyword void are also represented as class objects. Class has no public constructor. Class objects are automatically constructed by the Java virtual machine when loading classes and by calling the defineclass method in the class loader

CLass. The forname() bytecode has been loaded into the Java virtual machine to get the bytecode; The bytecode has not been generated in the Java virtual machine and loaded by the class loader. The loaded bytecode is buffered into the virtual machine.  

Consider the following simple example to see how reflection works.

This lists Java util. The name of each method of the stack class and their qualifiers and return types. This program uses class ForName loads the specified class and then calls getDeclaredMethods to get the list of methods defined in this class. java. lang.reflect. Methods is a class used to describe a single method in a class.

The following example uses the class object to display the class name of the object:

You can also use a class literal (JLS section 15.8.2) to obtain a class object of the specified type (or void). For example:

When there are no object instances, there are two main methods.

Note that in the second method, the parameter in forname must be the complete class name (package name + class name), and this method needs to catch exceptions. Now you can create an instance of role class by obtaining cls1. Using the newinstance method of class is equivalent to calling the default constructor of the class.

II Common methods

1. Isprimitive (judge whether it is a basic type of bytecode)

2. Getconstructors and getconstructors ()

There is no order of construction methods in Java, which is distinguished by type and number of parameters.

3. The file class represents a member variable in a class.

III Typical examples

1. Change B in all string type member variables to a.

2. Write a program to call the main method in the class according to the class name provided by the user.

Why use reflection?

3. Simulate instanceof operator

In this example, create a class object of class s, and then check whether some objects are instances of S. Integer (37) is not, but new s() is.

IV Method class

Represents a method in a class (not an object).

V Reflection of array

The array utility class is used to complete the reflection operation of the array.

The same type and latitude have the same bytecode.

Int.class and integer Class is not the same bytecode, integer Type, type represents the bytecode int.class = = integer of the basic class corresponding to the wrapper class TYPE

Vi Conclusion

The above is the simple use of the reflection mechanism. Obviously, friends who have studied spring must understand why we can get the specified methods and variables through the configuration file. When we create objects, we pass in strings, just like what you need, we produce for you, and we have been using objects, This shows that the dynamic characteristics and dependency of Java language are greatly reduced.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

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