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).键值. 初始缺省值. ...
随机推荐
- 《JavaScript权威指南》学习笔记 第三天 找个对象
现实生活中真的对象没有找到,在JavaScript 里左一个对象又一个对象,搞的我也是晕晕乎乎不知所云.人事复杂,人心难懂.我虽然是文科生,但是也不善于巧言.还是在js里面找找对象吧.那么我们今天就从 ...
- iOS qrcode 默认尺寸与修改
四种容错格式的尺寸:27.31.31.35. // 5.将CIImage转换成UIImage,并放大显示 UIImage *imagex = [UIImage imageWithCIImage:out ...
- 生成秘钥文件 sn.exe(Strong Name Tool)
Visual Studio 内置 Strong Name Tool, 我们直接运行"VS开发人员命令提示"就可以生成秘钥文件. 秘钥文件包含公钥和私钥. 来看这个例子: 在文件夹下 ...
- swfUpload 上传图片
前端: <script src="~/Scripts/swfupload/swfupload.js"></script> <script src=&q ...
- easyUI datagrid editor扩展dialog
easyUI datagrid简单使用:着重两点1.editor对象的click事件:2.将dialog窗体内的值填写到当前正编辑的单元格内 <!DOCTYPE html> <htm ...
- Spring MVC学习笔记——注解式控制器
- 《深入理解bootstrap》读书笔记:第4章 CSS组件(下)
十. 标签(.label类,label-xxx) 高亮一些标题部分. 1 2 3 4 5 6 <h1>HELLO<span class="label label-defau ...
- 端口扫描(TCP)
还待优化... #include <string.h> #include <WinSock.h> #include <stdio.h> #pragma commen ...
- MagicalRecord简单使用小记
一般采用pod安装,导入框架 #import <CoreData+MagicalRecord.h> - (BOOL)application:(UIApplication *)applica ...
- AWK改变输入输出分隔符实例分析
awk默认从STDIN接受数据,打印文本到STDOUT. awk的默认输入和输出分隔符: FS : 输入字段分隔符,默认空格. RS : 输入行分隔符,默认\n. OFS : 输出字段分隔符,默认空格 ...