Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-15540

Introduce null-safety of Spring Framework API

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: None
    • Fix Version/s: 5.0 RC2
    • Component/s: None
    • Labels:

      Description

      The proposal brought by this issue is to make nullability of return values and parameters of Spring Framework API explicit via annotations, and leverage this information in Kotlin user Spring projects.

      More details can be found in these 2 Square blog posts Rolling out @Nullable and Non-null is the default.

      If at some points @ParametersAreNonnullByDefault is supported at package level, it seems it could be possible to add @Nullable annotation only on nullable parameter and return values without bloating our code source with non-nullability annotations.

      Notice that the com.google.code.findbugs:jsr305 dependency used is applied with a provided scope which seems to be enough to make it taken in account by Kotlin (without that Kotlin consider Java nullability as unknown). We need to check this would not create some classloader related issues.

        Issue Links

          Activity

          Hide
          sdeleuze Sébastien Deleuze added a comment -

          Workaround for Spring projects developers: it is possible to make Spring @Nullable annotation recognized in IntelliJ IDEA by going to menu File -> Settings -> Editor -> Inspections -> @NotNull / @Nullable problems -> Options -> Configure Annotations and adding org.springframework.lang.Nullable to the list of nullable annotations.

          Show
          sdeleuze Sébastien Deleuze added a comment - Workaround for Spring projects developers: it is possible to make Spring @Nullable annotation recognized in IntelliJ IDEA by going to menu File -> Settings -> Editor -> Inspections -> @NotNull / @Nullable problems -> Options -> Configure Annotations and adding org.springframework.lang.Nullable to the list of nullable annotations.
          Hide
          sdeleuze Sébastien Deleuze added a comment -

          After a useful discussion with Peter Gromov about JSR 305 and IDEA support for custom @Nullable, it seems we have reached a consensus. See also these interesting slides for more background https://www.cs.umd.edu/~pugh/JSR-305.pdf.

          On Spring Framework side, I am going to update our @org.springframework.lang.Nullable annotation in order to leverage JSR 305 @TypeQualifierNickname meta-annotation + NonNull(when=MAYBE) since this is a more suitable semantic than NonNull(when=UNKNOWN) carried by @javax.annotation.Nullable. Also the removal of @TypeQualifierDefault will make nullable only applied to the return value when set at method level.

          @Documented
          @TypeQualifierNickname 
          @Nonnull(when=MAYBE)
          @Target({ElementType.METHOD, ElementType.PARAMETER})
          @Retention(RetentionPolicy.RUNTIME)
          public @interface Nullable {
          }
          

          IntelliJ IDEA 2017.1 next update will provide pre-configured support for @org.springframework.lang.Nullable in order to provide a good developer experience by default for Spring Framework 5.0 GA. Before we get that, Spring developers should configure the annotation manually as explained in my previous comment. IntelliJ IDEA 2017.2 will provide full support for JSR 305 meta annotations, allowing to support Spring null(safety in a fully generic way).

          Eclipse support nullable + non-null by default via configuration of the custom annotations so they will be able to leverage null-safety as well.
          Netbeans does not support non-null by default strategy, but I will open a feature request on their bug tracker.

          I will add a documentation page providing more detail about null-safety for Spring developers and tooling vendors.

          Show
          sdeleuze Sébastien Deleuze added a comment - After a useful discussion with Peter Gromov about JSR 305 and IDEA support for custom @Nullable , it seems we have reached a consensus. See also these interesting slides for more background https://www.cs.umd.edu/~pugh/JSR-305.pdf . On Spring Framework side, I am going to update our @org.springframework.lang.Nullable annotation in order to leverage JSR 305 @TypeQualifierNickname meta-annotation + NonNull(when=MAYBE) since this is a more suitable semantic than NonNull(when=UNKNOWN) carried by @javax.annotation.Nullable . Also the removal of @TypeQualifierDefault will make nullable only applied to the return value when set at method level. @Documented @TypeQualifierNickname @Nonnull (when=MAYBE) @Target ({ElementType.METHOD, ElementType.PARAMETER}) @Retention (RetentionPolicy.RUNTIME) public @interface Nullable { } IntelliJ IDEA 2017.1 next update will provide pre-configured support for @org.springframework.lang.Nullable in order to provide a good developer experience by default for Spring Framework 5.0 GA. Before we get that, Spring developers should configure the annotation manually as explained in my previous comment. IntelliJ IDEA 2017.2 will provide full support for JSR 305 meta annotations, allowing to support Spring null(safety in a fully generic way). Eclipse support nullable + non-null by default via configuration of the custom annotations so they will be able to leverage null-safety as well. Netbeans does not support non-null by default strategy, but I will open a feature request on their bug tracker. I will add a documentation page providing more detail about null-safety for Spring developers and tooling vendors.
          Hide
          sdeleuze Sébastien Deleuze added a comment -

          See also related null-safety support on Reactor side, with a similar strategy for @NonNullApi and usage of javax.annotation.Nullable directly in order to avoid adding an additional @Nullable annotation in the classpath and because they don't need to be as defensive as Spring about JSR 305 annotations.

          After Juergen Hoeller epic commit I am waiting feedbacks from other projects like Boot (SPR-15642) or Data before resolving this issue.

          Show
          sdeleuze Sébastien Deleuze added a comment - See also related null-safety support on Reactor side, with a similar strategy for @NonNullApi and usage of javax.annotation.Nullable directly in order to avoid adding an additional @Nullable annotation in the classpath and because they don't need to be as defensive as Spring about JSR 305 annotations. After Juergen Hoeller epic commit I am waiting feedbacks from other projects like Boot ( SPR-15642 ) or Data before resolving this issue.
          Hide
          sdeleuze Sébastien Deleuze added a comment -

          After another huge commit from Juergen Hoeller, nullability declaration is now extended to the field level as well.

          Show
          sdeleuze Sébastien Deleuze added a comment - After another huge commit from Juergen Hoeller , nullability declaration is now extended to the field level as well.
          Hide
          MattSmiglarski Matthew added a comment -

          Hello. I have just raised issue SPR-16109 which was introduced by a commit from this issue.

          Show
          MattSmiglarski Matthew added a comment - Hello. I have just raised issue SPR-16109 which was introduced by a commit from this issue.

            People

            • Assignee:
              sdeleuze Sébastien Deleuze
              Reporter:
              sdeleuze Sébastien Deleuze
              Last updater:
              Juergen Hoeller
            • Votes:
              6 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                3 weeks, 2 days ago