检查器框架、-Xlint:all 和 JUnit

Checker Framework, -Xlint:all and JUnit

我试图从一开始就保持一个非常干净和严格的设置的项目,包括:

  1. 使用 the Checker Framework.
  2. 启用所有编译器警告并将它们视为错误(-Xlint:all-Werror)。
  3. 使用 JUnit.

以下是 Maven pom.xml 的相关部分:

<dependencies>
    <!-- Annotations: nullness, etc -->
    <dependency>
        <groupId>org.checkerframework</groupId>
        <artifactId>checker-qual</artifactId>
        <version>${checkerframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.checkerframework</groupId>
        <artifactId>jdk8</artifactId>
        <version>${checkerframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.checkerframework</groupId>
                            <artifactId>checker</artifactId>
                            <version>${checkerframework.version}</version>
                        </path>
                    </annotationProcessorPaths>
                    <annotationProcessors>
                        <!-- Add all the checkers you want to enable here -->
                        <annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker
                        </annotationProcessor>
                    </annotationProcessors>
                    <compilerArgs>
                        <arg>-Xbootclasspath/p:${annotatedJdk}</arg>
                        <arg>-Xlint:all</arg>
                        <arg>-Werror</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M1</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

不幸的是,当我引入一个使用 @Test 注释的测试 class 时,我收到以下编译警告并因此出现构建错误:

Warning:java: No processor claimed any of these annotations: org.junit.jupiter.api.Test

如何避免此警告?

找到解决方案:可以使用 -Xlint:-processing 消除此特定警告:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    ...
                    <compilerArgs>
                        <arg>-Xbootclasspath/p:${annotatedJdk}</arg>
                        <arg>-Xlint:all</arg>
                        <!-- Silence warning "No processor claimed any of these annotations". One of the
                        annotations that would trigger it is org.junit.jupiter.api.Test -->
                        <arg>-Xlint:-processing</arg>
                        <arg>-Werror</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>