diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index 268fdfa47ed..442419e94ab 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -5061,7 +5061,7 @@ protected SerializableComparator createSortingComparator() { @Override protected void internalSetDataProvider(DataProvider dataProvider) { - boolean newProvider = getDataProvider() != dataProvider; + boolean newProvider = internalGetDataProvider() != dataProvider; super.internalSetDataProvider(dataProvider); if (newProvider) { Set oldVisibleDetails = new HashSet<>( diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridOverrideDataProviderGetterTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridOverrideDataProviderGetterTest.java new file mode 100644 index 00000000000..67f60fb7130 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridOverrideDataProviderGetterTest.java @@ -0,0 +1,74 @@ +package com.vaadin.tests.components.grid; + +import com.vaadin.data.provider.ListDataProvider; +import com.vaadin.ui.Grid; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class GridOverrideDataProviderGetterTest +{ + private ListDataProvider listDataProvider; + + @Before + public void setUp() + { + MyBean myBean1 = new MyBean("name1"); + MyBean myBean2 = new MyBean("name2"); + listDataProvider = new ListDataProvider<>(Arrays.asList(myBean1,myBean2)); + } + + /** + * Originally calling the constructor of MyGrid will throw: + *
+     * java.lang.ClassCastException: class com.vaadin.data.provider.CallbackDataProvider cannot be cast to class com.vaadin.data.provider.ListDataProvider (com.vaadin.data.provider.CallbackDataProvider and com.vaadin.data.provider.ListDataProvider are in unnamed module of loader 'app')
+     * 
+ * Don't call an overridable method in your constructor is the moral of the story + */ + @Test + public void testCalling() + { + try { + Grid grid = new MyGrid<>(listDataProvider); + assertTrue(grid.getDataProvider() instanceof ListDataProvider); + } catch(ClassCastException e) { + fail("Should not throw a ClassCastException error:" + e); + } + } + + private static class MyGrid extends Grid + { + public MyGrid(ListDataProvider myDataProvider) + { + super(myDataProvider); + } + + /** + * I'm overriding this because I can + */ + @Override + public ListDataProvider getDataProvider() + { + return (ListDataProvider) super.getDataProvider(); + } + } + + private static class MyBean + { + private final String name; + + MyBean(String name) + { + this.name = name; + } + + public String getName() + { + return name; + } + } +}