Findbugs + JSR305:指定默认行为的可能性?

iva*_*off 6 java static-analysis findbugs jsr305

注意:我正在谈论的那些注释是由JSR305指定的.

我有最新的Findbugs(1.3.9), 当一些用@Nonnull注释的字段被赋值为null时,它会正确地发现错误.

但是,在我的项目中,"非空逻辑"是默认情况.我会说 只有5%的情况才明确允许null.

因此,用@Nonnull注释95%的字段会非常不方便.我更愿意用@Nullable注释那些5%的字段.

我尝试使用@Nonnull注释整个包,它不会改变任何东西.

那么,以某种方式可以指定默认逻辑?

Von*_*onC 0

我不确定 Fiundbug 是否可以处理以下注释,但如果您想用“NonNull”注释所有包,您可能需要使用:

@ParametersAreNonnullByDefault

/**
 * This annotation can be applied to a package, class or method to indicate that
 * the method parameters in that element are nonnull by default unless there is:
 * <ul>
 * <li>An explicit nullness annotation
 * <li>The method overrides a method in a superclass (in which case the
 * annotation of the corresponding parameter in the superclass applies)
 * <li> there is a default parameter annotation applied to a more tightly nested
 * element.
 * </ul>
 * 
 */
@Documented
@Nonnull
@TypeQualifierDefault(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface ParametersAreNonnullByDefault {
}
Run Code Online (Sandbox Code Playgroud)

另请参阅这篇文章

注意:至少该注释出现在一些 FindBugs 测试用例中。