Explanation of Java Reflection Mechanism

  • 2021-07-18 07:51:58
  • OfStack

Reflection in Java provides a means of obtaining object meta-information at runtime. That is, the normal method is to create an object through one class, and the reflection method is to find the information of one class through one object.

The reflection mechanism of Java is realized by means of four classes: class, Constructor, Field, Method;

class represents the time class object, Constructor-class constructor object, Field-class attribute object, Method-class method object. Through these four objects, we can roughly see the components of a class.

The role of Java reflex:

In the Java runtime environment, for any 1 class, you can know what properties and methods that class has. For any 1 object, you can call any 1 of its methods. This ability to dynamically retrieve class information and dynamically call methods of objects comes from the reflection (Reflection) mechanism of Java language.

The Java reflection mechanism mainly provides the following functions

Determine the class to which any 1 object belongs at run time. Constructs an object of any 1 class at run time. Determine the member variables and methods of any 1 class at run time. Calls the method of any 1 object at run time.

Common classes and functions of reflection: The realization of Java reflection mechanism depends on four classes: Class, Constructor, Field, Method;

Among them, class represents class object, Constructor-class constructor object, Field-class attribute object and Method-class method object. Through these four objects, we can roughly see each component of a class. At the core of this is the Class class, which is the basis for implementing reflection, and contains methods that we have already described in Part 1. When applying reflection, we are most concerned about the constructor, properties and methods of a class. Let's mainly introduce the methods for these three elements in Class class:

1. The method to get the constructor

Constructor getConstructor (Class [] params)--Gets a public constructor that uses a special parameter type, Constructor [] getConstructors ()--Gets all public constructors for the class Constructor getDeclaredConstructor (Class [] params)--Gets a constructor that uses a specific parameter type (regardless of access level) Constructor [] getDeclaredConstructors ()--Gets all constructors for the class (regardless of access level)

2. The method of obtaining field information

Field getField (String name)--Get the named public field Field [] getFields ()--Gets all the public fields of the class Field getDeclaredField (String name)--Gets the named field of the class declaration Field [] getDeclaredFields ()--Get all the fields of the class declaration

3. Method of obtaining method information

Method getMethod (String name, Class [] params)--Gets a named public method using a specific parameter type Method [] getMethods ()--Gets all public methods of the class Method getDeclaredMethod (String name, Class [] params)--Gets the named method of the class declaration using the close-up parameter type Method [] getDeclaredMethods ()--Get all the methods declared by the class

Using reflection and attribute file in program development can achieve the purpose of separating program code from configuration file. If we want to get information about an object, we generally need the process of "introducing the name of the required 'package. class'-through new instantiation-to get the instantiated object". Using reflection, we can become the process of "instantiating the object-getClass () method-to get the complete 'package. class' name".

Summarize


Related articles: