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

org.codehaus.groovy.runtime.typehandling.GroovyCastException in Groovy ".with { ... }" - Block

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.3, 2.4.4, 2.4.5
    • 4.0.0-alpha-1, 3.0.5, 2.5.13
    • groovy-jdk
    • None
    • all?
      Tested with Ubuntu 14.04.3 LTS (64 Bit) | MacOS 10.11 and Groovy 2.4.5, 2.3.4

    Description

      Problem

      subClass.with {
        type = ['String']
      }
      

      Should change SubClass.type and not TopClass.type.
      The script works fine with type2 instead of type. Is type a reserved word? I can't find any documentation about this.

      Unit-Test / Groovy Console-Test

      class SubClass{
          List type
      }
      
      class TopClass{
      
          int type = 10
      
          @Lazy
          List something = {
              List tmp = []
              for(int i = 0; i < 5; i++){
                  def subClass = new SubClass()
                  subClass.with {
                      type = ['String'] // throws org.codehaus.groovy.runtime.typehandling.GroovyCastException
                  }
                  tmp.add(subClass)
              }
              tmp
          }()
      }
      
      def topClass = new TopClass()
      println GroovySystem.version
      println(topClass.type)
      println(topClass.something)
      println(topClass.type)
      

      Exception

      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[String]' with class 'java.util.ArrayList' to class 'int'
      	at TopClass$_getSomething_closure1$_closure2.doCall(ConsoleScript3:15)
      	at TopClass$_getSomething_closure1.doCall(ConsoleScript3:14)
      	at TopClass$_getSomething_closure1.doCall(ConsoleScript3)
      	at TopClass.getSomething(ConsoleScript3:11)
      	at ConsoleScript3.run(ConsoleScript3:26)
      

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              MaikIgloffstein Maik Igloffstein
              Votes:
              0 Vote for this issue
              Watchers:
              6 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 - 0.5h
                  0.5h