Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7698

@CompileStatic safe navigation is broken for property style non-synthetic setter calls

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.0, 2.4.1, 2.4.2, 2.4.3, 2.4.4, 2.4.5
    • 2.4.6
    • None
    • None

    Description

      @CompileStatic does not create correct safe-navigation code for java setters when using property syntax. This regression happened with the update to 2.4 and affects all released versions up till now. The 2.3.x versions produce correct instructions.

      I've created a small demo project to reproduce this issue https://github.com/leonard84/groovy-compilestatic-bug

      Here a minimal excerpt to visualize the problem.

      SafeNavigation.groovy
      @CompileStatic
      class SafeNavigation {
          void javaSafeNavigation6() {
              DetailJava detailJava = null
              detailJava?.id = 'new'
          }
      }
      
      DetailJava.java
      public class DetailJava {
      
          private String id;
      
          public String getId() {
              return id;
          }
      
          public void setId(String id) {
              this.id = id;
          }
      }
      

      Using IntelliJs decompiler (Fernflower)

      groovy-2.4.x
         public void javaSafeNavigation6() {
              Object detailJava = null;
              String var2 = "new";
              ((DetailJava)detailJava).setId(var2);
              Object var10001 = null;
          }
      
      groovy-2.3.x
          public void javaSafeNavigation6() {
              Object detailJava = null;
              Object var10000;
              if(detailJava != null) {
                  ((DetailJava)detailJava).setId("new");
                  var10000 = null;
              } else {
                  var10000 = null;
              }
          }
      

      As you can see the null check is completely omitted. When using the setter directly it works, also reading the value works for both getter and property style. It also works correctly for groovy classes.

      Attachments

        Activity

          People

            shils Shil Sinha
            leonard84 Leonard Brünings
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: