##JSpecify 1.0.0 发布,为 Java 空值处理带来统一标准

**2024年8月21日** -JSpecify 1.0.0 版本正式发布,旨在为 JVM 语言定义一组通用的注解类型,改善静态分析和语言互操作性。该项目由 OpenJDK、EISOP、PMD、Android、Error Prone、Guava(谷歌)、Kotlin、IntelliJ(JetBrains)、Azure SDK(微软)、Sonar、Spring 等多个知名项目和团队共同参与。

JSpecify 的首个版本专注于引入类型使用注解,用于指明静态类型的空值状态。这将主要用于变量、参数和返回值,以及任何需要在概念上追踪空值情况的场景。

空值问题一直是 Java 生态系统中的一个棘手问题,开发者们一直努力避免 NullPointerException (NPE) 错误。然而,由于 Java 语言本身的限制以及对向后兼容性的承诺,将空值意识融入到 Java 中一直是一个巨大的挑战。

JSpecify 的出现为解决这一问题提供了一个新的思路。它提供了一套统一的注解,让开发者能够明确地声明变量、参数和返回值的空值状态,从而帮助静态分析工具更有效地识别潜在的 NPE 错误。

JSpecify 1.0.0 版本定义了四个注解:

* **@Nullable:** 表示该类型可以为空。
* **@NonNull:** 表示该类型不能为空。
* **@NullMarked:**用于标记模块、包或类型声明,表示所有未明确标注的类型使用都是非空的。
* **@NullUnmarked:** 用于标记模块、包或类型声明,表示所有未明确标注的类型使用都是可空的。

JSpecify 承诺保持向后兼容性,这意味着参与的项目不会重命名 jar 包中的注解、移动它们或做出其他导致更新后代码编译失败的变更。

Spring Framework 项目负责人 Juergen Hoeller 表示,Spring Framework 在 2017 年采用了默认非空的设计原则,JSpecify 的出现将进一步推动 Spring Framework 在空值处理方面的改进。

JSpecify 的发布标志着 Java 生态系统在空值处理方面迈出了重要的一步。它为开发者提供了一个统一的标准,帮助他们更有效地处理空值问题,避免 NPE 错误,提高代码质量。

**未来展望:**

JSpecify团队表示,未来将继续完善 JSpecify,使其支持更多功能,例如泛型和更复杂的空值处理场景。他们也希望 JSpecify 能被更多 Java 项目采用,成为 Java 生态系统中不可或缺的一部分。

英语如下:

##JSpecify 1.0.0 Released: Say Goodbye to Java Null PointerHeadaches!

**Keywords:** JSpecify, null, Java

**August21, 2024** – JSpecify 1.0.0 has been officially released, aiming to define a set of common annotation typesfor JVM languages, improving static analysis and language interoperability. The project is a collaborative effort involving several prominent projects and teams, including OpenJDK, EISOP,PMD, Android, Error Prone, Guava (Google), Kotlin, IntelliJ (JetBrains), Azure SDK (Microsoft), Sonar, and Spring.

The first version of JSpecify focuses on introducing type-use annotations to indicate thenull state of static types. This will primarily be used for variables, parameters, and return values, as well as any scenario where it’s necessary to conceptually track nullity.

Null pointer problems have been a persistent pain point in theJava ecosystem, with developers constantly striving to avoid NullPointerException (NPE) errors. However, due to limitations in the Java language itself and the commitment to backward compatibility, integrating null awareness into Java has been a significant challenge.

JSpecify presents a new approach to addressing this issue. It provides a unified set of annotationsthat allow developers to explicitly declare the null state of variables, parameters, and return values, enabling static analysis tools to more effectively identify potential NPE errors.

JSpecify 1.0.0 defines four annotations:

* **@Nullable:** Indicates that the type can be null.
* **@NonNull:**Indicates that the type cannot be null.
* **@NullMarked:** Used to mark modules, packages, or type declarations, indicating that all unmarked type uses are non-null.
* **@NullUnmarked:** Used to mark modules, packages, or type declarations, indicating that all unmarked type uses are nullable.

JSpecify promises to maintain backward compatibility, meaning participating projects will not rename annotations in jar packages, move them, or make other changes that could cause code to fail compilation after an update.

Juergen Hoeller, the lead of the Spring Framework project, stated that Spring Framework adopted a default non-nulldesign principle in 2017, and the emergence of JSpecify will further drive improvements in null handling within Spring Framework.

The release of JSpecify marks a significant step forward in null handling within the Java ecosystem. It provides developers with a unified standard, helping them more effectively handle null issues, avoid NPEerrors, and improve code quality.

**Future Outlook:**

The JSpecify team has stated that they will continue to enhance JSpecify to support more features, such as generics and more complex null handling scenarios. They also hope that JSpecify will be adopted by more Java projects, becoming an integral part of the Javaecosystem.

【来源】https://mp.weixin.qq.com/s/WO61_-IN0Rd_JioR7hhFWQ

Views: 2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注