如何在默认情况下指示成员字段是@Nonnull? [英] How to indicate that member fields are @Nonnull by default?

查看:303
本文介绍了如何在默认情况下指示成员字段是@Nonnull?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是这个的后续行动。

My question is a follow-up to this one.

在过去的FindBugs版本中,可以使用 @DefaultAnnotation(Nonnull.class) @DefaultAnnotationForFields(Nonnull.class)表示包中的所有字段应被视为 @Nonnull 。在当前版本的FindBugs(2.0)中,不推荐使用 @DefaultAnnotation @DefaultAnnotationForFields ,我们都应该使用JSR -305代替。但JSR-305似乎并未涵盖(现已弃用)FindBugs注释所涵盖的所有内容。

In past versions of FindBugs, it was possible to use @DefaultAnnotation(Nonnull.class) or @DefaultAnnotationForFields(Nonnull.class) to indicate that all fields in a package should be treated as @Nonnull. In the current version of FindBugs (2.0), @DefaultAnnotation and @DefaultAnnotationForFields are deprecated, and we should all use JSR-305 instead. But JSR-305 doesn't seem to cover everything the (now deprecated) FindBugs annotations cover.

javadoc 确实提供了许多替代方案:

The javadoc does suggest a number of alternatives:


  • @ParametersAreNonnullByDefault 。这(显然)仅适用于参数,而不适用于成员字段。

  • @CheckReturnValue ,应用于类型或包时。同样,这不适用于成员字段。

  • @TypeQualifierDefault 。也许这可以做我想要的,但我不明白它是如何工作的,除了一些神秘的javadoc之外,我无法找到任何有关其用法或意图的文档或示例。我认为这将有助于我创建自己的注释,但是我可以确定所有工具(FindBugs,Eclipse等)都能正确地解释这个新注释(甚至可以解释)吗?

  • @ParametersAreNonnullByDefault. This (obviously) only applies to parameters, not to member fields.
  • @CheckReturnValue, when applied to a type or package. Again, this doesn't apply to member fields.
  • @TypeQualifierDefault. Maybe this can do what I want, but I don't understand how it works, and I'm unable to find any documentation or examples on its usage or intent, besides some cryptic javadoc. I think it will help me create my own annotations, but can I be sure that all the tools (FindBugs, Eclipse, etc.) will interpret this new annotation correctly (or even at all)?

javadoc 没有提供任何有关如何处理其弃用的提示。

The javadoc doesn't provide any hints on how to deal with its deprecation.

因此,使用当前版本的FindBugs和/或JSR- 305,我应该如何表明某个包(甚至某个类)中的所有成员字段都被视为 @Nonnull ?它甚至可能吗?

So, using the current versions of FindBugs and/or JSR-305, how should I indicate that all member fields in a certain package (or even in a certain class) are supposed to be treated as @Nonnull? Is it even possible?

推荐答案

我有一个类似的问题,发现以下似乎适用于findbugs(2.0.1) -rc2)

I had a similar question, and found that the following seems to work with findbugs (2.0.1-rc2)

使用以下注释定义创建一个java文件

Create a java file with the following annotation definition

@Nonnull
@TypeQualifierDefault(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldsAreNonNullByDefault
{
}

类似地,强制方法的所有返回值都是非空的

similarly, to enforce that all return values from a method are non-null

@Nonnull
@TypeQualifierDefault(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReturnTypesAreNonNullByDefault
{
}

然后正常注释包。

我在测试中使用了以下内容(package-info.java)

I used the following for my tests (package-info.java)

@javax.annotation.ParametersAreNonnullByDefault
@com.habit.lib.lang.FieldsAreNonNullByDefault
@com.habit.lib.lang.ReturnTypesAreNonNullByDefault

package com.mypackagename.subpkg;

这篇关于如何在默认情况下指示成员字段是@Nonnull?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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