The reflection mechanism in Java allows us to obtain the constructors, member variables, and member functions in the bytecode file of a Java class at runtime.

1. Three ways to obtain a Class object:

  1. Class.forname (” Class name string “) (Note: Class name string must be full, package name + Class name)
        Class baseInfo = Class.forName("top.sguotao.ReflectionJava");
Copy the code
  1. The name of the class. The class
        Class object = Object.class;
Copy the code
  1. Instance object.getClass ()
        Class date = (new Date()).getClass();
        Class testclass = this.getClass();
Copy the code

2. Obtain class Constructor ().

  1. The parameter list is parameterTypes, and the access control is a public constructor
        public Constructor getConstructor(Class[] parameterTypes)
Copy the code
  1. Constructor that gets all access controls that are public
        public Constructor[] getConstructors()
Copy the code
  1. The parameter list is parameterTypes and is a constructor declared by the class itself. The access control includes public, protected, and private functions.
	public Constructor getDeclaredConstructor(Class[] parameterTypes)
Copy the code
  1. Gets all constructors declared by the class itself. The access control includes public, protected, and private functions.
	public Constructor[] getDeclaredConstructors()
Copy the code
  1. Return the constructor of the class if it is declared in another class’s constructor, or null if it is not
	public Constructor getEnclosingConstructor()
Copy the code

3. Get the member variables of the class

  1. Getname (” name is name “); getname (” name is name “); getname (” name is name “);
	public Field getField(String name)
Copy the code
  1. Get all public member variables (including all public member variables inherited from the base class and implemented from the interface)
	public Field[] getFields()
Copy the code
  1. Gets “name is name” and is a member variable declared by the class itself, including public, protected, and private member variables.
	public Field getDeclaredField(String name)
Copy the code
  1. Gets all member variables declared by the class itself, including public, protected, and private.
	public Field[] getDeclaredFields()
Copy the code

4. Get the member functions of the class

  1. Get a function whose name is name and whose parameters are public of parameterTypes (including all public functions inherited from the base class and implemented from the interface)
	public Method getMethod(String name, Class[] parameterTypes)
Copy the code
  1. Get all public functions (including all public functions inherited from the base class and implemented from the interface)
	public Method[] getMethods()
Copy the code
  1. Get the name of the function that takes parameterTypes and is declared by the class itself, including public, protected, and private methods.
	public Method getDeclaredMethod(String name, Class[] parameterTypes)
Copy the code
  1. Gets all functions declared by the class itself, including public, protected, and private methods.
	public Method[] getDeclaredMethods()
Copy the code
  1. If the class is an inner class of a method in another class, calling getEnclosingMethod() is the class’s method; If not, return NULL.
	public Method getEnclosingMethod()
Copy the code