1 Basic Concepts
- Usually write code is fixed, no matter how many times the results of the run is fixed, writing code in some special occasions are not sure what type of object to create, are not sure what kind of method to invoke, all hope that through the runtime parameters passed to decide, this mechanism is called dynamic programming techniques, namely reflection mechanism.
- In plain English, reflection is a mechanism for dynamically creating objects and calling methods.
- At present, the mainstream framework is implemented by reflection mechanism.
Person p = new Person(); A reference to a Person type refers to an object of type Person
p.show();Call the member method show in the Person class
Copy the code
2 Class Class
2.1 Basic Concepts of Class
- An instance of the java.lang.Class Class can be used to describe classes and interfaces in A Java application, which is a data type.
- There is no common constructor for this class, and instances of this class are automatically constructed by the Java Virtual machine and class loaders, essentially runtime classes loaded into memory.
2.2 Method of obtaining Class Objects
- The class object of the corresponding type can be obtained by using datatype. Class.
- The Class object of the corresponding type can be obtained by reference/object.getClass ().
- The Class object corresponding to the base data TYPE can be obtained by wrapping the.type Class.
- Use class.forname () to get the Class object of the specified type.
- Gets a Class object of the specified type using a ClassLoader.
2.3 Common methods
Method statement | Function is introduced |
---|---|
static Class<? > forName(String className) | Gets the Class object corresponding to the type specified by the argument and returns |
T newInstance() | Used to create a new instance of the Class represented by the Class object |
3 Constructor class
3.1 Basic Concepts
Java. Lang. Reflect the Constructor class is mainly used to describe the method of access to information
3.2 Common methods of Class
Method statement | Function is introduced |
---|---|
Constructor getConstructor(Class<? >… parameterTypes) | Gets the public constructor specified by the parameter in the type represented by this Class object |
Constructor<? >[] getConstructors() | Gets all the public constructors of the type represented by this Class object |
Common methods of the Constructor class
Method statement | Function is introduced |
---|---|
T newInstance(Object… initargs) | Use the Constructor described by this Constructor object to construct a new instance of the Class object representing the Class type |
int getModifiers() | Gets the access modifier for the method |
String getName() | Gets the name of the method |
Class<? >[] getParameterTypes() | Gets the types of all the method arguments |
4 the Field class
4.1 Basic Concepts
The java.lang.Reflect. Field class is mainly used to describe the information retrieved from a single member variable.
4.2 Common methods of Class
| | Field getDeclaredField (String name) is used to obtain the parameters specified in the Class Class object represents a single member variable | | Field [] getDeclaredFields () | Used to get the Class object represented by all the members of the Class | variable information
4.3 Common methods of Field class
Method statement | Function is introduced |
---|---|
Object get(Object obj) | Gets the value of the member variable represented by this Field object in the parameter object obj |
void set(Object obj, Object value) | Change the value of the Field object representing the member variable in the parameter object obj to the value of the parameter value |
void setAccessible(boolean flag) | When the argument is passed true, the reflection object should be used without Java language query checking |
int getModifiers() | Gets the access modifier for a member variable |
Class<? > getType() | Gets the data type of a member variable |
String getName() | Gets the name of the member variable |
Method 5 classes
5.1 Basic Concepts
The java.lang.Reflect. Method class is mainly used to describe the information retrieved from individual member methods.
5.2 Common methods of Class
Method statement | Function is introduced |
---|---|
Method getMethod(String name, Class<? >… parameterTypes) | Gets the specified public member method of the Class object representing the Class with name and parameterTypes |
Method[] getMethods() | Used to get all the public member methods in the Class object representation |
5.3 Method Common methods of the class
Method statement | Function is introduced |
---|---|
Object invoke(Object obj, Object… args) | The member Method represented by this Method object is called using the object obj, passing args as the argument |
int getModifiers() | Gets the access modifier for the method |
Class<? > getReturnType() | Gets the return value type of the method |
String getName() | Gets the name of the method |
Class<? >[] getParameterTypes() | Gets the types of all the method arguments |
Class<? >[] getExceptionTypes() | Get method exception information |