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

Little improvement in code generated for equals() by @EqualsAndHashCode ast transformation

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • None
    • 1.8.2, 1.9-beta-3
    • None
    • None
    • Patch

    Description

      Currently, equals() generated by @EqualsAndHashCode looks like

      class C {
            String a, b
            boolean equals(Object other) {
                    ...
                    ...
                    if(!(other instanceof C)) {return false}
                    if(this.a != other.a) {...}
                    if(this.b != other.b) {...}
                    ...
            }
      }
      

      It access getters/fields of enclosing class using type Object, which is not ideal and creates a little issue for static analysis done by Groovy++.

      A slightly better implementation could be:

      class C {
            String a, b
            boolean equals(Object other) {
                    ...
                    ...
                    if(!(other instanceof C)) {return false}
                    C another = (C) other
                    if(this.a != another.a) {...}
                    if(this.b != another.b) {...}
                    ...
            }
      }
      

      The patch attach has been reviewed on the "dev" mailing list.

      Attachments

        Activity

          People

            roshandawrani Roshan Dawrani
            roshandawrani Roshan Dawrani
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: