Why Reflection is slowly?(Trail: The Reflection API)
反射的使用
反射通常用于在JVM中应用程序运行中需要检查或者修改运行时行为的项目。这是一个相对高级的特性,并且仅仅可以被对深刻理解java原理的开发者使用。这里给出一个警告的意见,反射是一个强大的技术,而且可以让应用执行不可能的操作。
扩展属性
一个应用程序可以通过他们的完全限定的名称创建可扩展的对象的实例使用外部的,用户定义的类。
类浏览器和可视化开环境
一个类浏览器需要枚举类中的成员。可视化开发环境可以根据反射选择到正确的类型信息,帮助开发者写出正确的代码。
调试器和测试工具
调试器需要能检查类中的私有成员。测试工具可以利用反射系统调用类中可被发现的API定义,确保在测试套件中的代码的高覆盖率。
反射的缺点
反射是强大的,但是不应该被滥用。如果不需要反射就能执行一个操作,那么最好避免使用反射。记住下面通过反射访问代码的一些关键点。
性能开销
因为反射调用是调用类型的,导致无法进行JVM优化。所以,反射的操作的性能会比不使用反射的要慢。应该避免在高性能场景的编码中频繁的调用。
安全限制
反射需要在安全管理器下运行时可能不存在的运行时权限。对于运行在受限制的安全上下文中的代码(比如一个Applet)这是很重要的一点。
内部暴露
反射可以执行一些在非反射中非法的代码,比如访问私有属性和方法,使用反射可能会导致不可预料的问题,比如代码功能不可用、破坏可移植性,因此平台升级中可能会改变行为(业务逻辑)。
以下为原文:
Uses of Reflection
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
Extensibility Features
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.
Class Browsers and Visual Development Environments
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.
Debuggers and Test Tools
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.
Drawbacks of Reflection
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.
Performance Overhead
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
Security Restrictions
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.
Exposure of Internals
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.
Come from
Why Reflection is slowly?(Trail: The Reflection API)的更多相关文章
- LWL-Hitokoto API(一言-纯净API)
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:liwanglin12链接:https://blog.lwl12.com/read/hitokoto-api.html来源:L ...
- LwIP - raw/callback API、协议栈API(sequential API)、BSD API(或者说 SOCKET API)
1.使用raw/callback API编程,用户编程的方法是向内核注册各种自定义的回调函数,回调函数是与内核实现交换的唯一方式. recv_udp, accept_function, sent_tc ...
- vue中比较完美请求的栗子(使用 axios 访问 API)
vue中比较完美请求的栗子(使用 axios 访问 API) 官网地址:https://vuejs.bootcss.com/v2/cookbook/using-axios-to-consume-api ...
- HBase操作(Shell与Java API)
版权声明:本文为博主原创文章,未经博主允许不得转载. 转: http://blog.csdn.net/u013980127/article/details/52443155 下面代码在Hado ...
- FFmpeg开发笔记(五):ffmpeg解码的基本流程详解(ffmpeg3新解码api)
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- WM_PAINT产生原因有2种(用户操作和API)——WM_PAINT和WM_ERASEBKGND产生时的先后顺序不一定(四段讨论)
1. 当WM_PAINT不是由InvalidateRect产生时,即由最大化,最小化等产生时,或者移动产生(移动有时只会产生WM_ERASEBKGND消息)系统先发送WM_ERASEBKGND消息,再 ...
- taobao_api项目开坑,自主完成淘宝主要接口的开发-版本:卖家版(非淘宝api)
项目名称:taobao_api 项目目的:独立实现各个淘宝操作的相关api,不依赖淘宝提供的api,而是自己实现接口 前期实现接口:已付款订单查询(自动更新), 订单发货 , 订单备注 应用场景:中小 ...
- HTML5 WebSocket 权威指南 学习一 (第二章 WebSocket API)
WebSocket 协议两种URL方案 ws 客户端和服务器之间的非加密流量 wss 客户端和服务器之间的加密流量 WebSocket Secure 表示使用传输层安全性(SSL)的WebSocket ...
- C#中读写配置参数文件(利用Windows的API)
读配置文件与写配置文件的核心代码如下: [DllImport("kernel32")] // 读配置文件方法的6个参数:所在的分区(section).键值. 初始缺省值. ...
随机推荐
- zabbix监控系列(3)之zabbix触发器格式配置
前言 今天公司线上环境新添一个监控项,监控 一些日志的是否出现错误,有错误就及时报警,那么问题来了 ,报警必须告诉我们哪个日志有错误,这样才能够快速定位到哪个日志有问题. 配置 zabbix-agen ...
- Node-webkit简介
Node-webkit 概述 Node-webkit 是Github 上一个非常热门的开源项目,它基于著名的浏览器开源项目 Chromium 和服务器端 JavaScript 实现 Node.js 的 ...
- Microsoft SQL Server Management Studio ------------------------------ 附加数据库 对于 服务器
http://zhidao.baidu.com/link?url=didvEEY86Kap_F9PnRAJMGoLXv63IW1fhElfiOpkkmalJ9mvZoqNULlGKcGHN31y_4z ...
- 几个有用的JavaScript/jQuery代码片段(转)
1. 检查数据是否包含在Array中 //jQuery实现 jQuery.inArray("value", arr); // 使用方法: if( jQuery.inArray(&q ...
- FPS
游戏的FPS跟什么有关 http://zhidao.baidu.com/link?url=jZJoN_-MTC9fWCBSBaL5vezBp13MRSv8PdCcQMbwX5tiFGzxNEHe7UB ...
- maven可选依赖(Optional Dependencies)和依赖排除(Dependency Exclusions)
我们知道,maven的依赖关系是有传递性的.如:A-->B,B-->C.但有时候,项目A可能不是必需依赖C,因此需要在项目A中排除对A的依赖.在maven的依赖管理中,有两种方式可以对依赖 ...
- phpcms v9 0day
index.php?m=member&c=index&a=login 后缀 username=phpcms&password=123456%26username%3d%2527 ...
- php实现发送邮件
smtp.php: <?php class smtp { /* Public Variables */ var $smtp_port; var $time_out; ...
- fedora23也会死机, 怎么办
现在使用的 fedora23 , 在有些 时候老是 死机, 有的是 firefos引起的, 但 更多的时候, 是 由 终端terminal gnome-terminal引起的, 特别是在 操作 vim ...
- python 运行时报错误SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2
File "1.py", line 2SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2, but no ...