groovy
  1. groovy
  2. GROOVY-4645

Convariant returns causes compiler to fail when generating property getters

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.7.6, 1.8-beta-3
    • Fix Version/s: 1.8.5, 2.0-beta-2, 1.7.11
    • Component/s: Compiler
    • Labels:
      None
    • Number of attachments :
      0

      Description

      When attempting to compile the following groovy code I get the compiler error under groovy 1.7.6 and 1.8.3-beta-3. This has to do with defining an interface (or class) with getters and then defining properties (or final members variables) on a subclass using covariant returns. I can work around this issue by explicitly defining the required getter in the subclass which is rather annoying b/c I like groovy getter generation and in our codebase we are running into this a lot. This is also a problem when using "def" types. I would be glad to assist in any way if I can. Thanks.

      For example:

      interface CovariantReturns {
        Foo getGood()
        Object getBad()
      }
      
      class Foo {}
      
      class CovariantReturnsImpl implements CovariantReturns {
        //getter is generated by groovy as required by the interface
        final Foo good
      
        //this variable is defined as the subtype of Object
        final Foo bad
      
        /*
         I would expect that the following method would be generated by groovy
         which is using Java's covariant returns feature.  Including the method below
         will allow the groovy compiler to succeed but is annoying that it has to be explicitly
         defined in code.
      
         public Foo getBad() { return bad }
         */
      }
      
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      CovariantReturns.groovy: 8: Can't have an abstract method in a non-abstract class. The class 'CovariantReturns
      Impl' must be declared abstract or the method 'java.lang.Object getBad()' must be implemented.
       @ line 8, column 1.
         class CovariantReturnsImpl implements CovariantReturns {
         ^
      
      1 error
      

        Issue Links

          Activity

          Hide
          Paul King added a comment -

          add code tags

          Show
          Paul King added a comment - add code tags
          Paul King made changes -
          Field Original Value New Value
          Description When attempting to compile the following groovy code I get the compiler error under groovy 1.7.6 and 1.8.3-beta-3. This has to do with defining an interface (or class) with getters and then defining properties (or final members variables) on a subclass using covariant returns. I can work around this issue by explicitly defining the required getter in the subclass which is rather annoying b/c I like groovy getter generation and in our codebase we are running into this a lot. This is also a problem when using "def" types. I would be glad to assist in any way if I can. Thanks.


          For example:

          interface CovariantReturns {
            Foo getGood()
            Object getBad()
          }

          class Foo {}

          class CovariantReturnsImpl implements CovariantReturns {
            //getter is generated by groovy as required by the interface
            final Foo good

            //this variable is defined as the subtype of Object
            final Foo bad

            /*
             I would expect that the following method would be generated by groovy
             which is using Java's covariant returns feature. Including the method below
             will allow the groovy compiler to succeed but is annoying that it has to be explicitly
             defined in code.

             public Foo getBad() { return bad }
             */

          }

          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          CovariantReturns.groovy: 8: Can't have an abstract method in a non-abstract class. The class 'CovariantReturns
          Impl' must be declared abstract or the method 'java.lang.Object getBad()' must be implemented.
           @ line 8, column 1.
             class CovariantReturnsImpl implements CovariantReturns {
             ^

          1 error
          When attempting to compile the following groovy code I get the compiler error under groovy 1.7.6 and 1.8.3-beta-3. This has to do with defining an interface (or class) with getters and then defining properties (or final members variables) on a subclass using covariant returns. I can work around this issue by explicitly defining the required getter in the subclass which is rather annoying b/c I like groovy getter generation and in our codebase we are running into this a lot. This is also a problem when using "def" types. I would be glad to assist in any way if I can. Thanks.


          For example:
          {code}
          interface CovariantReturns {
            Foo getGood()
            Object getBad()
          }

          class Foo {}

          class CovariantReturnsImpl implements CovariantReturns {
            //getter is generated by groovy as required by the interface
            final Foo good

            //this variable is defined as the subtype of Object
            final Foo bad

            /*
             I would expect that the following method would be generated by groovy
             which is using Java's covariant returns feature. Including the method below
             will allow the groovy compiler to succeed but is annoying that it has to be explicitly
             defined in code.

             public Foo getBad() { return bad }
             */
          }
          {code}

          {noformat}
          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          CovariantReturns.groovy: 8: Can't have an abstract method in a non-abstract class. The class 'CovariantReturns
          Impl' must be declared abstract or the method 'java.lang.Object getBad()' must be implemented.
           @ line 8, column 1.
             class CovariantReturnsImpl implements CovariantReturns {
             ^

          1 error
          {noformat}
          Hide
          Paul King added a comment -

          Possibly overlaps with GROOVY-4415?

          Show
          Paul King added a comment - Possibly overlaps with GROOVY-4415 ?
          CÚdric Champeau made changes -
          Link This issue relates to GROOVY-4415 [ GROOVY-4415 ]
          Hide
          CÚdric Champeau added a comment -

          Same issue, two different ways of demonstrating.

          Show
          CÚdric Champeau added a comment - Same issue, two different ways of demonstrating.
          CÚdric Champeau made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Assignee Cedric Champeau [ melix ]
          Fix Version/s 2.0-beta-2 [ 18072 ]
          Fix Version/s 1.7.11 [ 17244 ]
          Fix Version/s 1.8.5 [ 18071 ]
          Resolution Fixed [ 1 ]
          Paul King made changes -
          Status Resolved [ 5 ] Closed [ 6 ]

            People

            • Assignee:
              CÚdric Champeau
              Reporter:
              Travis
            • Votes:
              4 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: