Documentation

The Java™ Tutorials

Trail: The Reflection APITrail:反射API

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. 反射通常用于需要能够检查或修改Java虚拟机中运行的应用程序的运行时行为的程序。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.测试工具可以利用反射来系统地调用类上定义的可发现集API,以确保测试套件中的高级别代码覆盖率。

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. 由于反射涉及动态解析的类型,因此无法执行某些Java虚拟机优化。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. 由于反射允许代码执行非反射代码中非法的操作,例如访问private字段和方法,因此使用反射可能会产生意外的副作用,这可能导致代码功能失调,并可能破坏可移植性。Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.反射代码打破了抽象,因此可能会随着平台的升级而改变行为。

Trail Lessons试讲课

This trail covers common uses of reflection for accessing and manipulating classes, fields, methods, and constructors. 本教程介绍了反射在访问和操作类、字段、方法和构造函数时的常见用法。Each lesson contains code examples, tips, and troubleshooting information.每一课都包含代码示例、提示和疑难解答信息。

Classes
This lesson shows the various ways to obtain a Class object and use it to examine properties of a class, including its declaration and contents.本课程介绍获取Class对象并使用它检查类的属性(包括其声明和内容)的各种方法。
Members成员
This lesson describes how to use the Reflection APIs to find the fields, methods, and constructors of a class. 本课程介绍如何使用反射API查找类的字段、方法和构造函数。Examples are provided for setting and getting field values, invoking methods, and creating new instances of objects using specific constructors.提供了一些示例,用于设置和获取字段值、调用方法以及使用特定构造函数创建对象的新实例。
Arrays and Enumerated Types数组和枚举类型
This lesson introduces two special types of classes: arrays, which are generated at runtime, and enum types, which define unique named object instances. 本课程介绍两种特殊类型的类:在运行时生成的数组和定义唯一命名对象实例的enum类型。Sample code shows how to retrieve the component type for an array and how to set and get fields with array or enum types.示例代码显示如何检索数组的组件类型,以及如何设置和获取具有数组或enum类型的字段。

Note: 

The examples in this trail are designed for experimenting with the Reflection APIs. 本教程中的示例是为实验反射API而设计的。The handling of exceptions therefore is not the same as would be used in production code. 因此,异常的处理与生产代码中使用的不同。In particular, in production code it is not recommended to dump stack traces that are visible to the user.特别是,在生产代码中,不建议转储用户可见的堆栈跟踪。



Previous page: Beginning of Tutorial
Next page: Classes