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