How to indicate that member fields are @Nonnull by default?

感情迁移 提交于 2019-12-03 05:42:48

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

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
{
}

and then annotate the package as normal.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!