Documentation

The Java™ Tutorials
Hide TOC
Type Annotations and Pluggable Type Systems类型批注和可插入类型系统
Trail: Learning the Java Language
Lesson: Annotations

Type Annotations and Pluggable Type Systems类型批注和可插入类型系统

Before the Java SE 8 release, annotations could only be applied to declarations.在Java SE 8发布之前,批注只能应用于声明。As of the Java SE 8 release, annotations can also be applied to any type use.从JavaSE8版本开始,批注也可以应用于任何类型的使用。This means that annotations can be used anywhere you use a type.这意味着可以在使用类型的任何位置使用批注。A few examples of where types are used are class instance creation expressions (new), casts, implements clauses, and throws clauses.使用类型的几个示例是类实例创建表达式(new)、强制转换、implements子句和throws子句。This form of annotation is called a type annotation and several examples are provided in Annotations Basics.这种形式的批注称为类型批注,批注基础中提供了几个示例。

Type annotations were created to support improved analysis of Java programs way of ensuring stronger type checking.创建类型批注是为了支持改进的Java程序分析,从而确保更强的类型检查。The Java SE 8 release does not provide a type checking framework, but it allows you to write (or download) a type checking framework that is implemented as one or more pluggable modules that are used in conjunction with the Java compiler.JavaSE8版本不提供类型检查框架,但它允许您编写(或下载)一个类型检查框架,该框架作为一个或多个可插入模块实现,并与Java编译器一起使用。

For example, you want to ensure that a particular variable in your program is never assigned to null; you want to avoid triggering a NullPointerException.例如,您希望确保程序中的某个特定变量从未分配给null;您希望避免触发NullPointerExceptionYou can write a custom plug-in to check for this.您可以编写一个自定义插件来检查这一点。You would then modify your code to annotate that particular variable, indicating that it is never assigned to null.然后,您将修改代码以批注该特定变量,表明该变量从未赋值为nullThe variable declaration might look like this:变量声明可能如下所示:

@NonNull String str;

When you compile the code, including the NonNull module at the command line, the compiler prints a warning if it detects a potential problem, allowing you to modify the code to avoid the error.编译代码时,包括命令行中的NonNull模块,如果编译器检测到潜在问题,将打印警告,允许您修改代码以避免错误。After you correct the code to remove all warnings, this particular error will not occur when the program runs.更正代码以删除所有警告后,程序运行时不会发生此特定错误。

You can use multiple type-checking modules where each module checks for a different kind of error.您可以使用多个类型检查模块,其中每个模块检查不同类型的错误。In this way, you can build on top of the Java type system, adding specific checks when and where you want them.通过这种方式,您可以在Java类型系统的基础上进行构建,在需要的时间和地点添加特定的检查。

With the judicious use of type annotations and the presence of pluggable type checkers, you can write code that is stronger and less prone to error.通过明智地使用类型批注和可插入类型检查器,您可以编写更强大、更不容易出错的代码。

In many cases, you do not have to write your own type checking modules.在许多情况下,您不必编写自己的类型检查模块。There are third parties who have done the work for you.有第三方为您完成了工作。For example, you might want to take advantage of the Checker Framework created by the University of Washington.例如,您可能想利用华盛顿大学创建的Checker框架。This framework includes a NonNull module, as well as a regular expression module, and a mutex lock module.该框架包括一个NonNull模块、一个正则表达式模块和一个互斥锁模块。For more information, see the Checker Framework.有关详细信息,请参阅Checker框架


Previous page: Predefined Annotation Types
Next page: Repeating Annotations