EqualsAndHashCode(includeFields=true) transform does not work in gradle if a java file exists in the groovy source directory


(Steinar Haugen) #1

Many things came into play simultaneously when I came across this “hard-to-track” bug. Luckily, it is easy reproduce in a small standalone example and it is easy to work around.

To reproduce the bug, you will need to: 1. Have a groovy class with the ‘@EqualsAndHashCode(includeFields=true)’ transform. 2. A java class or enum somewhere in the ‘src/main/groovy’ directory. 3. A spock or junit test that uses the ‘equals’ method from the transform.

The bug is simply that the ‘equals’ method does not work any more. It seems like the transform is removed and equals fall back to normal object identity.

Note that: * if you remove the ‘includeFields=true’ part from the transform, the bug does not appear. * if you put the java class in the ‘src/main/java directory’, the bug does not appear. * the java class does not need to be used, it’s existence is enough to provoke the bug. * the test runs fine in IntelliJ, but fails in gradle.

Example -------

‘build.gradle’

apply plugin: ‘groovy’

apply plugin: ‘idea’

repositories {

mavenCentral()

}

dependencies {

compile ‘org.codehaus.groovy:groovy-all:2.1.7’

testCompile ‘org.spockframework:spock-core:0.7-groovy-2.0’

}

‘src/main/groovy/Description.groovy’

import groovy.transform.EqualsAndHashCode

@EqualsAndHashCode(includeFields=true)

class Description implements Serializable {

protected title

}

‘src/main/groovy/Status.java’

public enum Status {

started, stopped

}

‘src/test/groovy/DescriptionSpecification.groovy’

import spock.lang.Specification

class DescriptionSpecification extends Specification {

def “equals should work”() {

expect:

new Description(title: ‘title’) == new Description(title: ‘title’)

}

}