NullCheck (Groovy 5.0.8)

Package: groovy.transform

## [Java] Annotation Type NullCheck

- groovy.transform.NullCheck

-     @Documented
      @Retention(RetentionPolicy.SOURCE)
      @Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR})
      @GroovyASTTransformationClass("org.codehaus.groovy.transform.NullCheckASTTransformation")
      public @interface NullCheck

  Class, method or constructor annotation which indicates that each parameter should be checked to ensure it isn't null. If placed at the class level, all explicit methods and constructors will be checked. Explicit means those declared within the class and not inherited or added by transforms (but see includeGenerated()).

  Example usage:

       import groovy.transform.NullCheck
       import static groovy.test.GroovyAssert.shouldFail

       @NullCheck
       class Greeter {
           private String audience

           Greeter(String audience) {
               this.audience = audience.toLowerCase()
           }

           String greeting(String salutation) {
               salutation.toUpperCase() + ' ' + audience
           }
       }

       assert new Greeter('World').greeting('hello') == 'HELLO world'

       def ex = shouldFail(IllegalArgumentException) { new Greeter(null) }
       assert ex.message == 'audience cannot be null'

       ex = shouldFail(IllegalArgumentException) { new Greeter('Universe').greeting(null) }
       assert ex.message == 'salutation cannot be null'

  The produced code for the above example looks like this:

       class Greeter {
           private String audience

           Foo(String audience) {
               if (audience == null) throw new IllegalArgumentException('audience cannot be null')
               this.audience = audience.toLowerCase()
           }

           String greeting(String salutation) {
               if (salutation == null) throw new IllegalArgumentException('salutation cannot be null')
               salutation.toUpperCase() + ' ' + audience
           }
       }

  Since:
    3.0.0

- - ### Element Summary

    Optional Element Summary
    Type | Name and Description
    --- | ---
    `boolean` | `includeGenerated` Whether to try to add null checking to generated methods/constructors such as those added by other transforms.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Element Detail

    - #### public boolean includeGenerated

      Whether to try to add null checking to generated methods/constructors such as those added by other transforms. Using this option may lead to surprising results, e.g. it will only apply to methods/constructors added prior to when the `NullCheck` transformation is processed. Null checking is not enabled for constructors containing generated bytecode or generated constructors with calls to this(...) or super(...) regardless of this flag.

      Since:
        3.0.2
      Default:
        false