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

Iterator for (x in y) and .each returning conllection not items in deep nesting closures

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-JSR-5
    • 1.0-RC-1
    • None
    • None

    Description

      The .each method and the for (x in y) methods sometimes provide the colelction as the iteration instead of teh iterated objects....

      def loopBug(arrayList) {
      println arrayList.class.name
      println arrayList[0].class.name
      def target
      def swing = new groovy.swing.SwingBuilder()
      def panel = swing.panel(alignmentX:0.0f, alignmentY:0.0f) {
      boxLayout(axis: javax.swing.BoxLayout.X_AXIS)
      hbox(alignmentX:0.0f, alignmentY:0.0f) {
      vbox(alignmentX:0.0f, alignmentY:0.0f) {
      target = checkBox(actionPerformed: { event -> println ""; println arrayList.class.name; for (e in arrayList) { println e.class.name; }} )
      }
      }
      }
      return target
      }

      def foo = new java.util.ArrayList()
      foo.add("1")
      foo.add("2")

      def bar=loopBug(foo)
      bar.doClick()

      println ""

      def loopBug2(arrayList) {
      println arrayList.class.name
      println arrayList[0].class.name
      def target
      def swing = new groovy.swing.SwingBuilder()
      def panel = swing.panel(alignmentX:0.0f, alignmentY:0.0f) {
      boxLayout(axis: javax.swing.BoxLayout.X_AXIS)
      hbox(alignmentX:0.0f, alignmentY:0.0f) {
      vbox(alignmentX:0.0f, alignmentY:0.0f) {
      target = checkBox(actionPerformed: { event -> println ""; println arrayList.class.name; arrayList.each { println it.class.name; }} )
      }
      }
      }
      return target
      }

      def baz=loopBug2(foo)
      baz.doClick()

      I expect as output
      "java.util.ArrayList
      java.lang.String

      java.util.ArrayList
      java.lang.String

      java.util.ArrayList
      java.lang.String

      java.util.ArrayList
      java.lang.String"

      but I get
      "java.util.ArrayList
      java.lang.String

      java.util.ArrayList
      java.util.ArrayList

      java.util.ArrayList
      java.lang.String

      java.util.ArrayList
      java.util.ArrayList"

      Attachments

        Activity

          People

            guillaume Guillaume Sauthier
            shemnon Daniel Ferrin
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: