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

@CompileStatic error when property of delegated-to object is read

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.2.0-beta-1
    • 2.2.0
    • Static compilation
    • None

    Description

      import groovy.transform.CompileStatic
      
      @CompileStatic
      class MyCar {
          String brand
          String model
      }
      
      @CompileStatic
      class MyCarMain {
          static void main(String[] args) {
              def main = new MyCarMain()
              def car = main.configureCar {
                  brand = "BMW"
                  model = brand + " X5"
              }
              println car.model
          }
      
          MyCar configureCar(@DelegatesTo(MyCar) Closure closure) {
              def car = new MyCar()
              closure.delegate = car
              closure.resolveStrategy = Closure.DELEGATE_FIRST
              closure()
              car
          }
      }
      

      The line model = brand + " X5" produces this compile error:

      Groovyc: Access to java.lang.Object#brand is forbidden
      

      There is no such compile error when using @TypeChecked.

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            pniederw Peter Niederwieser
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: