Reflection (reflection) is the key to Java being considered as a dynamic language , and the reflection mechanism allows the program to obtain internal information of any class with the help of the Reflection API during the execution period, and can directly manipulate Internal properties and methods of arbitrary objects
Features provided by the Java reflection mechanism
determine the class to which any object belongs at run time
To construct an object of any class at run time
determine the member variables and methods that any one class has at run time
To invoke member variables and methods of any object at run time
Generating Dynamic Agents
Reflection-related major APIs:
Java.lang.Class: Representing a class is the source of reflection,
Java.lang.reflect. method: Means of representing a class
Java.lang.reflect. Field: Represents a member variable of a class
Java.lang.reflect. Constructor: The construction method of the Representative class
Note: When creating a class, try to keep an empty parameter constructor
How to get an instance of class
1) Premise: If the specific class is known, the method is most safe and reliable, and the program performance is the highest, if the class attribute is obtained.
Example: Class clazz = String.class;
2) Premise: an instance of a class is known, calling the instance's GetClass () method to get the class object
Example: Class clazz = "www.atguigu.com". GetClass ();
3) Premise: A class is known to the full class name, and the class path, can be obtained through the class class static method Forname (), may throw classnotfoundexception
Example: Class clazz = Class.forName ("java.lang.String");
4) Other means (not required)
ClassLoader cl = This.getclass (). getClassLoader ();
Class clazz4 = Cl.loadclass ("Class-wide name");
create the object of the runtime class: call the Newinstance () method of the class object, which is actually called the null parameter constructor
Requirements: 1) The class must have a constructor that has no arguments.
2) The constructor of the class must have sufficient access rights.
To get the properties of a run-time class
1.public field[] GetFields ()
Class clazz = String.class;
field[] fields = Clazz.getfields (); Returns the field of the class represented by this class object and its public (direct and indirect) parent class or interface.
2.public field[] Getdeclaredfields ()
field[] fields = Clazz.getdeclaredfields (); Returns all the field of the class or interface represented by this class object.
In the field method:
public int getmodifiers () returns the modifier for this field as an integer
int i = F.getmodifiers ();
String str = modifier.tostring (i);
Public class<?> GetType () Gets the field's property type
Public String GetName () returns the name of the field.
Use reflection to obtain:
1. Ways to get Runtime classes
Public method[] Getdeclaredmethods () returns all the methods of the class or interface represented by this class object
Public method[] GetMethods () returns the class represented by this class object and its (direct and indirect) method of the public of the parent class or interface
In the method class:
Public class<?> Getreturntype () gets all the return values
Public class<?>[] Getparametertypes () get all the parameters
public int getmodifiers () get modifier
Public class<?>[] Getexceptiontypes () Get exception information
annotation[] ann = M.getannotations ();
2. Get all the constructors
Public constructor<t>[] GetConstructors () returns all public constructor methods for the class represented by this class object.
Public constructor<t>[] Getdeclaredconstructors () returns all the constructor methods for the class declaration represented by this class object.
In the constructor class:
Get modifier: public int getmodifiers ();
Get method Name: Public String getName ();
The type of the obtained parameter: public class<?>[] Getparametertypes ();
3. Get all the interfaces implemented
Public class<?>[] Getinterfaces () determines the interface that this object represents for the implementation of the class or interface.
4. Obtain the Inherited parent class
Public class<? Super t> Getsuperclass () returns the class of the parent class representing the entity (class, interface, base type) represented by this class.
5. Get annotation Related
Get Annotation (class<t> annotationclass) getdeclaredannotations () is only defined as runtime to be reflected to the
6. Access to generics-related
Gets the parent class generic type: Type type = Clazz.getgenericsuperclass ();
Generic type: Parameterizedtype param = (parameterizedtype) type;
Gets the actual array of generic type parameters: type[] arg = param.getactualtypearguments ();
(Class) Arg[0].getname ()
7. Get the package where the class resides Package Getpackage ()
Invokes a specified method in a class by reflection, specifying a property, specifying a constructor
Dynamic Proxy : A dynamic Proxy is a method by which a client invokes other objects through a proxy class and dynamically creates a proxy object for the target class as needed when the program is run.
Java Foundation--java Reflection mechanism