jspecify-skill
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJspecify provides a set of annotations to explicitly declare the nullness expectations of the Java code.
Jspecify提供了一组注解,用于显式声明Java代码中的空值预期。
Add jSpecify support in Maven projects
在Maven项目中添加Jspecify支持
If you are using Maven, then add the jspecify dependency in .
In , update or add the , to include the following configuration.
pom.xmlpom.xmlmaven-compiler-pluginxml
<dependencies>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
<configuration>
<release>25</release>
<encoding>UTF-8</encoding>
<fork>true</fork>
<compilerArgs>
<arg>-XDcompilePolicy=simple</arg>
<arg>--should-stop=ifError=FLOW</arg>
<arg>-Xplugin:ErrorProne -XepDisableAllChecks -Xep:NullAway:ERROR -XepOpt:NullAway:OnlyNullMarked -XepOpt:NullAway:JSpecifyMode=true</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.42.0</version>
</path>
<path>
<groupId>com.uber.nullaway</groupId>
<artifactId>nullaway</artifactId>
<version>0.12.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>如果你使用Maven,请在中添加jspecify依赖。在中,更新或添加,并包含以下配置。
pom.xmlpom.xmlmaven-compiler-pluginxml
<dependencies>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
<configuration>
<release>25</release>
<encoding>UTF-8</encoding>
<fork>true</fork>
<compilerArgs>
<arg>-XDcompilePolicy=simple</arg>
<arg>--should-stop=ifError=FLOW</arg>
<arg>-Xplugin:ErrorProne -XepDisableAllChecks -Xep:NullAway:ERROR -XepOpt:NullAway:OnlyNullMarked -XepOpt:NullAway:JSpecifyMode=true</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.42.0</version>
</path>
<path>
<groupId>com.uber.nullaway</groupId>
<artifactId>nullaway</artifactId>
<version>0.12.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>Add jSpecify support in Gradle projects
在Gradle项目中添加Jspecify支持
If you are using Gradle, then add the jspecify dependency.
In or , update or add the following jspecify configuration.
build.gradlebuild.gradle.ktsgroovy
plugins {
id("net.ltgt.errorprone") version "4.3.0"
}
tasks.withType(JavaCompile).configureEach {
options.errorprone {
disableAllChecks = true // Other error prone checks are disabled
option("NullAway:OnlyNullMarked", "true") // Enable nullness checks only in null-marked code
error("NullAway") // bump checks from warnings (default) to errors
option("NullAway:JSpecifyMode", "true") // https://github.com/uber/NullAway/wiki/JSpecify-Support
}
// Keep a JDK 25 baseline
options.release = 25
}
dependencies {
implementation("org.jspecify:jspecify:1.0.0")
errorprone("com.google.errorprone:error_prone_core:2.42.0")
errorprone("com.uber.nullaway:nullaway:0.12.12")
}如果你使用Gradle,请添加jspecify依赖。在或中,更新或添加以下jspecify配置。
build.gradlebuild.gradle.ktsgroovy
plugins {
id("net.ltgt.errorprone") version "4.3.0"
}
tasks.withType(JavaCompile).configureEach {
options.errorprone {
disableAllChecks = true // Other error prone checks are disabled
option("NullAway:OnlyNullMarked", "true") // Enable nullness checks only in null-marked code
error("NullAway") // bump checks from warnings (default) to errors
option("NullAway:JSpecifyMode", "true") // https://github.com/uber/NullAway/wiki/JSpecify-Support
}
// Keep a JDK 25 baseline
options.release = 25
}
dependencies {
implementation("org.jspecify:jspecify:1.0.0")
errorprone("com.google.errorprone:error_prone_core:2.42.0")
errorprone("com.uber.nullaway:nullaway:0.12.12")
}Add @NullMarked to package-info.java files
为package-info.java文件添加@NullMarked注解
In every java package under the application main source code (),
create if not exists already, and add the annotation as follows:
src/main/javapackage-info.java@NullMarkedjava
@org.jspecify.annotations.NullMarked
package com.mycompnay.myproject;If file already exists, update the file to add annotation.
DO NOT REMOVE ANY OTHER EXISTING CODE IN FILE.
package-info.java@org.jspecify.annotations.NullMarkedpackage-info.java在应用主源代码()下的每个Java包中,如果文件不存在则创建它,并添加注解,示例如下:
src/main/javapackage-info.java@NullMarkedjava
@org.jspecify.annotations.NullMarked
package com.mycompnay.myproject;如果文件已存在,请更新该文件以添加注解。请勿删除文件中的任何现有代码。
package-info.java@org.jspecify.annotations.NullMarkedpackage-info.javaVerify jSpecify support
验证Jspecify支持
If python is installed, after adding the jSpecify support, run
to check if all non-empty packages has file or not.
scripts/verify_nullmarked.pypackage-info.java如果已安装Python,添加Jspecify支持后,运行以检查所有非空包是否都包含文件。
scripts/verify_nullmarked.pypackage-info.java