我有下面的简单代码用于@NonNull使用Maven 测试FindBugs 注释.我执行
mvn clean install
Run Code Online (Sandbox Code Playgroud)
它正确地无法构建,因为print(null)违反了非null条件.
您可以NonNull使用类注释将类中的所有方法参数设置为默认值
@DefaultAnnotation(NonNull.class)
Run Code Online (Sandbox Code Playgroud)
如何NonNull为给定包(和子包)下的所有类中的所有方法参数设置默认值?
src/main/java/test/Hello.java
package test;
import edu.umd.cs.findbugs.annotations.NonNull;
public class Hello {
static public void print(@NonNull Object value) {
System.out.println("value: " + value.toString());
}
static public void main(String[] args) {
if (args.length > 0) {
print(args[0]);
} else {
print(null);
}
}
}
Run Code Online (Sandbox Code Playgroud)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hello</groupId>
<artifactId>hello</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>net.sourceforge.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>net.sourceforge.findbugs</groupId>
<artifactId>jsr305</artifactId> …Run Code Online (Sandbox Code Playgroud) Eclipse具有@NonNullByDefault注释,它将所有值视为@NonNull除非您明确地将它们注释为@Nullable.
IntelliJ IDEA中是否有等效选项,或者您是否必须始终使用@Nonnull?