groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul King (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (GROOVY-9028) @EqualsAndHashCode can't recognize final field
Date Fri, 08 Mar 2019 02:36:00 GMT

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

Paul King edited comment on GROOVY-9028 at 3/8/19 2:35 AM:
-----------------------------------------------------------

This is indeed the new annotation attribute validation doing its job. In 2.4.16, it's not
really the case that the code "works fine". It does compile fine but if you look at the generated
code, it isn't what you would expect. For example, the {{hashCode}} method looks something
like this:
{code}
public int hashCode() {
    Object _result = HashCodeHelper.initHash()
    return _result 
}
{code}
If you add the missing annotation attribute, {{includeFields = true}}, then it compiles and
works correctly in 2.4.16 and 2.5+.
The resulting code for the {{hashCode}} method will now be:
{code}
public int hashCode() {
    Object _result = HashCodeHelper.initHash()
    if (!(name.is(this))) {
        _result = HashCodeHelper.updateHash(_result, name)
    }
    return _result 
}
{code}


was (Author: paulk):
This is indeed the new annotation attribute validation doing its job. In 2.4.16, it's not
really the case that the code "works fine". It does compile fine but if you look at the generated
code, it isn't what you would expect. For example, the {{hashCode}} method looks something
like this:
{code}
public int hashCode() {
    Object _result = HashCodeHelper.initHash()
    return _result 
}
{code}
If you add the missing annotation attribute, {{includeFields = true}}, then it compiles and
works correctly in 2.4.16 and 2.5+.
The resulting code for the {{hashCode}} nethod will now be:
{code}
public int hashCode() {
    Object _result = HashCodeHelper.initHash()
    if (!(name.is(this))) {
        _result = HashCodeHelper.updateHash(_result, name)
    }
    return _result 
}
{code}

> @EqualsAndHashCode can't recognize final field
> ----------------------------------------------
>
>                 Key: GROOVY-9028
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9028
>             Project: Groovy
>          Issue Type: Bug
>          Components: ast builder
>    Affects Versions: 2.5.3
>         Environment: Groovy Version: 2.5.3 JVM: 11.0.2 Vendor: Oracle Corporation OS:
Mac OS X
>            Reporter: zhb
>            Priority: Major
>
> {code:java}
> import groovy.transform.EqualsAndHashCode
> @EqualsAndHashCode(includes = ["name"])
> class Test {
>     private final String name
>     Test(String name) {
>         this.name = name
>     }
>     String getName() {
>         return name
>     }
> }
> {code}
> Groovy 2.5.3 complains: 
> {noformat}
> /Users/zhb/Projects/tmp/Main.groovy: 3: Error during @EqualsAndHashCode processing: 'includes'
property 'name' does not exist.
> @ line 3, column 1.
> @EqualsAndHashCode(includes = ["name"])
> ^
> 1 error
> {noformat}
> But Groovy 2.4.16 works fine.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message