"The Java reflection mechanism is in a running state where all the properties and methods of the class are known to any class, and any of its methods and properties can be called for any object"
The acquisition of a class object
1. Getting through objects
New Object (); Obj.getclass ();
2. Get through the class name
Object. class;
3. Get through the path name of the class
Class.forName ("Com.metadata.Student");
Ii. instantiation and constructors of classes
The obtained class object can be instantiated directly from the Clazz.newinstance () method, but requires the target class to have a default parameterless constructor, or an exception will be thrown.
In cases where the class does not have a default parameterless constructor, or if a specific constructor is required to instantiate, it needs to be done through the newinstance () of the constructor class.
1. Get the public constructor, excluding the parent class
Class
public constructor<?> public constructor<t> getconstructor (class<?> ..... Parametertypes)
2. Get the current class constructor, ignore modifiers
// Class Public Constructor<?>[] getdeclaredconstructors () public constructor<t> Getdeclaredconstructor (CLASS<?> parametertypes)
Constructor call
// Constructor Public T newinstance (Object ... initargs)
Ignore modifier, Force call
public void Setaccessible (Boolean flag)
Third, the acquisition of class member variables
1. Get public variables, including the parent class
// Class Public field[] GetFields () Public Field GetField (String name)
2. Gets the current class member variable, ignoring the modifier
// Class Public field[] Getdeclaredfields () Public Field Getdeclaredfield (String name)
Member variable Assignment
// Field // obj As an instance object Public void Set (Object obj,object value)
Ignore modifier, Force call
public void Setaccessible (Boolean flag)
Iv. acquisition of class methods
1. Get the public method, including the parent class
// Class Public method[] GetMethods () Public Method GetMethod (String name, Class<?> .... parametertypes)
2. Gets the current class method, ignoring the modifier
// Class Public method[] Getdeclaredmethods () Public Method Getdeclaredmethod (String name, Class<?> .... parametertypes)
Method invocation
// Method // obj instantiates an object for a class if it is a static method, obj is nullinvoke (Object obj, object ... args)
Ignore modifier, Force call
public void Setaccessible (Boolean flag)
Java Reflection Common API Rollup