注意:我所指的那些注释是由JSR305指定的。
我有最新的Findbug (1.3.9),当使用@Nonnull注释的某个字段被赋值为null时,它会正确地发现错误。
但是,在我的项目中,“非空逻辑”是默认的情况。我要说的是,null只允许在5%的情况下。
因此,用@Nonnull注释95%的字段是非常不方便的。我宁愿用@Nullable来注释这5%的字段。
我试着用@Nonnull对整个包进行注释,它不会改变任何东西。
因此,可以以某种方式指定默认逻辑??。
发布于 2009-09-30 10:50:38
我不确定Fiundbug是否能够处理以下注释,但是如果您想用"NonNull“注释all包,可以使用:
@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 {
}另见这篇文章。
注意:至少在一些FindBugs测试用例中有这个注释。
发布于 2011-10-24 17:35:45
FindBugs现在有@ReturnValuesAreNonnullByDefault了。它还有@DefaultAn表示法、@DefaultAnnotationForField、@DefaultAnnotationForMethods和@DefaultAnnotationForParameters。
但我应该否认我没有在我的项目中使用过任何一种。
https://stackoverflow.com/questions/1497182
复制相似问题