Groovy
  1. Groovy
  2. GROOVY-4645

Convariant returns causes compiler to fail when generating property getters

    Details

    • Type: Bug Bug
    • Status: 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

      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

          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open Resolved Resolved
          305d 14h 8m 1 Cédric Champeau 29/Nov/11 11:15
          Resolved Resolved Closed Closed
          24d 15h 52m 1 Paul King 24/Dec/11 03:07
          Mark Thomas made changes -
          Workflow jira [ 12972845 ] Default workflow, editable Closed status [ 12974236 ]
          Mark Thomas made changes -
          Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
          Mark Thomas made changes -
          Workflow jira [ 12733577 ] Default workflow, editable Closed status [ 12745393 ]
          Mark Thomas made changes -
          Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
          Paul King made changes -
          Status Resolved [ 5 ] Closed [ 6 ]
          Cédric Champeau made changes -
          Fix Version/s 1.7.11 [ 17244 ]
          Resolution Fixed [ 1 ]
          Fix Version/s 2.0-beta-2 [ 18072 ]
          Status Open [ 1 ] Resolved [ 5 ]
          Fix Version/s 1.8.5 [ 18071 ]
          Assignee Cedric Champeau [ melix ]
          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 -
          Link This issue relates to GROOVY-4415 [ GROOVY-4415 ]
          Hide
          Paul King added a comment -

          Possibly overlaps with GROOVY-4415?

          Show
          Paul King added a comment - Possibly overlaps with GROOVY-4415 ?
          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 -

          add code tags

          Show
          Paul King added a comment - add code tags
          Travis Schneeberger created issue -

            People

            • Assignee:
              Cédric Champeau
              Reporter:
              Travis Schneeberger
            • Votes:
              4 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development