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

Property calls are not correctly type checked if the setter parameter type or getter return type are not the field type

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.5
    • Fix Version/s: 2.4.6
    • Component/s: Static Type Checker
    • Labels:
      None
    • Environment:
      JDK 1.8.0_60

      Description

      If a class defines a property but also explicitly provides a getter or setter, property style get/set calls are type checked using the underlying field type rather than the getter return type/setter parameter type.

      Setter example:

      class Foo {
          Bar bar;
      
          void setBar(int x) {
              this.bar = new Bar(x: x)
          }
      }
      
      class Bar {
          int x
      }
      
      @groovy.transform.TypeChecked
      void test() {
          Foo foo = new Foo()
          foo.bar = new Bar()
      }
      
      test()
      

      The code above will pass STC and throw a GroovyCastException at runtime. Conversely, code like foo.bar = 1 will not pass STC. The case for getters with return type different from the field type is similar.

        Attachments

          Activity

            People

            • Assignee:
              shils Shil Sinha
              Reporter:
              shils Shil Sinha
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: