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

Cannot assign value of type java.lang.Object in ternary operator with null

    XMLWordPrintableJSON

Details

    Description

      This seems to be something like a regression of GROOVY-5734.

      The following fails in Groovy 2.4.3/2.4.4, while it didn't fail in Groovy 2.3.11:

      Bar.java
      package test2;
      
      public class Bar {
      }
      
      Foo.java
      package test2;
      
      public class Foo {
      	private Bar bar;
      	
      	public void setBar(Bar bar) {
      		this.bar = bar;
      	}
      	
      	public Bar getBar() {
      		return bar;
      	}
      }
      
      Test.groovy
      package test2
      
      import groovy.transform.CompileStatic;
      
      @CompileStatic
      public class Test {
      	void test() {
      		boolean check = true
      		Foo foo = new Foo()
      		foo.bar = check? new Bar(): null
      	}
      }
      

      The assignment to foo.bar fails with:Cannot assign value of type java.lang.Object to variable of type test2.Bar.

      The same heppens if Foo is written in Groovy instead of Java.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              mauromol Mauro Molinari
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: