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

Setters with non-void return type fail at runtime when statically compiled

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.3
    • Fix Version/s: 2.4.4
    • Component/s: Static compilation
    • Labels:
      None
    • Environment:
      OSX Yosemite, JDK 1.8.0_25

      Description

      Calling a statically compiled, non-void setter for a property will result in a NoSuchMethodError being thrown at runtime. There is no error if the setter corresponds to a field.
      Example:

      import groovy.transform.CompileStatic
      
      @CompileStatic
      class A {
        int prop
        private int field
      
        A setProp(int prop) {
          this.prop = prop
          this
        }
      
        A setField(int field) {
          this.field = field
          this
        }
      
        static void main(String[] args) {
          A a = new A()
          a.setField(1) // ok
          a.setProp(1) //throws NoSuchMethodError at runtime
        }
      }
      
      A a = new A()
      a.setField(1) //ok
      a.setProp(1) //throws NoSuchMethodError
      

      The problem appears to be in the `makeSetProperty` method of `StaticTypesBinaryExpressionMultiTypeDispatcher`, where only void setter methods of a ClassNode are looked up.

      The most recent version of Groovy where this isn't an issue is 2.3.3.

        Attachments

          Activity

            People

            • Assignee:
              melix C├ędric Champeau
              Reporter:
              shils Shil Sinha
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: