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

@CompileStatic cannot compile generic class's static method

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Blocker
    • Resolution: Fixed
    • Affects Version/s: 2.4.12
    • Component/s: Static compilation
    • Labels:
      None
    • Environment:
      Windows 10, Gradle 4.2.1;
      IRL @CompileStatic applied via groovyOptions.configurationScript:
      withConfig(configuration) { ast(groovy.transform.CompileStatic) }

      Description

      Below code is a simplification of a Gradle API + Android Gradle Plugin consuming code:

      @CompileStatic
      class Test<T extends Task> {
      
      	static def something(Task task, Collection<BaseVariant> variants) {
      		variants
      				.collectMany {it.sourceFolders}
      				.collect {ConfigurableFileTree tree ->
      			task.buildDir.toPath().relativize(tree.dir.toPath()).toString() + File.separator
      		}
      	}
      }
      
      interface BaseVariant {
      
      	List<ConfigurableFileTree> getSourceFolders()
      }
      
      interface ConfigurableFileTree {
      
      	File getDir()
      }
      
      interface Task {
      
      	File getBuildDir()
      }
      

      Removing <T extends Task> makes it compile, otherwise:

      
      test\src\main\groovy\Test.groovy: 8: Expected parameter of type Task but got ConfigurableFileTree
       @ line 8, column 15.
         				.collect {ConfigurableFileTree tree ->
                       ^
      
      test\src\main\groovy\Test.groovy: 9: [Static type checking] - No such property: dir for class: Task
       @ line 9, column 38.
         			task.buildDir.toPath().relativize(tree.dir.toPath()).toString() + File.separator
                                              ^
      
      test\src\main\groovy\Test.groovy: 9: [Static type checking] - Cannot find matching method java.nio.file.Path#relativize(java.lang.Object). Please check if the declared type is right and if the method exists.
       @ line 9, column 4.
         			task.buildDir.toPath().relativize(tree.dir.toPath()).toString() + File.separator
            ^
      
      3 errors
      
      :checkstyle:compileGroovy FAILED
      

      So far I haven't found a workaround other than removing generics!

        Attachments

          Activity

            People

            • Assignee:
              daniel_sun Daniel Sun
              Reporter:
              twisterrob Robert Papp
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: