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

Using with method with a closure that references a protected property produces ClassCastException

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.7
    • Fix Version/s: 2.5.6, 3.0.0-beta-1
    • Component/s: None
    • Labels:
      None

      Description

      The following example:

      class Foo {
          Object propertyMissing(String name) {
               return "stuff"
          }
          
          void build(Closure callable) {
               this.with(callable)
          }
      }
      
      @groovy.transform.CompileStatic
      class Bar {
          protected List bar = []
          
          boolean doStuff() {
              Foo foo = new Foo()
              foo.build {
                 return bar.isEmpty() 
              }
          }
      }
      
      new Bar().doStuff()
      

      Produces

      java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List
      	at Bar$_doStuff_closure1.doCall(ConsoleScript3:19)
      	at Bar$_doStuff_closure1.call(ConsoleScript3)
      	at org.codehaus.groovy.runtime.DefaultGroovyMethods.with(DefaultGroovyMethods.java:242)
      	at Bar.doStuff(ConsoleScript3:18)
      	at Ba
      

      The equivalent code without CompileStatic prints:

      Result: false
      

      The behaviour of both should be he same IMO

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                graemerocher1 Graeme Rocher
              • 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 - 0.5h
                  0.5h