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

Wrong method is chosen when using super with generics

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.4
    • 2.5.0-alpha-1
    • None
    • None

    Description

      Given code:

      generics_extension_test.groovy
      interface ParamInterface{}
      
      class ParamImplementation implements ParamInterface{}
      
      class ParamExtension extends ParamImplementation {}
      
      class A<T extends ParamInterface> {
      
          def getResult(T a) {
              return "A"
          }
      }
      
      class B<T extends ParamImplementation> extends A<T> {
          def getResult(T b) {
              return "B"
          }
      }
      
      class C extends B<ParamExtension> {
          @Override
          def getResult(ParamExtension b) {
              return super.getResult(b)
          }
      }
      
      
      String result = new C().getResult(new ParamExtension())
      assert result == "B"
      

      I get output:

      Assertion failed: 
      
      assert result == "B"
             |      |
             A      false
      
      	at generics_extension_test.run(generics_extension_test.groovy:31)
      

      When calling super method, instead of looking for method in a class one hierarchy bellow, groovy goes through complex calculations for finding a better method. I debug'ed up to a point in
      groovy.lang.MetaClassImpl#chooseMostSpecificParams
      where it turned out that distance for interface was 2 and distance for class that directly extends the method was much bigger, therefore the method from class A is chosen instead of method from class B. However, looking objectively, it would seem that B.getResult is a much better fit:
      C extends B and B extends A
      parameter hierarchy is shorter (ParamExtension -> ParamImplementation -> ParamInterface)

      Attachments

        1. generics_extension_test.groovy
          0.6 kB
          Marius Vaitkus

        Activity

          People

            blackdrag Jochen Theodorou
            MariusV Marius Vaitkus
            Votes:
            5 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: