Detailed Java reflection

table of Contents

One, the concept of java reflection

Second, the Class object

         How to obtain

Three, get the construction method

(1) Public class

(2) No distinction between access rights

Fourth, how to use reflection to manipulate methods in a class

         (1) Obtain construction method information

         (2) Create an object

Five, use reflection to manipulate the construction method in the class


One, the concept of java reflection

Java reflection (Reflection) is a new mechanism for manipulating member variables, construction methods and common methods in a class.

In order to realize the operation of member variables, construction methods and common methods, we need to use the Class class under the java.lang package provided by Java itself and the reflection API under the java.lang.reflect package.

 

Second, the Class object

The Class class is the entrance to the Java reflection mechanism , which encapsulates the runtime information of a class or interface , which can be obtained by calling the methods of the Class class.

Features of the Class class:

(1) The class is in the java.lang package ;
(2) The class is modified by final , that is, the class cannot be inherited by subclasses ;
(3) The class implements the Serializable interface ;
(4) The construction method of the class is Modified by private , that is , objects of this class cannot be created through the new keyword ;

Obtaining method:

(1) Obtain an instance of the Class class through the Class class static forName ("class package name. class name"). It is recommended to use this form: (the generic value is uncertain and the question mark is available)

(2) Obtain the Class instance by using the class name .class:

(3) If you have created an object of reference type, you can obtain an instance of the Class class by calling the getClass() method in the object

(4) If it is a basic data type, you can obtain an instance of the Class class through the packaging class .TYPE, of course, you can also obtain an instance of the Class class through the basic data type .class.

The first output int, the second output java.lang.Integer

(5) The class instance corresponding to the array can be obtained through the element type [].class:

Output [Ljava.lang.Integer

(6) By calling the getSuperClass() method of the Class instance of a certain class, the Class instance of the superclass of this class can be obtained:

Three, get the construction method

public class

(1) Constructor[] getConstrutors(): Returns an array of Constructor objects corresponding to all public construction methods (excluding inheritance) contained in the Class object representing the class.
(2) Constructor getConstrutor(Class<?>... parameterTypes): Returns the Constructor object corresponding to the public constructor (excluding inheritance) that matches the parameter list in the Class object representation class.

No distinction between access rights

(1) Constructor<?>[] getDeclaredConstructors(): Returns an array of Constructor objects corresponding to all the constructors declared in the class (without distinguishing access rights) that the Class object represents.
(2) Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes): Returns the Constructor object that matches the type of the formal parameter defined in the Class object representation class (without distinguishing access rights).

 

Fourth, how to use reflection to manipulate methods in a class

(1) Obtain information about the construction method

  The information of a certain construction method can be obtained through certain methods of the Constructor class . These methods are as follows:
(1) Class<T> getDeclaringClass(): Returns the Class object of the class that declares the constructor object corresponding to the constructor method.
(2) int getModifiers(): Returns the modifiers of the constructor represented by the Constructor object in integer form.
(3) String getName(): Returns the name of the constructor represented by the Constructor object in the form of a string.
(4) Class<?>[] getParameterTypes(): returns an array of the parameter types of the constructor represented by the Constructor object corresponding to the Class object. If the constructor has no parameters, the length of the array is 0.

E.g:

Note:
(1) Both getConstructors() and getConstructor(Class<?>... parameterTypes) methods cannot obtain the Constructor object corresponding to the default non-parameter construction method in the non-public class.
(2) The getDeclaredConstructors() and getDeclaredConstructor(Class<?>... parameterTypes) methods can obtain the Constructor object corresponding to the default no-parameter constructor in the non-public class.

(2) Create an object

Objects can be created through certain methods of the Constructor class . These methods are as follows:
(1) void setAccessible(boolean flag): Whether to ignore the influence of access permissions when calling the constructor, true means ignore, and false means not ignore.
(2) T newInstance(Object...initargs): Use the constructor represented by this Constructor object to create a new object that declares the constructor class. initargs is the parameter passed into the construction method. If the construction method has no parameters, it can be set to null or an array with a length of 0.

 

Five, use reflection to manipulate the construction method in the class

Guess you like

Origin blog.csdn.net/m0_46383618/article/details/113472988