Java reflection is a technology that allows you to get objects (classes, attributes, and methods) by name.
For example, we can use a class name to generate an instance of a class;
If you know the method name, you can call this method. If you know the attribute name, you can access the value of this attribute.
We can even set the value of the object's private property through reflection.
The following example illustrates the meaning and purpose of reflection:
public class Car { private String brand; private String color; private int maxSpeed; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } public String toString(){ return "the car is:"+ getBrand() + ", color is:" +getColor() +", maxspeed is:"+getMaxSpeed(); } public Car() { } public Car(String brand, String color, int maxSpeed) { this.brand = brand; this.color = color; this.maxSpeed = maxSpeed; } public void introduce() { System.out.println("brand:" + brand + ";color:" + color + ";maxSpeed:" + maxSpeed); }}
Public class reflecttest {public static car initbydefaconconst () throws throwable {// get the car object classloader loader = thread. currentthread (). getcontextclassloader (); Class <?> Clazz = loader. loadclass ("Spring. IOC. demo1.car "); // obtain the default constructor function of the class and instantiate an object to achieve the same effect as new car ();/* constructor <?> Cons = clazz. getdeclaredconstructor (class []) null); car = (CAR) cons. newinstance (); * // get the constructor with parameters of the class. The getconstructor must be a public constructor. Class ptypes [] = {string. class, String. class, Int. class}; constructor cons2 = clazz. getdeclaredconstructor (ptypes); car = (CAR) cons2.newinstance ("A", "B", 1); // set attributes by using the reflection method/* method setbrand = clazz. getmethod ("setbrand", String. class); setbrand. invoke (car, "Hongqi 001"); Method setcolor = clazz. getmethod ("setcolor", String. class); setcolor. invoke (car, "black"); Method setmaxspeed = clazz. getmethod ("setmaxspeed", Int. class); setmaxspeed. invoke (CAR, 200); */return car;} public static void main (string [] ARGs) throws throwable {car = reflecttest. initbydefaconconst (); car. introduce ();}}
Through reflection, we can see that we can directly set the private attributes of the object,
You can also assign a value to an attribute by calling the Set Method of the attribute,
In the Spring framework, IOC dependency injection turns to a large amount of use of the reflection mechanism of Java itself,
The framework encapsulates our handwritten code so that we can directly configure it through the spring configuration file.
There are not many classes required for Java class reflection. They are field, constructor, method, class, and object. Below I will give a simple description of these classes.
Field Class: provides information about the attributes of a class or interface and its dynamic access permissions. The reflected field may be a class (static) attribute or instance attribute. A simple understanding of it can regard it as a class that encapsulates the attributes of the reflection class.
Constructor class: Provides information about a single constructor of the class and its access permissions. This class is different from the field class. The field class encapsulates the attributes of the reflection class, while the constructor class encapsulates the constructor method of the reflection class.
Method class: Provides information about a separate method on a class or interface. The reflected methods may be class methods or instance methods (including abstract methods ). This class is not hard to understand. It is a class used to encapsulate reflection class methods.
Class class: an instance of a class indicates the classes and interfaces in a running Java application. Enumeration is a type, and annotation is an interface. Each array is a class mapped to a Class Object. All arrays with the same element type and dimension share the class object.
Java. Lang. class provides four independent reflection calls for any of the following three components: constructor, field, and method to obtain information in different ways.
All calls follow a standard format.
A set of reflection calls used to find constructors:
L constructor getconstructor (class [] Params) -- obtains a common constructor that uses special parameter types,
L constructor [] getconstructors () -- obtain all public constructors of the class
L constructor getdeclaredconstructor (class [] Params) -- obtains constructors using specific parameter types (unrelated to the access level)
L constructor [] getdeclaredconstructors () -- obtain all constructors of the class (irrelevant to the access level)
The class reflection call for obtaining field information is different from the call for accessing constructor. The field name is used in the parameter type array:
L field getfield (string name) -- Obtain the named public field
L field [] getfields () -- obtain all public fields of the class
L field getdeclaredfield (string name) -- Obtain the name field of the class declaration
L field [] getdeclaredfields () -- obtain all fields declared by class
Function used to obtain method information:
L method getmethod (string name, class [] Params) -- use a specific parameter type to obtain the named public Method
L method [] getmethods () -- obtain all public methods of the class
L method getdeclaredmethod (string name, class [] Params) -- Obtain the class declaration naming method by using the parameter type of close-up.
L method [] getdeclaredmethods () -- obtain all methods declared by class