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

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

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.7
    • 2.5.9, 3.0.0-rc-2
    • None
    • 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

              Unassigned Unassigned
              graemerocher1 Graeme Rocher
              Votes:
              0 Vote for this issue
              Watchers:
              4 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 - 2h 10m
                  2h 10m