Browse Source

Render Object.toString() when no converter is set (#15417)

Change-Id: I8a00857d5829cd01ceb98c4264029f62e45e7073
tags/7.4.0.rc1
Artur Signell 9 years ago
parent
commit
f8bc96e146

+ 11
- 14
server/src/com/vaadin/data/RpcDataProviderExtension.java View File

@@ -25,7 +25,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.thirdparty.guava.common.collect.BiMap;
@@ -40,7 +39,6 @@ import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.Property.ValueChangeNotifier;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.Converter.ConversionException;
import com.vaadin.server.AbstractExtension;
import com.vaadin.server.ClientConnector;
import com.vaadin.server.KeyMapper;
@@ -1060,20 +1058,19 @@ public class RpcDataProviderExtension extends AbstractExtension {
try {
presentationValue = presentationType.cast(modelValue);
} catch (ClassCastException e) {
ConversionException ee = new Converter.ConversionException(
"Unable to convert value of type "
+ modelValue.getClass().getName()
+ " to presentation type "
+ presentationType.getName()
+ ". No converter is set and the types are not compatible.");
if (presentationType == String.class) {
// We don't want to throw an exception for the default cause
// when one column can't be rendered. Just log the exception
// and let the column be empty
presentationValue = (T) "";
getLogger().log(Level.SEVERE, ee.getMessage(), ee);
// If there is no converter, just fallback to using
// toString().
// modelValue can't be null as Class.cast(null) will always
// succeed
presentationValue = (T) modelValue.toString();
} else {
throw ee;
throw new Converter.ConversionException(
"Unable to convert value of type "
+ modelValue.getClass().getName()
+ " to presentation type "
+ presentationType.getName()
+ ". No converter is set and the types are not compatible.");
}
}
} else {

+ 6
- 1
server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java View File

@@ -44,6 +44,11 @@ public class RendererTest {

private static class TestBean {
int i = 42;

@Override
public String toString() {
return "TestBean [" + i + "]";
}
}

private static class ExtendedBean extends TestBean {
@@ -177,7 +182,7 @@ public class RendererTest {

@Test
public void testEncodingWithoutConverter() throws Exception {
assertEquals("", render(baz, new TestBean()).asString());
assertEquals("TestBean [42]", render(baz, new TestBean()).asString());
}

@Test

Loading…
Cancel
Save