淺析Java 反射機(jī)制的用途和缺點(diǎn)
反射的用途 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.
反射被廣泛地用于那些需要在運(yùn)行時(shí)檢測(cè)或修改程序行為的程序中。這是一個(gè)相對(duì)高級(jí)
的特性,只有那些語(yǔ)言基礎(chǔ)非常扎實(shí)的開(kāi)發(fā)者才應(yīng)該使用它。如果能把這句警示時(shí)刻放在心
里,那么反射機(jī)制就會(huì)成為一項(xiàng)強(qiáng)大的技術(shù),可以讓?xiě)?yīng)用程序做一些幾乎不可能做到的事情。
反射的缺點(diǎn) 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.
盡管反射非常強(qiáng)大,但也不能濫用。如果一個(gè)功能可以不用反射完成,那么最好就不用。
在我們使用反射技術(shù)時(shí),下面幾條內(nèi)容應(yīng)該牢記于心:
性能第一 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.
反射包括了一些動(dòng)態(tài)類(lèi)型,所以 JVM 無(wú)法對(duì)這些代碼進(jìn)行優(yōu)化。因此,反射操作的效
率要比那些非反射操作低得多。我們應(yīng)該避免在經(jīng)常被 執(zhí)行的代碼或?qū)π阅芤蠛芨叩某?
序中使用反射。
安全限制 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.
使用反射技術(shù)要求程序必須在一個(gè)沒(méi)有安全限制的環(huán)境中運(yùn)行。如果一個(gè)程序必須在有
安全限制的環(huán)境中運(yùn)行,如 Applet,那么這就是個(gè)問(wèn)題了。
內(nèi)部暴露 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.
由于反射允許代碼執(zhí)行一些在正常情況下不被允許的操作(比如訪問(wèn)私有的屬性和方
法),所以使用反射可能會(huì)導(dǎo)致意料之外的副作用--代碼有功能上的錯(cuò)誤,降低可移植性。
反射代碼破壞了抽象性,因此當(dāng)平臺(tái)發(fā)生改變的時(shí)候,代碼的行為就有可能也隨著變化。
相關(guān)文章
Java多線程基礎(chǔ) 線程的等待與喚醒(wait、notify、notifyAll)
這篇文章主要介紹了Java多線程基礎(chǔ) 線程的等待與喚醒,需要的朋友可以參考下2017-05-05Springboot整合Dozer實(shí)現(xiàn)深度復(fù)制的方法
Dozer是一種Java?Bean到Java?Bean的映射器,遞歸地將數(shù)據(jù)從一個(gè)對(duì)象復(fù)制到另一個(gè)對(duì)象,它是一個(gè)強(qiáng)大的,通用的,靈活的,可重用的和可配置的開(kāi)源映射框架,本文給大家介紹Springboot整合Dozer的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧2022-03-03Java的Tomcat和Servlet的運(yùn)行原理詳解
這篇文章主要為大家詳細(xì)介紹了Java的Tomcat和Servlet,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03Java?工具類(lèi)實(shí)現(xiàn)音頻音量提升
本文主要介紹了可以將音頻提升音量的一個(gè)java工具類(lèi)示例代碼,代碼具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴來(lái)了解一下吧,,希望能夠給你帶來(lái)幫助2021-11-11Eclipse/MyEclipse轉(zhuǎn)IntelliJ IDEA完全攻略(圖文)
這篇文章主要介紹了Eclipse/MyEclipse轉(zhuǎn)IntelliJ IDEA完全攻略(圖文),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01解決poi導(dǎo)出時(shí)單元格樣式被覆蓋問(wèn)題
這篇文章主要介紹了解決poi導(dǎo)出時(shí)單元格樣式被覆蓋問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11通過(guò)Java帶你了解網(wǎng)絡(luò)IO模型
這篇文章將通過(guò)Java帶大家了解網(wǎng)絡(luò)IO模型,包括BIO,NoBlockingIO,NIO(NewIO),AIO等做了詳細(xì)得介紹,感興趣的小伙伴可以參考閱讀本文2023-05-05Java中Arrays.asList()方法詳解及實(shí)例
這篇文章主要介紹了Java中Arrays.asList()方法將數(shù)組作為列表時(shí)的一些差異的相關(guān)資料,需要的朋友可以參考下2017-06-06