我有下面的简单代码用于@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>
<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>
Run Code Online (Sandbox Code Playgroud)
Dav*_*ess 16
您可以为单个包执行此操作,但我还没有找到将其传播到子包的方法.对于方法参数,请使用内置包注释@ParametersAreNonnullByDefault.将注释应用于package-info.java包的目录中的文件包中.
请注意,我正在使用JSR-305中的FindBugs荣誉的
javax.annotation注释.
com/example/foo/package-info.java
/**
* Package that doesn't allow null values as method parameters.
*/
@ParametersAreNonnullByDefault
package com.example.foo;
import javax.annotation.ParametersAreNonnullByDefault;
Run Code Online (Sandbox Code Playgroud)
对于字段和方法返回值,您需要创建自己的注释.我通过复制源代码ParametersAreNonnullByDefault和更改ElementType枚举来完成此操作.
com/example/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
}
Run Code Online (Sandbox Code Playgroud)
几个月前,我开始从头开始重写一个相当复杂的系统,每个包都应用了这三个注释(字段,参数和返回值).避免null价值的动机之一就是在适当的时候使用Null Object模式.结合尽可能多地支持最终字段和只做一件事的小类确实保持了代码的清洁.
| 归档时间: |
|
| 查看次数: |
3792 次 |
| 最近记录: |