反射的使用

反射通常用于在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

http://docs.oracle.com/javase/tutorial/reflect/index.html

Why Reflection is slowly?(Trail: The Reflection API)的更多相关文章

  1. LWL-Hitokoto API(一言-纯净API)

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:liwanglin12链接:https://blog.lwl12.com/read/hitokoto-api.html来源:L ...

  2. LwIP - raw/callback API、协议栈API(sequential API)、BSD API(或者说 SOCKET API)

    1.使用raw/callback API编程,用户编程的方法是向内核注册各种自定义的回调函数,回调函数是与内核实现交换的唯一方式. recv_udp, accept_function, sent_tc ...

  3. vue中比较完美请求的栗子(使用 axios 访问 API)

    vue中比较完美请求的栗子(使用 axios 访问 API) 官网地址:https://vuejs.bootcss.com/v2/cookbook/using-axios-to-consume-api ...

  4. HBase操作(Shell与Java API)

    版权声明:本文为博主原创文章,未经博主允许不得转载.     转: http://blog.csdn.net/u013980127/article/details/52443155 下面代码在Hado ...

  5. FFmpeg开发笔记(五):ffmpeg解码的基本流程详解(ffmpeg3新解码api)

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  6. WM_PAINT产生原因有2种(用户操作和API)——WM_PAINT和WM_ERASEBKGND产生时的先后顺序不一定(四段讨论)

    1. 当WM_PAINT不是由InvalidateRect产生时,即由最大化,最小化等产生时,或者移动产生(移动有时只会产生WM_ERASEBKGND消息)系统先发送WM_ERASEBKGND消息,再 ...

  7. taobao_api项目开坑,自主完成淘宝主要接口的开发-版本:卖家版(非淘宝api)

    项目名称:taobao_api 项目目的:独立实现各个淘宝操作的相关api,不依赖淘宝提供的api,而是自己实现接口 前期实现接口:已付款订单查询(自动更新), 订单发货 , 订单备注 应用场景:中小 ...

  8. HTML5 WebSocket 权威指南 学习一 (第二章 WebSocket API)

    WebSocket 协议两种URL方案 ws 客户端和服务器之间的非加密流量 wss 客户端和服务器之间的加密流量 WebSocket Secure 表示使用传输层安全性(SSL)的WebSocket ...

  9. C#中读写配置参数文件(利用Windows的API)

      读配置文件与写配置文件的核心代码如下:   [DllImport("kernel32")] // 读配置文件方法的6个参数:所在的分区(section).键值. 初始缺省值. ...

随机推荐

  1. hibernate----(Hql)查询

    package com.etc.test; import java.util.List;import java.util.Properties; import org.hibernate.Query; ...

  2. 10 months then free? 10个月,然后自由

    Parole board to recommend Oscar Pistorius be released in August 假释委员会将建议奥斯卡·皮斯托瑞斯在8月份被释放 By Don Melv ...

  3. uC/OS-II任务(OS_task)块

    /*************************************************************************************************** ...

  4. Spring MVC学习笔记——给Controller和视图传值

    一.给Controller传值,值将显示在控制台 1.第一种:使用@RequestParam,改HelloController.java //RequestMapping表示用哪一个url来对应 @R ...

  5. JavaWeb学习笔记——访问静态HTML网页

    基础知识: 1.一个标准 的WEB应用,在根目录下必须有一个WEB-INF文件夹,文件夹下必须有一个WEB.xml文件. 2.classes文件夹用来存放编译的class文件. 3.lib文件夹用来存 ...

  6. MySQL数据导出与导入

    工具 mysql mysqldump 应用举例 导出 导出全库备份到本地的目录 mysqldump -u$USER -p$PASSWD -h127.0.0.1 -P3306 --routines -- ...

  7. 固定导航(Sticky nav)

    方法1: <div class="footer"></div> .footer{ position:fixed; bottom:0; left:0; wid ...

  8. C#中返回值封装

    在平时开发过程中常常需要取一个方法的返回值,BOSS写了一个返回值类,做个练习以备不时之需: 返回值支持泛型和非泛型 先贴上代码: 非泛型返回值类: using System; using Syste ...

  9. 详解FindBugs的各项检测器 .

    FindBugs是一个静态分析工具,在程序不需运行的情况下,分析class文件,将字节码与一组缺陷模式进行对比,试图寻找真正的缺陷或者潜在的性能问题.本文档主要详细说明FindBugs 2.0.3版本 ...

  10. php apache用户写文件夹权限设置

    php一般是以apache用户身份去执行的,把apache加入到存储你文件的父文件夹属组里去,然后改该父文件夹权限为775,这样属组成员就有写的权限,而apache属于这个组就可以改写该目录下所有文件 ...