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

@EqualsAndHashCode.excludes doesn't work correctly with class inheritance

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8.0
    • 1.8.1
    • None
    • None

    Description

      I cannot inherit exclusions of EqualsAndHashCode, I have to duplicate exclusions in every child class.
      Please see the tescase, only testA() and testE() pass.

      class EqualsTest {
          @EqualsAndHashCode(excludes = 'a1')
          static class A {
              def a1, a2;
          }
      
          @Test
          void testA() {
              assert new A(a1: 'same', a2: 'same') == new A(a1: 'same', a2: 'same') // ok
              assert new A(a1: 'diff', a2: 'same') == new A(a1: 'erent', a2: 'same') // ok
          }
      
          static class B extends A {
              def b
          }
      
          @Test
          void testB() {
              assert new B(a1: 'diff', a2: 'same', b: 'same') == new B(a1: 'erent', a2: 'same', b: 'same')
              // failed! Shouldn't equals() be inherited from A?
          }
      
          //ok. let's try to specify explicitly that we call super.equals()
          @EqualsAndHashCode(callSuper = true)
          static class C extends A {
              def c
          }
      
          @Test
          void testC() {
              assert new C(a1: 'diff', a2: 'same', c: 'same') == new C(a1: 'erent', a2: 'same', c: 'same')
              // failed!
          }
      
          //hm. what about excluding it again in the child class?
          @EqualsAndHashCode(callSuper = true, excludes = 'a1')
          static class D extends A {
              def d
          }
      
          @Test
          void testD() {
              assert new D(a1: 'diff', a2: 'same', d: 'same') == new D(a1: 'erent', a2: 'same', d: 'same')
              // failed!
          }
      
          //hm. should I forget about inheritance??
          @EqualsAndHashCode(excludes = 'a1')
          static class E extends A {
              def e
          }
      
          @Test
          void testE() {
              assert new E(a1: 'diff', a2: 'same', e: 'same') == new E(a1: 'erent', a2: 'same', e: 'same')
              // YES :(
          }
      
      }
      

      Attachments

        1. equalsExcludes.groovy
          2 kB
          Pavlo Morgunyuk

        Activity

          People

            paulk Paul King
            pawlom Pavlo Morgunyuk
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: