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/src/main/java/com/vaadin/data/BeanBinder.java | |
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/src/main/java/com/vaadin/data/BeanBinder.java')
-rw-r--r-- | server/src/main/java/com/vaadin/data/BeanBinder.java | 11 |
1 files changed, 10 insertions, 1 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; |