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

looping of java.lang.Iterable or java.lang.Iterator objects in CompileStatic mode

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.3.8
    • Static compilation
    • None

    Description

      There are problems in looping of Iterable or Iterator objects in CompileStatic mode. The behaviour is different from dynamic mode.

      better example:

      @groovy.transform.CompileStatic
      class Iterables {
          Iterable<Object> toIterable(List list) {
              return new Iterable<Object>() {
                  Iterator iterator() {
                      list.iterator()
                  }
              }
          } 
          
          void loopIterable(List list) {
              for(Object o : toIterable(list)) {
                  println o
              }
          }
          
          void loopIterator(List list) {
              for(Object o : toIterable(list).iterator()) {
                  println o
              }
          }
          
          public static void main(String[] args) {
              new Iterables().loopIterable([1, 2, 3])
              new Iterables().loopIterator([4, 5, 6])
          }
      }
      

      prints

      Iterables$1@6f4d33
      java.util.ArrayList$Itr@90ff2a1
      

      prints in dynamic mode

      1
      2
      3
      4
      5
      6
      

      EDITED: original example:

      I was able to reproduce the problem with this code example (runnable in GroovyConsole):

      @Grab("org.yaml:snakeyaml:1.14")
      import org.yaml.snakeyaml.Yaml
      
      @groovy.transform.CompileStatic
      class YmlLoader {
          Map map
      
          @groovy.transform.CompileDynamic
          public void loadYmlDynamic(InputStream input) {
              Yaml yaml = new Yaml()
              for(Object yamlObject : yaml.loadAll(input)) {
                  if(yamlObject instanceof Map) {
                      map = (Map)yamlObject
                  }
              }
          }
      
          public void loadYmlStatic(InputStream input) {
              Yaml yaml = new Yaml()
              for(Object yamlObject : yaml.loadAll(input)) {
                  if(yamlObject instanceof Map) { // problem here with CompileStatic
                      map = (Map)yamlObject
                  }
              }
          }
      }
      
      YmlLoader a = new YmlLoader()
      a.loadYmlDynamic new ByteArrayInputStream("a: 1".getBytes())
      a.map
      
      YmlLoader b = new YmlLoader()
      b.loadYmlDynamic new ByteArrayInputStream("a: 1".getBytes())
      assert a.map == b.map
      
      YmlLoader c = new YmlLoader()
      c.loadYmlStatic new ByteArrayInputStream("a: 1".getBytes())
      assert a.map == c.map
      

      Attachments

        Activity

          People

            melix Cédric Champeau
            lhotari Lari Hotari
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: