java – 将findbugs NotNull设置为一个包下的所有类的默认值

前端之家收集整理的这篇文章主要介绍了java – 将findbugs NotNull设置为一个包下的所有类的默认值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下简单的代码,用于测试使用Maven的FindBugs @NonNull注释.我执行
  1. mvn clean install

而且由于print(null)违反了非空条件,因此无法正确生成.

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

  1. @DefaultAnnotation(NonNull.class)

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

的src /主/爪哇/测试/ Hello.java

  1. package test;
  2. import edu.umd.cs.findbugs.annotations.NonNull;
  3. public class Hello {
  4. static public void print(@NonNull Object value) {
  5. System.out.println("value: " + value.toString());
  6. }
  7.  
  8. static public void main(String[] args) {
  9. if (args.length > 0) {
  10. print(args[0]);
  11. } else {
  12. print(null);
  13. }
  14. }
  15. }

的pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>hello</groupId>
  8. <artifactId>hello</artifactId>
  9. <version>1.0</version>
  10.  
  11. <dependencies>
  12. <dependency>
  13. <groupId>net.sourceforge.findbugs</groupId>
  14. <artifactId>annotations</artifactId>
  15. <version>1.3.2</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>net.sourceforge.findbugs</groupId>
  19. <artifactId>jsr305</artifactId>
  20. <version>1.3.7</version>
  21. </dependency>
  22. </dependencies>
  23.  
  24. <build>
  25. <plugins>
  26. <plugin>
  27. <groupId>org.apache.maven.plugins</groupId>
  28. <artifactId>maven-compiler-plugin</artifactId>
  29. <configuration>
  30. <source>1.6</source>
  31. <target>1.6</target>
  32. </configuration>
  33. </plugin>
  34. <plugin>
  35. <groupId>org.codehaus.mojo</groupId>
  36. <artifactId>findbugs-maven-plugin</artifactId>
  37. <version>2.5.2</version>
  38. <configuration>
  39. <includeTests>true</includeTests>
  40. </configuration>
  41. <executions>
  42. <execution>
  43. <phase>compile</phase>
  44. <goals>
  45. <goal>check</goal>
  46. </goals>
  47. </execution>
  48. <execution>
  49. <id>findbugs-test-compile</id>
  50. <phase>test-compile</phase>
  51. <goals>
  52. <goal>check</goal>
  53. </goals>
  54. </execution>
  55. </executions>
  56. </plugin>
  57. </plugins>
  58. </build>
  59. </project>

解决方法

您可以为单个软件包执行此操作,但是我还没有找到一种方法来传播到子包.对于方法参数,使用内置程序包注释 @ParametersAreNonnullByDefault.将注释应用到程序包目录中的package-info.java文件中的程序包.

Note that I’m using the javax.annotation annotations from 07001 which FindBugs honors.

COM /示例/富/ package-info.java

  1. /**
  2. * Package that doesn't allow null values as method parameters.
  3. */
  4. @ParametersAreNonnullByDefault
  5. package com.example.foo;
  6.  
  7. import javax.annotation.ParametersAreNonnullByDefault;

对于字段和方法返回值,您需要创建自己的注释.我通过复制ParametersAreNonnullByDefault的源并更改ElementType枚举来做到这一点.

COM /示例/ UTIL / FieldsAreNonnullByDefault.java

  1. package com.example.util;
  2.  
  3. import java.lang.annotation.Documented;
  4. import java.lang.annotation.ElementType;
  5. import java.lang.annotation.Retention;
  6. import java.lang.annotation.RetentionPolicy;
  7.  
  8. import javax.annotation.Nonnull;
  9. import javax.annotation.Meta.TypeQualifierDefault;
  10.  
  11. /**
  12. * Applies the {@link Nonnull} annotation to every class field unless overridden.
  13. */
  14. @Documented
  15. @Nonnull
  16. @TypeQualifierDefault(ElementType.FIELD) // <-- use METHOD for return values
  17. @Retention(RetentionPolicy.RUNTIME)
  18. public @interface FieldsAreNonnullByDefault
  19. {
  20. // nothing to add
  21. }

几个月前,我开始从头开始重写一个相当复杂的系统,每个包都应用了这三个注释(字段,参数和返回值).出于避免空值的动机的一个好处是在适当的情况下使用Null对象模式.尽可能地结合最终的领域,只做一件事的小班子真的保持了代码的清洁.

猜你在找的Java相关文章