将包下所有类的findbugs NotNull设置为默认值 [英] Set findbugs NotNull as default for all classes under a package

查看:61
本文介绍了将包下所有类的findbugs NotNull设置为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的简单代码,用于使用Maven测试FindBugs @NonNull批注.我执行

I have the simple code below for testing the FindBugs @NonNull annotation with Maven. I execute

mvn clean install

由于print(null)违反了非null条件,因此它无法正确构建.

And it correctly fails to build because print(null) violates the non-null condition.

您可以使用类批注将NonNull设置为类内所有方法参数的默认值

You can set NonNull as default for all method parameters inside a class using the class annotation

@DefaultAnnotation(NonNull.class)

如何将给定包(和子包)下所有类中所有方法参数的NonNull设置为默认值?

How can I set NonNull as default for all method parameters inside all classes under a given package (and sub-packages)?

src/main/java/test/Hello.java

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);
        }
    }
}

pom.xml

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文件中,将注释应用于程序包.

You can do this for individual packages, but I haven't found a way to have it propagate to subpackages. For method parameters use the built-in package annotation @ParametersAreNonnullByDefault. Apply the annotation to the package in its package-info.java file inside the package's directory.

请注意,我使用的是 FindBugs认可的JSR-305 .

com/example/foo/package-info.java

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;

对于字段和方法的返回值,您需要创建自己的注释.我是通过复制ParametersAreNonnullByDefault的源代码并更改ElementType枚举来实现的.

For fields and method return values you'll need to create your own annotations. I did this by copying the source for ParametersAreNonnullByDefault and changing the ElementType enum.

com/example/util/FieldsAreNonnullByDefault.java

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
}

几个月前,我开始从头开始重写一个相当复杂的系统,每个程序包都应用了这三个注释(字段,参数和返回值).避免使用null值的动机之一是在适当的地方使用Null Object模式.加上尽可能多地使用final字段以及只做一件事的小类,确实使代码保持了干净.

I began rewriting a fairly complex system from scratch a couple months ago, and every package has these three annotations applied (fields, parameters, and return values). One benefit that's come out of the incentive to avoid null values is using the Null Object pattern where appropriate. That combined with favoring final fields as much as possible and small classes that do one thing only has really kept the code clean.

这篇关于将包下所有类的findbugs NotNull设置为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆