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

@CompileStatic doesn't determine correct type for return from generic static method

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.1.7
    • 2.3.1
    • Static compilation
    • None

    Description

      The following code produces the compilation error:

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      bad.groovy: 20: [Static type checking] - Cannot assign value of type java.lang.Object to variable of type Ifc
       @ line 20, column 16.
         	    Ifc ifc = Generator.create(Ifc)
                        ^
      
      1 error
      
      import groovy.transform.CompileStatic
      import java.lang.reflect.Method
      
      interface Ifc {
      	void method()
      }
      
      @CompileStatic
      class Generator {
      	static <T> T create (Class<T> clazz) {
      	    return clazz.methods.collectEntries { Method method ->
      	        [ (method.name) : { println "${method.name} called"} ]
      	    }.asType(clazz)
      	}
      }
      
      @CompileStatic
      class User {
      	static void main() {
      	    Ifc ifc = Generator.create(Ifc)
      	    ifc.method()
      	}
      }
      
      User.main()
      

      Without the @CompileStatic on the User class, the code compiles and runs as expected.

      Attachments

        Activity

          People

            melix Cédric Champeau
            acourtneybrown Adam Brown
            Votes:
            4 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: