Under normal circumstances, writing code is fixed, and the result of execution is fixed no matter how many times it is run. In some special occasions, when writing code, you are not sure what type of object to create or what method to call. These They all hope to be determined by the parameters passed at runtime. This mechanism is called dynamic programming technology, which is the reflection mechanism.
Generally speaking, the reflection mechanism is a mechanism used to dynamically create objects and dynamically call methods. At present, the bottom layer of mainstream framework is implemented by reflection mechanism.
Such as:
Person p = new Person();-indicates that the reference to the Person type points to an object of type Person
p.show();-means to call the member method show in the Person class
2.Class class
basic concept
Instances of the java.lang.Class class can be used to describe the classes and interfaces in a Java application, that is, a data type.
There is no public construction method for this class. The instance of this class is automatically constructed by the Java virtual machine and the class loader. In essence, it is a runtime class loaded into memory.
How to get the Class object
Use the data type .class to obtain the Class object of the corresponding type (master).
Use reference/object.getClass() to get the Class object of the corresponding type.
Use the wrapper class .TYPE way to get the Class object corresponding to the basic data type.
Use Class.forName() to obtain the Class object of the type specified by the parameter (master).
Use the class loader ClassLoader to obtain the Class object of the specified type.
Commonly used methods (mastery)
static Class forName(String className) is used to get the Class object corresponding to the specified type of the parameter and return it
T newInstance() is used to create a new instance of the class represented by the Class object
The java.lang.reflect.Constructor class is mainly used to describe the obtained construction method information
Common methods of Class
Constructor getConstructor(Class... parameterTypes) is used to obtain the public construction method specified by the parameters in the type represented by this Class object
Constructor[] getConstructors() is used to obtain all public construction methods in the type represented by this Class object
Common methods of Constructor class
T newInstance(Object… initargs) Use the construction method described by this Constructor object to construct a new instance of the representative type of the Class object
int getModifiers() Get the access modifiers of the method
String getName() Get the name of the method
Class[] getParameterTypes() Get the types of all parameters of the method
The java.lang.reflect.Field class is mainly used to describe the obtained information of a single member variable.
Common methods of Class
Method declaration function introduction Field getDeclaredField(String name) is used to obtain the information of a single member variable specified by the parameter in the class represented by this Class object
Field[] getDeclaredFields() is used to obtain all member variable information in the class represented by this Class object
Common methods of the Field class
Object get(Object obj) Get the value of the member variable represented by this Field object in the parameter object obj
void set(Object obj, Object value) Modify the value of the member variable represented by this Field object in the parameter object obj to the value of the parameter value
void setAccessible(boolean flag) When the actual parameter is passed true, the reflection object should cancel the Java language access check int
getModifiers() Get access modifiers of member variables
Class getType() Get the data type of the member variable
String getName() Get the name of the member variable
The java.lang.reflect.Method class is mainly used to describe the obtained method information of a single member.
Common methods of Class
Method getMethod(String name, Class… parameterTypes) is used to obtain the Class object, which means that the name in the class is the name parameter
The designated public member method of parameterTypes Method[] getMethods() is used to obtain the Class object representing all public member methods in the class
Common methods of the Method class
Object invoke(Object obj, Object... args) Use the object obj to call the member method represented by this Method object, and the actual parameters are passed args
int getModifiers() Get the access modifiers of the method
Class getReturnType() Get the return value type of the method String getName() Get the name of the method
Class[] getParameterTypes() Get the types of all parameters of the method
Class[] getExceptionTypes() Get the exception information of the method