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

Paul King commented on GROOVY-10515:
------------------------------------

You don't need the include attribute unless you want to select a subset of 
fields/properties but it any case it is only for fields/properties of the 
current class (though in 2.4 we didn't check this). All fields/properties of 
the current class are selected by default. So feel free to not mention 
level0field in the Level0 class in the given example. It will still be involved 
in the equals/hashCode calculations for Level0.

There is no way to directly get level0field included in the equals/hashCode 
calculations for Level1 but you get it indirectly by using callSuper since that 
will factor the super equals/hashCode calculations from Level0 into Level1. You 
can freely leave out the includes of level1field from the Level1 definition 
since the default will be all fields/properties.

I'd suggest cutting and pasting your example into the GroovyConsole and 
inspecting the AST. It will show you what is happening. So for example for this 
code:
{code}
@EqualsAndHashCode(includeFields = true)
abstract class Level0 {
  public String level0field
}

@EqualsAndHashCode(includeFields = true, callSuper = true)
class Level1 extends Level0 {
  public String level1field
}
{code}
Using inspect AST I see the following definition (slightly cleaned up) for 
{{hashCode}} in {{Level1}}:
{code}
int hashCode() {
    Object _result = HashCodeHelper.initHash()
    if (!(level1field.is(this))) {
        _result = HashCodeHelper.updateHash(_result, level1field)
    }
    _result = HashCodeHelper.updateHash(_result, super.hashCode())
    return _result 
}
{code}

> EqualsAndHashCode no longer access parent class
> -----------------------------------------------
>
>                 Key: GROOVY-10515
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10515
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: David Brown
>            Priority: Minor
>
> It appears there is a breaking change between Groovy 2.4 and 2.5 to the 
> EqualsAndHashCode() method.   In 2.4, the includes attribute could reference 
> properties of parent classes up the inheritance tree, however, in 2.5 an 
> exception is thrown indicating the inherited field(s) do not exist. 
> The following code demonstrates a simple inheritance relationship with 
> reproducible exception that works correctly with Groovy 2.4.x 
> {code:java}
> import groovy.transform.EqualsAndHashCode
> @EqualsAndHashCode(includes = ['level0field'], includeFields = true)
> abstract class Level0 {
>   public String level0field
> }
> @EqualsAndHashCode(includes = ['level0field', 'level1field'], callSuper = 
> true, includeFields = true)
> class Level1 extends Level0 {
>   String level1field
> }
>  {code}



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

Reply via email to