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

Cannot assign value of type java.lang.Object with varargs, parameterized method and @CompileStatic

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.4.7
    • Fix Version/s: 2.4.8
    • Component/s: Static compilation
    • Labels:
      None
    • Environment:
      Groovy 2.4.7 (tested also with 2.4.4)

      Description

      Static compilation fails when method is parameterized and there are varargs arguments which are not used in the call.

      Error:(9, 23) Groovyc: [Static type checking] - Cannot assign value of type java.lang.Object to variable of type Ext.
      

      Simple classes to reproduce the problem:

      public class FooInJava { //needs to be in Java, a class in Groovy works fine
          <T> T create(Class<T> type, Object... args) { return null; }
      }
      
      class Ext {}
      
      @CompileStatic
      class FooMain {
          static void main(String[] args) {
              Ext create = new FooInJava().create(Ext) //casting is required to make compilation pass
          }
      }
      

      It only occurs if a class with unfortunate method signature is written in Java and static compilation is enabled. Casting to the right type helps. Originally spotted with Gradle - `ExtensionContainer.create(...)`.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                szpak Marcin Zajaczkowski
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: