Java reflection of Chuanzhi podcast Learning

Source: Internet
Author: User

In addition, Mr. Bi explained the reflection mechanism for us. Although reflection may not be involved in the development process in the future, reflection is indeed a powerful java tool, it allows us to create flexible code that can be configured during running without the need to represent the source link between components. For example, the Tomcat Class Loader mechanism is used by many frameworks.
1. What is reflection?
The concept of reflection was first proposed by Smith in 1982. It mainly refers to the ability of a program to access, detect, and modify its own state behavior. Reflection is one of the characteristics of the java programming language. It also allows running Java programs to check themselves, or "self-Review", and can directly manipulate internal properties of the program.
Ii. Classes required for java reflection
1. Class: its instance indicates the classes and interfaces in the running java application.
2. Constructor class: Provides information about a single Constructor of the class and its access permissions.
3. Method class: Provides information about a separate Method of a class or interface.
4. Array class: provides static methods for dynamically creating arrays or accessing arrays.
5. Field Class: provides information about the attributes of a class or interface, as well as its dynamic access permissions.
Iii. Reflection Functions
We have listed related classes above. What can we do with these classes?
1) Get the Class Object of the specified Class
Method 1: Use the object. getClass. (For example, Person p; Class c = p. getClass ();)
Method 2: Use the forName method of the Class (for example, Class c = Class. forName (Class Name );)
Method 3: obtain data by type name. class (Class c = int. class)
2) construct the object of any class at runtime. The steps are as follows:
① Create an object based on the class name
② Return the newly created object
L use the non-argument constructor method, and only the newInstance method of the class object needs to be used.
Class c = Class. forName (Class Name); Person p = (Person) c. newInstance;
L use the construction method with parameters as follows:
① Obtain the object of the specified class (same as above)
② Obtain the constructor Class object that meets the requirements of specified parameters through the Class Object
Constructor cons = c. getConstructor (String. class)
③ Call the newInstance method of the specified Constructor object, pass in the parameter value of the pair, and create the object.
Person p = (Person) cons. newInstance ("Zhang San ");
3) Call the method of any object at runtime. through reflection, you can even call the private method.
① Obtain the object of the specified class (same as above)
② Create an instance as required
③ Use the getDeclaredMethods of the Class Object (you can select different methods as needed) to obtain an instance of the Method and call the Method through the invoke Method
Method m = c. getDeclaredMethods ("setName", String. class );
Returnvalue = m. invoke (p, "zhangsan ");
NOTE: If it is a private Method, you can call setAccessible (true) of the Method object corresponding to this Method to cancel the security check of this Method.
4) Call the attributes of any object at runtime
① Obtain the object of the specified class (same as above)
② Create an instance as required
③ Get the instance of the Field Class through getDeclaredField of the Class Object (you can select different methods as needed), modify the value of the object through the Set method, and get the value of the object.
Field f = c. getDeclaredField ("name ");
F. set (p, "Zhang San"); System. out. println (f. get (p ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.