The reflection of the Java Foundation

Source: Internet
Author: User

Java reflection is the mechanism by which the runtime obtains class information and, in turn, dynamically constructs objects at run time, invokes object methods, and modifies object properties. Baidu Encyclopedia definition: "The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any object, can invoke its arbitrary methods and properties; This dynamic access to information and the ability to dynamically invoke object methods is called the reflection mechanism of the Java language.

First, the use of reflection

The reflection mechanism of Java can do 3 things: The runtime creates the object, the runtime invokes the method, and the run-time read-write property. In turn, the following functions are implemented:
Call some private methods to implement black technology. such as dual-SIM messaging, set the status bar color, auto-hang phone and so on.

It realizes serialization and deserialization, such as Orm,json parsing of PO.

Achieve cross-platform compatibility, such as the implementation of SocketImpl in the JDK.

Through XML or annotations, we implement the functions of dependency Injection (DI), annotation processing, dynamic agent, unit testing and so on. such as retrofit, spring or dagger.

Ii. advantages and disadvantages of Java reflection

Advantages:

(1) The ability to dynamically acquire instances of classes at runtime greatly improves the flexibility and extensibility of the system.
(2) combined with Java Dynamic compilation, it can achieve extremely powerful functions

Disadvantages:
(1) Low performance with reflection
(2) Using reflection is relatively unsafe
(3) The encapsulation of the class is destroyed, and the private methods and properties of the class can be obtained by reflection.

Third, the use of Java reflection

The primary reflection API, which is used to generate information about classes, interfaces, or objects in the current Java virtual machine.

Class: A reflection of the core class, you can get the properties of the class, methods and other content information.

Field class: Java.lang.reflect. Represents the properties of a class, and you can get and set the property values in the class.

Method class: Java.lang.reflect. Represents a method of a class that can be used to obtain information about a method in a class or to execute a method

Construcor class: Java.lang.reflect. Represents the construction method of a class.

Steps to use:

Get the Java.lang.Class object for the class you want to manipulate
Method of calling Class
Use the reflection API to manipulate this information

The reflection of the Java Foundation

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.