我有以下简单的代码,用于测试使用Maven的FindBugs @NonNull注释.我执行
mvn clean install
而且由于print(null)违反了非空条件,因此无法正确生成.
您可以使用类注释将NonNull设置为类中所有方法参数的默认值
@DefaultAnnotation(NonNull.class)
如何将NonNull设置为给定包(和子包)下的所有类中的所有方法参数的默认值?
的src /主/爪哇/测试/ 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);
}
}
}
的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>
<version>1.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<includeTests>true</includeTests>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
<execution>
<id>findbugs-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
解决方法
您可以为单个软件包执行此操作,但是我还没有找到一种方法来传播到子包.对于方法参数,使用内置程序包注释
@ParametersAreNonnullByDefault.将注释应用到程序包目录中的package-info.java文件中的程序包.
Note that I’m using the
javax.annotationannotations from 07001 which FindBugs honors.
COM /示例/富/ package-info.java
/** * Package that doesn't allow null values as method parameters. */ @ParametersAreNonnullByDefault package com.example.foo; import javax.annotation.ParametersAreNonnullByDefault;
对于字段和方法返回值,您需要创建自己的注释.我通过复制ParametersAreNonnullByDefault的源并更改ElementType枚举来做到这一点.
COM /示例/ UTIL / FieldsAreNonnullByDefault.java
package com.example.util;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.annotation.Nonnull;
import javax.annotation.Meta.TypeQualifierDefault;
/**
* Applies the {@link Nonnull} annotation to every class field unless overridden.
*/
@Documented
@Nonnull
@TypeQualifierDefault(ElementType.FIELD) // <-- use METHOD for return values
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldsAreNonnullByDefault
{
// nothing to add
}
几个月前,我开始从头开始重写一个相当复杂的系统,每个包都应用了这三个注释(字段,参数和返回值).出于避免空值的动机的一个好处是在适当的情况下使用Null对象模式.尽可能地结合最终的领域,只做一件事的小班子真的保持了代码的清洁.