Change-Id: I7a451f428f0aa3ea7fc322cac6b0e62d830d7987tags/7.4.0.beta1
@@ -130,16 +130,17 @@ public class BeanFieldGroup<T> extends FieldGroup { | |||
* The bean to use as data source. | |||
*/ | |||
public void setItemDataSource(T bean) { | |||
setItemDataSource(new BeanItem(bean)); | |||
setItemDataSource(new BeanItem<T>(bean, beanType)); | |||
} | |||
@Override | |||
public void setItemDataSource(Item item) { | |||
if (!(item instanceof BeanItem)) { | |||
if (item == null || (item instanceof BeanItem)) { | |||
super.setItemDataSource(item); | |||
} else { | |||
throw new RuntimeException(getClass().getSimpleName() | |||
+ " only supports BeanItems as item data source"); | |||
} | |||
super.setItemDataSource(item); | |||
} | |||
@Override |
@@ -62,7 +62,30 @@ public class BeanItem<BT> extends PropertysetItem { | |||
* | |||
*/ | |||
public BeanItem(BT bean) { | |||
this(bean, getPropertyDescriptors((Class<BT>) bean.getClass())); | |||
this(bean, (Class<BT>) bean.getClass()); | |||
} | |||
/** | |||
* <p> | |||
* Creates a new instance of <code>BeanItem</code> and adds all properties | |||
* of a Java Bean to it. The properties are identified by their respective | |||
* bean names. | |||
* </p> | |||
* | |||
* <p> | |||
* Note : This version only supports introspectable bean properties and | |||
* their getter and setter methods. Stand-alone <code>is</code> and | |||
* <code>are</code> methods are not supported. | |||
* </p> | |||
* | |||
* @param bean | |||
* the Java Bean to copy properties from. | |||
* @param beanClass | |||
* class of the {@code bean} | |||
* | |||
*/ | |||
public BeanItem(BT bean, Class<BT> beanClass) { | |||
this(bean, getPropertyDescriptors(beanClass)); | |||
} | |||
/** |
@@ -132,7 +132,7 @@ public class MethodProperty<T> extends AbstractProperty<T> { | |||
setArguments(getArgs, setArgs, setArgumentIndex); | |||
String name = (String) in.readObject(); | |||
Class<?>[] paramTypes = SerializerHelper.readClassArray(in); | |||
if (name != null) { | |||
if (instance != null && name != null) { | |||
setMethod = instance.getClass().getMethod(name, paramTypes); | |||
} else { | |||
setMethod = null; | |||
@@ -140,7 +140,7 @@ public class MethodProperty<T> extends AbstractProperty<T> { | |||
name = (String) in.readObject(); | |||
paramTypes = SerializerHelper.readClassArray(in); | |||
if (name != null) { | |||
if (instance != null && name != null) { | |||
getMethod = instance.getClass().getMethod(name, paramTypes); | |||
} else { | |||
getMethod = null; | |||
@@ -589,7 +589,11 @@ public class MethodProperty<T> extends AbstractProperty<T> { | |||
@Override | |||
public T getValue() { | |||
try { | |||
return (T) getMethod.invoke(instance, getArgs); | |||
if (instance == null) { | |||
return null; | |||
} else { | |||
return (T) getMethod.invoke(instance, getArgs); | |||
} | |||
} catch (final Throwable e) { | |||
throw new MethodException(this, e); | |||
} |
@@ -2,12 +2,16 @@ package com.vaadin.tests.server.component.fieldgroup; | |||
import static org.junit.Assert.assertEquals; | |||
import java.util.Collection; | |||
import org.junit.Assert; | |||
import org.junit.Test; | |||
import com.vaadin.data.Item; | |||
import com.vaadin.data.fieldgroup.BeanFieldGroup; | |||
import com.vaadin.data.fieldgroup.FieldGroup.CommitException; | |||
import com.vaadin.data.fieldgroup.PropertyId; | |||
import com.vaadin.data.util.BeanItem; | |||
import com.vaadin.ui.Field; | |||
import com.vaadin.ui.TextField; | |||
@@ -133,4 +137,31 @@ public class BeanFieldGroupTest { | |||
assertEquals(bean.nestedBean.hello, helloField.getValue().toString()); | |||
} | |||
@Test | |||
public void setDataSource_nullBean_nullBeanIsSetInDataSource() { | |||
BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class); | |||
group.setItemDataSource((MyBean) null); | |||
BeanItem<MyBean> dataSource = group.getItemDataSource(); | |||
Assert.assertNotNull("Data source is null for null bean", dataSource); | |||
Collection<?> itemPropertyIds = dataSource.getItemPropertyIds(); | |||
Assert.assertEquals("Unexpected number of properties", 3, | |||
itemPropertyIds.size()); | |||
for (Object id : itemPropertyIds) { | |||
Assert.assertNull("Value for property " + id + " is not null", | |||
dataSource.getItemProperty(id).getValue()); | |||
} | |||
} | |||
@Test | |||
public void setDataSource_nullItem_nullDataSourceIsSet() { | |||
BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class); | |||
group.setItemDataSource((Item) null); | |||
BeanItem<MyBean> dataSource = group.getItemDataSource(); | |||
Assert.assertNull("Group returns not null data source", dataSource); | |||
} | |||
} |