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
-
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.