computer sciencereflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime.[1]

A language supporting reflection provides a number of features available at runtime that would otherwise be difficult to accomplish in a lower-level language. Some of these features are the abilities to:

  • Discover and modify source-code constructions (such as code blocks, classes, methods, protocols, etc.) as first-class objects at runtime.
  • Convert a string matching the symbolic name of a class or function into a reference to or invocation of that class or function.
  • Evaluate a string as if it were a source-code statement at runtime.
  • Create a new interpreter for the language's bytecode to give a new meaning or purpose for a programming construct.

These features can be implemented in different ways. In MOO, reflection forms a natural part of everyday programming idiom. When verbs (methods) are called, various variables such as verb (the name of the verb being called) and this (the object on which the verb is called) are populated to give the context of the call. Security is typically managed by accessing the caller stack programmatically: Since callers() is a list of the methods by which the current verb was eventually called, performing tests on callers()[1] (the command invoked by the original user) allows the verb to protect itself against unauthorised use.

Compiled languages rely on their runtime system to provide information about the source code. A compiled Objective-C executable, for example, records the names of all methods in a block of the executable, providing a table to correspond these with the underlying methods (or selectors for these methods) compiled into the program. In a compiled language that supports runtime creation of functions, such as Common Lisp, the runtime environment must include a compiler or an interpreter.

Reflection can be implemented for languages not having built-in reflection facilities by using a program transformation system to define automated source-code changes.

Uses[edit]

Reflection can be used for observing and modifying program execution at runtime. A reflection-oriented program component can monitor the execution of an enclosure of code and can modify itself according to a desired goal related to that enclosure. This is typically accomplished by dynamically assigning program code at runtime.

In object-oriented programming languages such as Java, reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.

Reflection can be used to adapt a given program to different situations dynamically. Reflection-oriented programming almost always requires additional knowledge, framework, relational mapping, and object relevance in order to take advantage of more generic code execution.

Reflection is often used as part of software testing, such as for the runtime creation/instantiation of mock objects.

Reflection is also a key strategy for metaprogramming.

In some object-oriented programming languages, such as C# and Java, reflection can be used to override member accessibility rules. For example, reflection makes it possible to change the value of a field marked "private" in a third-party library's class.

https://en.wikipedia.org/wiki/Reflection_(computer_programming)

最新文章

  1. 细说JAVA反射
  2. Web 开发中应用 HTML5 技术的10个实例教程
  3. Mac之vim普通命令使用[转]
  4. appcan.windw.open appcan.frame.open appcan.window.openPopOver evaluateScript
  5. 《Java程序设计》第2周学习总结
  6. 前端面试库_JS部分_02
  7. WebService 实现BS环境与BS环境传递参数,根据参数生成txt文档
  8. 【原创】Linux编译内核
  9. MySQL索引1
  10. C++删除文件末尾字符
  11. Linux数字雨
  12. 哎呀,搬运blog好累啊,96篇呢QwQ
  13. html5手势操作与多指操作封装与Canvas图片裁切实战
  14. python 路径处理
  15. STM32之USB电路(摘要笔记)
  16. flink架构介绍
  17. KuDu论文解读
  18. jquery ajax參数加点号状态200进error
  19. centos如何设置定时任务
  20. poj2243 Knight Moves(BFS)

热门文章

  1. jsoup解析页面
  2. bzoj 4372: 烁烁的游戏 动态点分治_树链剖分_线段树
  3. vue .sync修饰符
  4. css超出不换行可滑动
  5. BZOJ 3439 Kpm的MC密码 (Trie树+线段树合并)
  6. SpringBoot 对静态资源的映射规则
  7. 在使用SSH+JPA开发中,ajax使用ObjectMapper类从后台向前台传值
  8. 2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence
  9. daning links 系列
  10. ASP.NET CORE--WIN10上无法单步调试解决方法