How do I get @ParametersAreNonnullByDefault to work?

后端 未结 2 832

I\'ve made several attempts at getting package annotation @ParametersAreNonnullByDefault to work for me in a maven project but with no success. Could someone share a link to

相关标签:
2条回答
  • 2020-12-14 17:51

    For Android projects (for example when using libraries like Retrofit etc), this is the dependency to put in app/build.gradle (as mentioned by @Saket):

    dependencies {
        implementation 'com.google.code.findbugs:jsr305:3.0.2'
        // ...
    }
    
    0 讨论(0)
  • 2020-12-14 17:52

    How to apply @ParametersAreNonnullByDefault

    Create a file package-info.java in your package where you want to enforce the desired behavior.

    In that file, do the following:

    /**
     * You should do it like this!
     */
    @ParametersAreNonnullByDefault
    package com.stackoverflow;
    
    import javax.annotation.ParametersAreNonnullByDefault;
    

    How not to apply @ParametersAreNonnullByDefault

    Don't do the following in a Java source file:

    /**
     * But you shouldn't do it this way!
     */
    @ParametersAreNonnullByDefault
    package com.stackoverflow;
    
    import javax.annotation.ParametersAreNonnullByDefault;
    
    public class Answer { ...
    

    Declaring annotations like this is ambiguous.

    Notes

    It would be OK to apply the annotation directly on Answer class.

    package com.stackoverflow;
    
    import javax.annotation.ParametersAreNonnullByDefault;
    
    /**
     * You can do it like this also.
     */
    @ParametersAreNonnullByDefault
    public class Answer { ...
    

    The exact same things apply to @ParametersAreNullableByDefault also.

    0 讨论(0)
提交回复
热议问题