You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2019/03/08 02:35:00 UTC

[jira] [Commented] (GROOVY-9028) @EqualsAndHashCode can't recognize final field

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

Paul King commented on GROOVY-9028:
-----------------------------------

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)