[ 
https://issues.apache.org/jira/browse/GROOVY-6151?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17477265#comment-17477265
 ] 

Eric Milles commented on GROOVY-6151:
-------------------------------------

[~blackdrag] Thanks for your take.  I did mean to use {{getDeclaredMethods()}} 
to avoid using equals supplied by {{AbstractMap}}.  But as you noted, it does 
break down quite quickly.

It seems the best advice for the original scenario is to add an overload of 
equals to step in front of the extension method:
{code:java}
  boolean equals(Map that) {
     this.equals((Object)that)
  }
// or
  boolean equals(TestError that) {
     this.equals((Object)that)
  }
{code}

> Equal on class (extends Map)
> ----------------------------
>
>                 Key: GROOVY-6151
>                 URL: https://issues.apache.org/jira/browse/GROOVY-6151
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.1.3
>            Reporter: Grzegorz Karawacki
>            Priority: Major
>         Attachments: Main.groovy, TestError.groovy
>
>
> The problem occurs for classes inherit from class Map:
> {code:title=TestError.groovy|borderStyle=solid}
> class TestError extends HashMap {
>     String id
>     String getId() {
>         return id
>     }
>     void setId(String id) {
>         this.id = id
>     }
>     boolean equals(Object o) {
>         if (this.is(o)) return true
>         if (getClass() != o.class) return false
>         if (!super.equals(o)) return false
>         TestError testError = (TestError) o
>         if (id != testError.id) return false
>         return true
>     }
>     int hashCode() {
>         int result = super.hashCode()
>         result = 31 * result + (id != null ? id.hashCode() : 0)
>         return result
>     }
> }
> {code}
> {code:title=Main.groovy|borderStyle=solid}
> TestError t1 = new TestError()
> t1.setId("1")
> TestError t2 = new TestError()
> t2.setId("2")
> println(t1.getId())
> println(t2.getId())
> println(t1.equals(t2))
> {code}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to