Skip to content

Commit 130a4ef

Browse files
committed
Enhance EditingSupport/*CellEditor assertions
When CellEditor used expects different object type, there is assertion message like: ``` org.eclipse.core.runtime.AssertionFailedException: assertion failed: at org.eclipse.core.runtime.Assert.isTrue(Assert.java:121) at org.eclipse.core.runtime.Assert.isTrue(Assert.java:106) at org.eclipse.jface.viewers.TextCellEditor.doSetValue(TextCellEditor.java:225) at org.eclipse.jface.viewers.CellEditor.setValue(CellEditor.java:853) at org.eclipse.jface.viewers.EditingSupport.initializeCellEditorValue(EditingSupport.java:102) ``` which is not really helpful in identifying the exact cause. Enhance the assertions to give as much information as possible.
1 parent aaf2861 commit 130a4ef

6 files changed

Lines changed: 116 additions & 10 deletions

File tree

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/CheckboxCellEditor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2014 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -126,7 +126,9 @@ protected void doSetFocus() {
126126
*/
127127
@Override
128128
protected void doSetValue(Object value) {
129-
Assert.isTrue(value instanceof Boolean);
129+
String valueType = value == null ? "null" : value.getClass().getName(); //$NON-NLS-1$
130+
Assert.isTrue(value instanceof Boolean,
131+
"CheckboxCellEditor expects a Boolean value but got " + valueType); //$NON-NLS-1$
130132
this.value = ((Boolean) value).booleanValue();
131133
}
132134

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/ComboBoxCellEditor.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2019 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -226,7 +226,10 @@ public LayoutData getLayoutData() {
226226
*/
227227
@Override
228228
protected void doSetValue(Object value) {
229-
Assert.isTrue(comboBox != null && (value instanceof Integer));
229+
Assert.isTrue(comboBox != null);
230+
String valueType = value == null ? "null" : value.getClass().getName(); //$NON-NLS-1$
231+
Assert.isTrue(value instanceof Integer,
232+
"ComboBoxCellEditor expects an Integer value but got " + valueType); //$NON-NLS-1$
230233
selection = ((Integer) value).intValue();
231234
comboBox.select(selection);
232235
}

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/EditingSupport.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2006, 2015 IBM Corporation and others.
2+
* Copyright (c) 2006, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -17,6 +17,7 @@
1717
package org.eclipse.jface.viewers;
1818

1919
import org.eclipse.core.runtime.Assert;
20+
import org.eclipse.core.runtime.AssertionFailedException;
2021

2122
/**
2223
* EditingSupport is the abstract superclass of the support for cell editing.
@@ -99,7 +100,18 @@ public ColumnViewer getViewer() {
99100
*/
100101
protected void initializeCellEditorValue(CellEditor cellEditor, ViewerCell cell) {
101102
Object value = getValue(cell.getElement());
102-
cellEditor.setValue(value);
103+
try {
104+
cellEditor.setValue(value);
105+
} catch (AssertionFailedException e) {
106+
String valueType = value == null ? "null" : value.getClass().getName(); //$NON-NLS-1$
107+
Assert.isTrue(false, "Failed to initialize cell editor " + cellEditor.getClass().getName() //$NON-NLS-1$
108+
+ " at column index " + cell.getColumnIndex() //$NON-NLS-1$
109+
+ " for element " + cell.getElement() //$NON-NLS-1$
110+
+ " with value of type " + valueType //$NON-NLS-1$
111+
+ ". The value returned by getValue(Object) is incompatible with the cell editor;" //$NON-NLS-1$
112+
+ " check that the EditingSupport and the CellEditor for this column match. Cause: " //$NON-NLS-1$
113+
+ e.getMessage());
114+
}
103115
}
104116

105117
/**

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/TextCellEditor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2015 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -222,7 +222,9 @@ protected void doSetFocus() {
222222
*/
223223
@Override
224224
protected void doSetValue(Object value) {
225-
Assert.isTrue(text != null && (value instanceof String));
225+
Assert.isTrue(text != null);
226+
String valueType = value == null ? "null" : value.getClass().getName(); //$NON-NLS-1$
227+
Assert.isTrue(value instanceof String, "TextCellEditor expects a String value but got " + valueType); //$NON-NLS-1$
226228
text.removeModifyListener(getModifyListener());
227229
text.setText((String) value);
228230
text.addModifyListener(getModifyListener());

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/viewers/AllViewersTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2017 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -29,7 +29,8 @@
2929
ListViewerRefreshTest.class, Bug200558Test.class, Bug201002TableViewerTest.class, Bug201002TreeViewerTest.class,
3030
Bug200337TableViewerTest.class, //
3131
Bug205700TreeViewerTest.class, Bug180504TableViewerTest.class, Bug180504TreeViewerTest.class,
32-
Bug256889TableViewerTest.class, Bug287765Test.class, Bug242231Test.class, StyledStringBuilderTest.class,
32+
Bug256889TableViewerTest.class, Bug287765Test.class, Bug242231Test.class,
33+
EditingSupportValueMismatchTest.class, StyledStringBuilderTest.class,
3334
TreeViewerWithLimitTest.class, TreeViewerWithLimitCompatibilityTest.class, TableViewerWithLimitTest.class,
3435
TableViewerWithLimitCompatibilityTest.class })
3536
public class AllViewersTests {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 Aleksandar Kurtakov and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.jface.tests.viewers;
12+
13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
import static org.junit.jupiter.api.Assertions.assertThrows;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
17+
import org.eclipse.core.runtime.AssertionFailedException;
18+
import org.eclipse.jface.viewers.CellEditor;
19+
import org.eclipse.jface.viewers.ColumnLabelProvider;
20+
import org.eclipse.jface.viewers.EditingSupport;
21+
import org.eclipse.jface.viewers.StructuredViewer;
22+
import org.eclipse.jface.viewers.TableViewer;
23+
import org.eclipse.jface.viewers.TableViewerColumn;
24+
import org.eclipse.jface.viewers.TextCellEditor;
25+
import org.eclipse.swt.SWT;
26+
import org.eclipse.swt.widgets.Composite;
27+
import org.junit.jupiter.api.Test;
28+
29+
/**
30+
* Verifies that when an {@link EditingSupport} {@code getValue(Object)} returns
31+
* a value incompatible with the {@link CellEditor} attached to the column,
32+
* there is enough context (column index, element, value type) to help finding
33+
* the cause.
34+
*/
35+
public class EditingSupportValueMismatchTest extends ViewerTestCase {
36+
37+
private TableViewer tableViewer;
38+
39+
@Override
40+
protected StructuredViewer createViewer(Composite parent) {
41+
tableViewer = new TableViewer(parent, SWT.NONE);
42+
tableViewer.setContentProvider(new TestModelContentProvider());
43+
44+
TableViewerColumn tableColumn = new TableViewerColumn(tableViewer, SWT.NONE);
45+
tableColumn.setEditingSupport(new EditingSupport(tableViewer) {
46+
47+
@Override
48+
protected void setValue(Object element, Object value) {
49+
// not reached in this test
50+
}
51+
52+
@Override
53+
protected Object getValue(Object element) {
54+
// Return a non-String value for TextCellEditor, which expects a String.
55+
return Integer.valueOf(42);
56+
}
57+
58+
@Override
59+
protected CellEditor getCellEditor(Object element) {
60+
return new TextCellEditor(tableViewer.getTable());
61+
}
62+
63+
@Override
64+
protected boolean canEdit(Object element) {
65+
return true;
66+
}
67+
});
68+
tableColumn.setLabelProvider(new ColumnLabelProvider());
69+
return tableViewer;
70+
}
71+
72+
@Test
73+
public void testMismatchProducesDetailedException() {
74+
TestElement testElement = fRootElement.getChildAt(0);
75+
76+
AssertionFailedException ex = assertThrows(AssertionFailedException.class,
77+
() -> tableViewer.editElement(testElement, 0));
78+
79+
String message = ex.getMessage();
80+
assertNotNull(message);
81+
assertTrue(message.contains("TextCellEditor"), "Name the cell editor: " + message);
82+
assertTrue(message.contains("column index 0"), "Report the column index: " + message);
83+
assertTrue(message.contains(Integer.class.getName()), "Report the offending value type: " + message);
84+
assertTrue(message.contains("Cause:"), "Original assertion message: " + message);
85+
}
86+
}

0 commit comments

Comments
 (0)