The basic concept

  • 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

Class Class

The basic concept

  • 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.

How to get a Class object

  • The class object of the corresponding type can be obtained by using the 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.

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

Constructor class

The basic concept

  • Java. Lang. Reflect the Constructor class is mainly used to describe the method of access to information

Common methods of the Class 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

A common method 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 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

The Field class

The basic concept

  • The java.lang.Reflect. Field class is mainly used to describe the information retrieved from a single member variable.

Common methods of the Class Class

Method statement Function is introduced
Field getDeclaredField(String name) Gets information about a single member variable specified by the parameter in the Class represented by this Class object
Field[] getDeclaredFields() Gets information about all member variables in the Class represented by this Class object

Common methods of the 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 access checks
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 the class

The basic concept

  • The java.lang.Reflect. Method class is mainly used to describe the information retrieved from individual member methods.

Common methods of the Class 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

Method A common Method 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

Get additional structural information

Method statement Function is introduced
Package getPackage() Gets the package information
Class<? super T> getSuperclass() Gets information about the inherited parent
Class<? >[] getInterfaces() Gets all the interfaces implemented
Annotation[] getAnnotations() Get annotation information
Type[] getGenericInterfaces() Get generic information