You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Matthias Fuchs (Jira)" <ji...@apache.org> on 2020/01/15 14:12:00 UTC
[jira] [Created] (GROOVY-9364) @Sortable not working correctly when
applied to both current and super class
Matthias Fuchs created GROOVY-9364:
--------------------------------------
Summary: @Sortable not working correctly when applied to both current and super class
Key: GROOVY-9364
URL: https://issues.apache.org/jira/browse/GROOVY-9364
Project: Groovy
Issue Type: Bug
Affects Versions: 2.5.8
Reporter: Matthias Fuchs
The *compareTo* method added by the *@Sortable* annotation breaks if the super class also has a *@Sortable* annotation.
Example:
{code:java}
import static org.junit.Assert.assertTrue
import org.junit.jupiter.api.Test
import groovy.transform.Canonical
import groovy.transform.CompileStatic
import groovy.transform.Sortable
@CompileStatic
public class BeanSortingTest {
private int compare(Object o1, Object o2) {
return ((Comparable) o1).compareTo((Comparable) o2)
}
@Test
void testTestData() {
def t1 = new TestData(code: 100)
def t2 = new TestData(code: 200)
def c1 = new TestChild(value: 10, code: 10)
def c2 = new TestChild(value: 20, code: 10)
checkComparison(t1, t2)
checkComparison(c1, c2) <--- FAILS HERE
}
private void checkComparison(Object o1, Object o2) {
assertTrue(compare(o1, o2) == -1)
assertTrue(o1.equals(o2) == false)
}
@Canonical
@Sortable(includes = [ "code" ])
public static class TestData {
Integer code
}
@Canonical
@Sortable(includes = [ "value" ], includeSuperProperties = true)
public static class TestChild extends TestData {
Double value
}
}
{code}
What happens is that the *compareTo* of the super class is invoked, which only compare the *code* property. In this case both objects have the same value set for *code*.
In fact the sub types *compareTo* method should be used primarily and only after that the code field of the super class should be compared. It seems that the super type's compareTo is invoked instead as this sort c1 first (although value is higher:
{code:java}
def c1 = new TestChild(value: 30, code: 10)
def c2 = new TestChild(value: 20, code: 20){code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)