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

CompileStatic typeCasting runtime error from Class Array to Class List

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.19, 3.0.3, 2.5.11
    • 4.0.0-alpha-1, 3.0.4, 2.5.12
    • Static compilation
    • Java: openjdk version "1.8.0_242"
      OS: OS X Catalina 10.15.3
      Groovy: 2.4.19
    • Important

    Description

      After upgrading asset-pipeline-core from groovy 2.0.7 to 2.4.15 then 2.4.19 . A bug was identified with @CompileStatic not properly handling runtime type casting when sorting an Array instead of a List

       

      Example

       

      Create File TestFile.groovy

      import groovy.transform.CompileStaticpublic class TestFile {	
          static void main(String[] args) {
              File file = new File('.')
              TestFile testFile = new TestFile()
              testFile.listSortedFiles(file)
          }
      
          @CompileStatic
          void listSortedFiles(File directory) {
              File[] files = directory.listFiles()
              files = files?.sort { File a, File b -> a.name.compareTo b.name } as File[]
              for(File file in files) {
              }
          }
      	
      		
      }
      
      

      Then simply run:

      groovy TestFile.groovy
      

       

      Exception:

       

      Caught: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.io.File;
      java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.io.File;
      	at TestFile.listSortedFiles(TestFile.groovy:15)
      	at TestFile$listSortedFiles.call(Unknown Source)
      	at TestFile.main(TestFile.groovy:8)
      

       

      Attachments

        Activity

          People

            emilles Eric Milles
            davydotcom David Estes
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 20m
                20m