diff options
author | Denis Anisimov <denis@vaadin.com> | 2016-09-16 17:09:44 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-09-21 08:18:05 +0000 |
commit | 53427c897cacc979d8378d46cd8b72fd0cd2fbe6 (patch) | |
tree | 4da936f5f4ec7c4e7b14c5fd83625401bce3620b /server | |
parent | 2a0cbd3bdbebaa25b44a096ae3ac6462a408d92d (diff) | |
download | vaadin-framework-53427c897cacc979d8378d46cd8b72fd0cd2fbe6.tar.gz vaadin-framework-53427c897cacc979d8378d46cd8b72fd0cd2fbe6.zip |
Avoid explicit casting to getter return type in getDescriptor() (#292).
BeanBinder may throw a ClassCastException in some cases because of
explicit "cast" call which doesn't do boxing/unboxing.
Change-Id: I8fc61612c2912f2626c075a4fc19cb812adf8aa2
Diffstat (limited to 'server')
-rw-r--r-- | server/src/main/java/com/vaadin/data/BeanBinder.java | 11 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/data/BeanBinderTest.java | 14 |
2 files changed, 22 insertions, 3 deletions
diff --git a/server/src/main/java/com/vaadin/data/BeanBinder.java b/server/src/main/java/com/vaadin/data/BeanBinder.java index 21aeb84735..b27e3ee4c3 100644 --- a/server/src/main/java/com/vaadin/data/BeanBinder.java +++ b/server/src/main/java/com/vaadin/data/BeanBinder.java @@ -30,6 +30,7 @@ import com.vaadin.data.util.converter.Converter; import com.vaadin.data.validator.BeanValidator; import com.vaadin.ui.AbstractMultiSelect; import com.vaadin.ui.AbstractSingleSelect; +import com.vaadin.util.ReflectTools; /** * A {@code Binder} subclass specialized for binding <em>beans</em>: classes @@ -237,11 +238,19 @@ public class BeanBinder<BEAN> extends Binder<BEAN> { @SuppressWarnings("unchecked") private Converter<TARGET, Object> createConverter() { return Converter.from( - fieldValue -> getter.getReturnType().cast(fieldValue), + fieldValue -> cast(fieldValue, getter.getReturnType()), propertyValue -> (TARGET) propertyValue, exception -> { throw new RuntimeException(exception); }); } + + private <T> T cast(TARGET value, Class<T> clazz) { + if (clazz.isPrimitive()) { + return (T) ReflectTools.convertPrimitiveType(clazz).cast(value); + } else { + return clazz.cast(value); + } + } } private final Class<? extends BEAN> beanType; diff --git a/server/src/test/java/com/vaadin/data/BeanBinderTest.java b/server/src/test/java/com/vaadin/data/BeanBinderTest.java index 1d775a2a6d..17b66ddbb9 100644 --- a/server/src/test/java/com/vaadin/data/BeanBinderTest.java +++ b/server/src/test/java/com/vaadin/data/BeanBinderTest.java @@ -10,8 +10,8 @@ import org.junit.Test; import com.vaadin.tests.data.bean.BeanToValidate; -public class BeanBinderTest extends - BinderTestBase<BeanBinder<BeanToValidate>, BeanToValidate> { +public class BeanBinderTest + extends BinderTestBase<BeanBinder<BeanToValidate>, BeanToValidate> { @Before public void setUp() { @@ -154,6 +154,16 @@ public class BeanBinderTest extends assertEquals("32", ageField.getValue()); } + @Test + public void beanBinderWithBoxedType() { + binder.forField(ageField) + .withConverter(Integer::valueOf, String::valueOf).bind("age"); + binder.bind(item); + + ageField.setValue(String.valueOf(20)); + assertEquals(20, item.getAge()); + } + private void assertInvalid(HasValue<?> field, String message) { BinderValidationStatus<?> status = binder.validate(); List<ValidationStatus<?>> errors = status.getFieldValidationErrors(); |