博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java反射机制的缺点
阅读量:4589 次
发布时间:2019-06-09

本文共 2315 字,大约阅读时间需要 7 分钟。

译自官方指南(Tutorial):

 

反射的用途 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.

反射被广泛地用于那些需要在运行时检测或修改程序行为的程序中。这是一个相对高级的特性,只有那些语言基础非常扎实的开发者才应该使用它。如果能把这句警示时刻放在心里,那么反射机制就会成为一项强大的技术,可以让应用程序做一些几乎不可能做到的事情。

 

 

反射的缺点 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.

反射包括了一些动态类型,所以JVM无法对这些代码进行优化。因此,反射操作的效率要比那些非反射操作低得多。我们应该避免在经常被 执行的代码或对性能要求很高的程序中使用反射。
安全限制  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. 使用反射技术要求程序必须在一个没有安全限制的环境中运行。如果一个程序必须在有安全限制的环境中运行,如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. 由于反射允许代码执行一些在正常情况下不被允许的操作(比如访问私有的属性和方法),所以使用反射可能会导致意料之外的副作用--代码有功能上的错误,降低可移植性。反射代码破坏了抽象性,因此当平台发生改变的时候,代码的行为就有可能也随着变化。

 

转载于:https://www.cnblogs.com/dyllove98/archive/2013/06/15/3137620.html

你可能感兴趣的文章
[USACO4.2] 草地排水 Drainage Ditches (最大流)
查看>>
dotnetcore+vue+elementUI 前后端分离 三(前端篇)
查看>>
gdb输入输出重定向
查看>>
包含.h就可以用其对应的函数
查看>>
mysql忘记root密码的处理方法
查看>>
Newtonsoft.Json之JArray, JObject, JProperty,JValue
查看>>
OO Summary (Homework 9-11)
查看>>
fedora 解决yumBackend.py进程CPU占用过高
查看>>
NTP 协议介绍
查看>>
软件测试 · 白盒测试
查看>>
docker-compose exec时出现"fork/exec /proc/self/exe: no such file or directory" 报错
查看>>
IIS的安装及网站发布的图解
查看>>
VM虚拟机安装苹果雪豹操作系统
查看>>
dos进去mysql导入数据库
查看>>
Oracle单表去重复(一)
查看>>
C#中如何获取一个二维数组的两维长度,即行数和列数?以及多维数组各个维度的长度?...
查看>>
JSON字符串互相转换的三种方式和性能比较
查看>>
C++中cout输出字符型指针地址值的方法
查看>>
Java运算符法则
查看>>
深入理解java异常处理机制
查看>>