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

Type checking complains when calling getter of delegated-to object with property syntax

    XMLWordPrintableJSON

Details

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

    Description

      import groovy.transform.TypeChecked
      
      @TypeChecked
      class MyCar {
          private String _brand
          private String _model
      
          String getBrand() {
              return _brand
          }
      
          void setBrand(String brand) {
              _brand = brand
          }
      
          String getModel() {
              return _model
          }
      
          void setModel(String model) {
              _model = model
          }
      }
      
      @TypeChecked
      class MyCarMain {
          static void main(String[] args) {
              def main = new MyCarMain()
              def car = main.configureCar {
                  brand = "BMW"
                  model = brand
              }
              println car.model
          }
      
          MyCar configureCar(@DelegatesTo(value = MyCar, strategy = Closure.DELEGATE_FIRST) Closure closure) {
              def car = new MyCar()
              closure.delegate = car
              closure.resolveStrategy = Closure.DELEGATE_FIRST
              closure()
              car
          }
      }
      
      Groovyc: [Static type checking] - The variable [brand] is undeclared.
      

      model = getBrand() solves the problem. Problem doesn't occur when declaring MyCar as:

      class MyCar {
        String brand
        String model
      }
      

      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: