首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Findbug+ JSR305:指定默认行为的可能性?

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

Stack Overflow用户
提问于 2009-09-30 10:06:04
回答 2查看 861关注 0票数 7

注意:我所指的那些注释是由JSR305指定的。

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

但是,在我的项目中,“非空逻辑”是默认的情况。我要说的是,null只允许在5%的情况下

因此,用@Nonnull注释95%的字段是非常不方便的。我宁愿用@Nullable来注释这5%的字段。

我试着用@Nonnull对整个包进行注释,它不会改变任何东西。

因此,可以以某种方式指定默认逻辑?

EN

回答 2

Stack Overflow用户

发布于 2009-09-30 10:50:38

我不确定Fiundbug是否能够处理以下注释,但是如果您想用"NonNull“注释all包,可以使用:

@ParametersAreNonnullByDefault

代码语言:javascript
复制
/**
 * 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测试用例中有这个注释。

票数 0
EN

Stack Overflow用户

发布于 2011-10-24 17:35:45

FindBugs现在有@ReturnValuesAreNonnullByDefault了。它还有@DefaultAn表示法、@DefaultAnnotationForField、@DefaultAnnotationForMethods和@DefaultAnnotationForParameters。

但我应该否认我没有在我的项目中使用过任何一种。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1497182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档