]> source.dussan.org Git - vaadin-framework.git/commitdiff
Search fields from superclasses #10504 91/491/3
authorJonni Nakari <jonni@vaadin.com>
Thu, 13 Dec 2012 12:58:18 +0000 (14:58 +0200)
committerVaadin Code Review <review@vaadin.com>
Mon, 17 Dec 2012 11:47:31 +0000 (11:47 +0000)
Modified buildAndBindMemberFields method to use a new
getFieldsInDeclareOrder method that searches fields also from
superclasses. Added a unit test to verify the new logic. Fixes the issue
in ticket #10504

Change-Id: Ic855e274c5b4d1c83760d6c2c53c67413a1da42c

server/src/com/vaadin/data/fieldgroup/FieldGroup.java
server/tests/src/com/vaadin/data/util/ReflectToolsGetSuperField.java [new file with mode: 0644]

index 51cd3126cbd7b99973b41b1199530c069f9d63a5..88030548570b96ed7d8028e0c78fbe281d652a2a 100644 (file)
@@ -794,8 +794,7 @@ public class FieldGroup implements Serializable {
             boolean buildFields) throws BindException {
         Class<?> objectClass = objectWithMemberFields.getClass();
 
-        for (java.lang.reflect.Field memberField : objectClass
-                .getDeclaredFields()) {
+        for (java.lang.reflect.Field memberField : getFieldsInDeclareOrder(objectClass)) {
 
             if (!Field.class.isAssignableFrom(memberField.getType())) {
                 // Process next field
@@ -1001,4 +1000,27 @@ public class FieldGroup implements Serializable {
         field.setCaption(caption);
         return field;
     }
-}
\ No newline at end of file
+
+    /**
+     * Returns an array containing Field objects reflecting all the fields of
+     * the class or interface represented by this Class object. The elements in
+     * the array returned are sorted in declare order from sub class to super
+     * class.
+     * 
+     * @param searchClass
+     * @return
+     */
+    protected static List<java.lang.reflect.Field> getFieldsInDeclareOrder(
+            Class searchClass) {
+        ArrayList<java.lang.reflect.Field> memberFieldInOrder = new ArrayList<java.lang.reflect.Field>();
+
+        while (searchClass != null) {
+            for (java.lang.reflect.Field memberField : searchClass
+                    .getDeclaredFields()) {
+                memberFieldInOrder.add(memberField);
+            }
+            searchClass = searchClass.getSuperclass();
+        }
+        return memberFieldInOrder;
+    }
+}
diff --git a/server/tests/src/com/vaadin/data/util/ReflectToolsGetSuperField.java b/server/tests/src/com/vaadin/data/util/ReflectToolsGetSuperField.java
new file mode 100644 (file)
index 0000000..efba608
--- /dev/null
@@ -0,0 +1,34 @@
+package com.vaadin.data.util;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.vaadin.data.fieldgroup.FieldGroup;
+import com.vaadin.data.fieldgroup.PropertyId;
+import com.vaadin.ui.TextField;
+
+public class ReflectToolsGetSuperField {
+
+    @Test
+    public void getFieldFromSuperClass() {
+        class MyClass {
+            @PropertyId("testProperty")
+            TextField test = new TextField("This is a test");
+        }
+        class MySubClass extends MyClass {
+            // no fields here
+        }
+        
+        PropertysetItem item = new PropertysetItem();
+        item.addItemProperty("testProperty", new ObjectProperty<String>("Value of testProperty"));
+        
+        MySubClass form = new MySubClass();
+        
+        FieldGroup binder = new FieldGroup(item);
+        binder.bindMemberFields(form);
+        
+        assertTrue("Value of testProperty".equals(form.test.getValue()));
+    }
+
+}