]> source.dussan.org Git - vaadin-framework.git/commitdiff
#4995 Nested bean property support: no extra property type parameter to addNestedCont...
authorHenri Sara <henri.sara@itmill.com>
Tue, 19 Apr 2011 12:48:04 +0000 (12:48 +0000)
committerHenri Sara <henri.sara@itmill.com>
Tue, 19 Apr 2011 12:48:04 +0000 (12:48 +0000)
svn changeset:18385/svn branch:6.6

src/com/vaadin/data/util/AbstractBeanContainer.java
src/com/vaadin/data/util/MethodProperty.java
src/com/vaadin/data/util/NestedMethodProperty.java
src/com/vaadin/data/util/NestedPropertyDescriptor.java
tests/src/com/vaadin/tests/server/container/BeanContainerTest.java
tests/src/com/vaadin/tests/server/container/BeanItemContainerTest.java
tests/src/com/vaadin/tests/server/container/PropertyDescriptorTest.java

index 1daa4050c54ab0f6495f18501edd7577a2a8c754..7ed8e731b948a4f10105b23ff88db7b9c4798388 100644 (file)
@@ -766,10 +766,9 @@ public abstract class AbstractBeanContainer<IDTYPE, BEANTYPE> extends
      * @param propertyType
      * @return true if the property was added
      */
-    public boolean addNestedContainerProperty(String propertyId,
-            Class<?> propertyType) {
+    public boolean addNestedContainerProperty(String propertyId) {
         return addContainerProperty(propertyId, new NestedPropertyDescriptor(
-                propertyId, propertyType));
+                propertyId, type));
     }
 
     @Override
index 3ae84ac5e6aac996dcb2c27e97abb9d0e66a847b..deb3177094b4469d15e0d512d4934a0fee3fd5ef 100644 (file)
@@ -515,8 +515,8 @@ public class MethodProperty<T> extends AbstractProperty {
      * @throws NoSuchMethodException
      *             if no getter found
      */
-    protected static Method initGetterMethod(String propertyName,
-            final Class<?> beanClass) throws NoSuchMethodException {
+    static Method initGetterMethod(String propertyName, final Class<?> beanClass)
+            throws NoSuchMethodException {
         propertyName = propertyName.substring(0, 1).toUpperCase()
                 + propertyName.substring(1);
 
@@ -536,7 +536,7 @@ public class MethodProperty<T> extends AbstractProperty {
         return getMethod;
     }
 
-    protected static Class<?> convertPrimitiveType(Class<?> type) {
+    static Class<?> convertPrimitiveType(Class<?> type) {
         // Gets the return type from get method
         if (type.isPrimitive()) {
             if (type.equals(Boolean.TYPE)) {
@@ -667,7 +667,7 @@ public class MethodProperty<T> extends AbstractProperty {
      *            type into which the value should be converted
      * @return converted value
      */
-    protected static Object convertValue(Object value, Class<?> type) {
+    static Object convertValue(Object value, Class<?> type) {
         if (null == value || type.isAssignableFrom(value.getClass())) {
             return value;
         }
index 39bd6f7b3afb9caa9eea8fe4cdd2420f6445cf56..3d400d7d023607e077e2cf1de7c0572a46462638 100644 (file)
@@ -54,7 +54,7 @@ public class NestedMethodProperty extends AbstractProperty {
             ClassNotFoundException {
         in.defaultReadObject();
 
-        initialize(instance, propertyName);
+        initialize(instance.getClass(), propertyName);
     }
 
     /**
@@ -71,30 +71,44 @@ public class NestedMethodProperty extends AbstractProperty {
      */
     public NestedMethodProperty(Object instance, String propertyName) {
         this.instance = instance;
-        initialize(instance, propertyName);
+        initialize(instance.getClass(), propertyName);
+    }
+
+    /**
+     * For internal use to deduce property type etc. without a bean instance.
+     * Calling {@link #setValue(Object)} or {@link #getValue()} on properties
+     * constructed this way is not supported.
+     * 
+     * @param instanceClass
+     *            class of the top-level bean
+     * @param propertyName
+     */
+    NestedMethodProperty(Class<?> instanceClass, String propertyName) {
+        instance = null;
+        initialize(instanceClass, propertyName);
     }
 
     /**
      * Initializes most of the internal fields based on the top-level bean
      * instance and property name (dot-separated string).
      * 
-     * @param instance
-     *            top-level bean to which the property applies
+     * @param beanClass
+     *            class of the top-level bean to which the property applies
      * @param propertyName
      *            dot separated nested property name
      * @throws IllegalArgumentException
      *             if the property name is invalid
      */
-    private void initialize(Object instance, String propertyName)
+    private void initialize(Class<?> beanClass, String propertyName)
             throws IllegalArgumentException {
 
         List<Method> getMethods = new ArrayList<Method>();
 
         String lastSimplePropertyName = propertyName;
-        Class<?> lastClass = instance.getClass();
+        Class<?> lastClass = beanClass;
 
         // first top-level property, then go deeper in a loop
-        Class<?> propertyClass = instance.getClass();
+        Class<?> propertyClass = beanClass;
         String[] simplePropertyNames = propertyName.split("\\.");
         if (propertyName.endsWith(".") || 0 == simplePropertyNames.length) {
             throw new IllegalArgumentException("Invalid property name '"
index 22c92014b68bebf06b34751df61aa4aff3bb02d7..cbe800dff3ef61d9d2c64ab8ea73d6bedc566eb3 100644 (file)
@@ -24,16 +24,18 @@ public class NestedPropertyDescriptor<BT> implements
      * Creates a property descriptor that can create MethodProperty instances to
      * access the underlying bean property.
      * 
-     * The property is only validated when trying to access its value.
-     * 
      * @param name
      *            of the property in a dotted path format, e.g. "address.street"
-     * @param propertyType
-     *            type (class) of the property
+     * @param beanType
+     *            type (class) of the top-level bean
+     * @throws IllegalArgumentException
+     *             if the property name is invalid
      */
-    public NestedPropertyDescriptor(String name, Class<?> propertyType) {
+    public NestedPropertyDescriptor(String name, Class<BT> beanType)
+            throws IllegalArgumentException {
         this.name = name;
-        this.propertyType = propertyType;
+        NestedMethodProperty property = new NestedMethodProperty(beanType, name);
+        this.propertyType = property.getType();
     }
 
     public String getName() {
index 921132f858e895f799376d9a4526a4f85c2566c5..1b3bf6974582d4e00225cecfd1b67248373dcbf1 100644 (file)
@@ -416,8 +416,7 @@ public class BeanContainerTest extends AbstractBeanContainerTest {
         container.addBean(new NestedMethodPropertyTest.Person("John",
                 new NestedMethodPropertyTest.Address("Ruukinkatu 2-4", 20540)));
 
-        assertTrue(container.addNestedContainerProperty("address.street",
-                String.class));
+        assertTrue(container.addNestedContainerProperty("address.street"));
         assertEquals("Ruukinkatu 2-4",
                 container.getContainerProperty("John", "address.street")
                         .getValue());
index 60ea01fdc7e71ee776a42167f250d08466487d38..09062fa138c77c266e2d27347a256b8d6f91b0dd 100644 (file)
@@ -709,8 +709,7 @@ public class BeanItemContainerTest extends AbstractBeanContainerTest {
                         20540));
         container.addBean(john);
 
-        assertTrue(container.addNestedContainerProperty("address.street",
-                String.class));
+        assertTrue(container.addNestedContainerProperty("address.street"));
         assertEquals("Ruukinkatu 2-4",
                 container.getContainerProperty(john, "address.street")
                         .getValue());
index 69efc5f324e6fa8878f215bddb39c97dae8bd3b6..c66d36ce8e3c14213661ea1a1fed46cefd48c11e 100644 (file)
@@ -43,7 +43,7 @@ public class PropertyDescriptorTest extends TestCase {
 
     public void testNestedPropertyDescriptorSerialization() throws Exception {
         NestedPropertyDescriptor<Person> pd = new NestedPropertyDescriptor<Person>(
-                "name", String.class);
+                "name", Person.class);
 
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         new ObjectOutputStream(baos).writeObject(pd);