Commits

plightbo  committed 0aacf86

handling of null map also done

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@160e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits dcb94d8

Comments (0)

Files changed (5)

File src/java/com/opensymphony/xwork/util/InstantiatingNullHandler.java

 
     private Object createObject(Map context, Class clazz, Object target, String property) throws InstantiationException, IllegalAccessException {
         if (clazz == Collection.class || clazz == List.class) {
-            return createNewList(target, property);
-        } else if (clazz == Set.class) {
-
+            Class collectionType = getCollectionType(target.getClass(), property);
+            if (collectionType == null) {
+                return null;
+            }
+            return new XWorkList(collectionType);
         } else if (clazz == Map.class) {
-
+            Class collectionType = getCollectionType(target.getClass(), property);
+            if (collectionType == null) {
+                return null;
+            }            
+            return new XWorkMap(collectionType);
         }
 
         return clazz.newInstance();
     }
 
-    private XWorkList createNewList(Object target, String property) {
-        Class clazz = getCollectionType(target.getClass(), property);
-        return new XWorkList(clazz);
-    }
 
     private Map buildConverterMapping(Class clazz) throws Exception {
         Map mapping = new HashMap();

File src/java/com/opensymphony/xwork/util/XWorkMap.java

+package com.opensymphony.xwork.util;
+
+import java.util.HashMap;
+
+/**
+ * User: plightbo
+ * Date: Jan 13, 2004
+ * Time: 9:21:03 PM
+ */
+public class XWorkMap extends HashMap {
+    private Class clazz;
+
+    public XWorkMap(Class clazz) {
+        this.clazz = clazz;
+    }
+
+    public Object get(Object key) {
+        Object o = super.get(key);
+        if (o == null) {
+            try {
+                o = clazz.newInstance();
+                this.put(key, o);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+        
+        return o;
+    }
+
+}

File src/test/com/opensymphony/xwork/util/Foo-conversion.properties

 bar=com.opensymphony.xwork.util.FooBarConverter
 Collection_cats=com.opensymphony.xwork.util.Cat
+Collection_catMap=com.opensymphony.xwork.util.Cat

File src/test/com/opensymphony/xwork/util/Foo.java

 
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 
 
 /**
     int number;
     long aLong;
     List cats;
+    Map catMap;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
     public void setCats(List cats) {
         this.cats = cats;
     }
+
+    public Map getCatMap() {
+        return catMap;
+    }
+
+    public void setCatMap(Map catMap) {
+        this.catMap = catMap;
+    }
 }

File src/test/com/opensymphony/xwork/util/OgnlValueStackTest.java

         assertEquals("Deep null cat", ((Cat) ((Cat) foo.getCats().get(0)).getFoo().getCats().get(1)).name);
     }
 
+    public void testSetNullMap() {
+        Foo foo = new Foo();
+        OgnlValueStack vs = new OgnlValueStack();
+        vs.getContext().put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
+        vs.push(foo);
+
+        vs.setValue("catMap['One'].name", "Cat One");
+        vs.setValue("catMap['Two'].name", "Cat Two");
+
+        assertNotNull(foo.getCatMap());
+        assertEquals(2, foo.getCatMap().size());
+        assertEquals("Cat One", ((Cat) foo.getCatMap().get("One")).getName());
+        assertEquals("Cat Two", ((Cat) foo.getCatMap().get("Two")).getName());
+
+        vs.setValue("catMap['One'].foo.catMap['Two'].name", "Deep null cat");
+        assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo());
+        assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo().getCatMap());
+        assertNotNull(((Cat) ((Cat) foo.getCatMap().get("One")).getFoo().getCatMap().get("Two")));
+        assertEquals("Deep null cat", ((Cat) ((Cat) foo.getCatMap().get("One")).getFoo().getCatMap().get("Two")).name);
+    }
+
     public void testSettingDogGender() {
         OgnlValueStack vs = new OgnlValueStack();