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

前端 未结 1 1350
情歌与酒
情歌与酒 2021-02-03 23:52

My question is a follow-up to this one.

In past versions of FindBugs, it was possible to use @DefaultAnnotation(Nonnull.class) or @DefaultAnnotationFo

相关标签:
1条回答
  • 2021-02-04 00:37

    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;
    
    0 讨论(0)
提交回复
热议问题