Why reflection can reflect, how is it implemented internally

Reflection mainly refers to the ability of a program to access, detect and modify its own state or behavior. In computer science, reflections are a class of applications that are self-describing and self-controlling. This kind of application realizes the description and detection of its own behavior through a certain mechanism, and can adjust or modify the state and related semantics of the behavior described by the application according to the state and result of its own behavior.
  The reflection mechanism in Java is called Reflection (when you see this word, your first thought should be to search the development documentation). It allows a running Java program to examine itself and directly manipulate the program's internal properties or methods. The Reflection mechanism allows a program to use the Reflection APIs to obtain internal information about any class with a known name during execution, including: package, type parameters, superclass, implemented interfaces, inner classes, outer classes, fields, constructors, methods, modifiers and so on, and can dynamically generate Instances, change the content of fields or invoke methods during the execution process.
  Well, to understand these, then we know that we can use the reflection mechanism to dynamically call some protected or even private methods or classes in Java programs, which can largely meet some of our more special needs. Of course you will ask, what is the use of reflection mechanism in the Android platform?
  When we are developing Android programs, in order to debug the program and quickly locate the error points of the program, we will download the source code of the corresponding version of the Android SDK from the Internet (here provides a download link for version 2.3.3). You will find that many classes or methods are often marked with the "@hide" annotation. Its function is to make this method or class invisible when generating the SDK, then our program may not be compiled and passed, and when it is finally released , there may be some problems.
  So, for this problem, the first method is to remove the "@hide" tag in the Android source code, and then recompile to generate an SDK. Another way is to use the Java reflection mechanism, which can be used to access methods that have access rights or modify their fields.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325941517&siteId=291194637