First speech reflection Application scenario & Features
1. Definition of Reflection
Gets the contents of the specified class and the dynamic invocation class dynamically. The program user is provided with an interface that can customize the function, which improves the extensibility of the program.
2. Realization method
By configuring the file, the program consumer sets the custom class name in the configuration file, calls the configuration file inside the program, gets the bytecode file of the class and the class object, and then invokes the member methods and member variables in the class, and realizes the interaction between the idiom and the user.
Where the class that implements the interface is defined by the user of the program itself.
3. Illustration example
Second, get the bytecode file object & Create Class object
1. Class
class--is used to describe the class of bytecode files, and the technique of reflection is implemented by this class.
This class provides and obtains methods for the constructor object, generic Method object, and Field object in the class bytecode file corresponding to the class.
2. How to get the object of a bytecode file
method One: obtained through the GetClass () method in the object class, which needs to be called through the object of the class.
Here's an example: Get the bytecode file object in the person class
Person p = new person ();--Create Class object
Class clazz = P.getclass ();--Gets the bytecode file object through the method GetClass () in object.
Description: This method needs to clarify the specific class and create objects of that class, which is cumbersome.
mode two: any data type has a static property ". Class", which can be called directly with the class: Class clazz = Person.class;
Description: The method is simpler than mode one, but it is still not sufficient to define the class name and static members of the class.
Way Three: The forname (String className) method in class, where the ClassName is complete, including the package name (for example, Cn.itcast.bean.Person).
The code is implemented as follows:
String className = "Cn.itcast.bean.Person";--the type here can be read through the configuration file, the class is defined by the program consumer, and the type is entered into the configuration file at the specified location, in the incoming program.
Class Clazz = forname (className);--The program obtains the corresponding bytecode file object for the class by the class name obtained from the configuration file.
Description: This class can be obtained only by the string name of the given class, which is more extensible.
The above three ways code example:
Third, get constructors & dynamically create class objects
1. Overview
To create a class object, you need to call the class's constructor (except in the case where the class internal definition method gets the object), and the class class defines the method Newinstance () that gets the class object, which invokes the parameterless constructor in the class;
The code is implemented as follows:
2. Call the class with a parameter constructor to create the class object
The Newinstance () method in class can only invoke a parameterless constructor in a class, and to create a class object from a parameter constructor in a class, get the parameter constructor in the class first.
Implementation method:
First get the constructor object with parameters in the class (using
GetConstructor (String.class,int.class) method), according to their own needs to determine the parameter list;
Then call method newinstance (String str,int i) by acquiring the constructor object to invoke the class with the parameter constructor to create the class object, the code is implemented as follows:
How to get a method in a class
1. Overview
After we get to the class's bytecode file object, we can get the object of the generic method from that object, then get the general method with the method object.
2. Get and invoke the general method
- Gets the byte file object;
Class clazz = Class.forName (className);
- Gets the method object according to the method name;
method = Clazz.getmethod (Methodname,string.class,int.class);
Take null parameter function: Clazz.getmethod (methodname,null)
Object obj = Clazz.newinstance ();--you need to create the class object first.
Methods.invoke (obj, "Zhangsan", "23");
Call static method: Methods.invoke (null, "Zhangsan", "23");--Call static method, no object required.
Summary: Gets the method object through the bytecode file object, invoking the method through Invoke () in the method class. If a non-static method is called, Invoke () has a class object in the actual argument, and if a static method is called, the class object is not required and null is substituted. Where there are no parameters, use NULL instead.
Talk about getting fields & violent access
1. Overview
The class class defines the method that gets the field object, and gets the Field object, which you can use to access the fields in the class (get, set, and so on).
2. Get and set field steps
- Gets the byte-code file object;--class.forname ()
- Call Method GetField (FieldName) in class with a bytecode file object or
Getdeclaredfield (FieldName) to obtain the custom name of the Field object;
GetField (fieldName)--Gets the object of the common field;
Getdeclaredfield (fieldName)--Gets the object of the field;
- Set a field;--field.set (obj,90); you need to specify a class object.
Note: For private fields, setting the value of the field here will fail, and we can use brute force access to cancel the access check and set the value of the field.
3. Private members in violent access classes--using the Accessiableobject class
Definition: The Accessiableobject class is the parent class of the field class, method class, constructor class, in which methods Setaccessible () can cancel permission checks for members in a class so that the field class object, method Class object, The constructor class object can access private fields, methods in the class;
Implementation code:
Field.setaccessible (true);--Remove permission access to the field, that is, brute-force access.
Field.set (obj,90);--Set private field values.
Description: The program declares a member as private, so it is generally not accessible to private members, just imagine protecting it.
Sixth lecture Reflection Application Case
Demo Notebook PC use USB Interface Example:
requirement: The laptop calls the device connected to the USB interface, requiring different devices to perform different methods.
Ideas :
1, the computer needs to use some peripheral equipment, such as mouse, keyboard, u disk, etc., in order to improve the scalability, should reduce the coupling of these devices and computers, so it is necessary to introduce interface technology (no matter what device access, as long as the USB rules, can be used by the computer).
2, however, if the USB interface to the new device, you need to modify the original code to invoke the new device, which is cumbersome, so you can introduce the technology of reflection, provide a configuration file.
3, we only need to define the function of the access device, and in the configuration file incoming device information, without modifying the original code, the computer can also use the access device.
Code Demo:
Dark Horse programmer-java Basics-Reflex