OSDN Git Service

* BeanUtil interface is changed
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Fri, 4 Apr 2008 12:37:27 +0000 (12:37 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Fri, 4 Apr 2008 12:37:27 +0000 (12:37 +0000)
 * version 0.7.0
 * BeanUtilTest has passed

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@2069 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd

pom.xml
src/main/java/org/xerial/util/bean/BeanUtil.java
src/main/java/org/xerial/util/bean/impl/ArraySetter.java
src/main/java/org/xerial/util/bean/impl/BeanBindingProcess.java
src/main/java/org/xerial/util/bean/impl/CollectionAdder.java
src/main/java/org/xerial/util/bean/impl/CollectionSetter.java
src/main/java/org/xerial/util/bean/impl/MapPutter.java
src/main/java/org/xerial/util/bean/impl/MapSetter.java
src/main/java/org/xerial/util/bean/impl/Setter.java
src/test/java/org/xerial/json/JSONWriterTest.java
src/test/java/org/xerial/util/bean/BeanUtilTest.java

diff --git a/pom.xml b/pom.xml
index 89b44eb..0cc3097 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -8,7 +8,7 @@
   <groupId>org.xerial</groupId>
   <artifactId>xerial-core</artifactId>
   <name>Xerial Core</name>
-  <version>0.6.8</version>
+  <version>0.7.0-SNAPSHOT</version>
   <description>Xerial Core: standard utilities for XerialJ project</description>
   <build>
     <resources>
index f886f02..108ae3c 100644 (file)
@@ -873,7 +873,7 @@ public class BeanUtil
      * @throws InvalidBeanException\r
      *             when a bean class has invalid structure\r
      */\r
-    public static void populateBean(Object bean, String jsonData) throws BeanException\r
+    public static void populateBeanWithJSON(Object bean, String jsonData) throws BeanException\r
     {\r
         // parse the input JSON data\r
         try\r
@@ -953,7 +953,7 @@ public class BeanUtil
                 {\r
                     throw new BeanException(BeanErrorCode.IllegalAccess, e);\r
                 }\r
-                populateBean(array[i], jsonArray.get(i));\r
+                populateBeanWithJSON(array[i], jsonArray.get(i));\r
             }\r
         }\r
         else\r
@@ -962,7 +962,7 @@ public class BeanUtil
 \r
     }\r
 \r
-    protected static void populateBean(Object bean, Object jsonValue) throws BeanException\r
+    protected static void populateBeanWithJSON(Object bean, Object jsonValue) throws BeanException\r
     {\r
         if (jsonValue.getClass() == JSONObject.class)\r
         {\r
@@ -995,7 +995,7 @@ public class BeanUtil
         }\r
     }\r
 \r
-    public static Object createBean(Class beanType, Reader jsonReader) throws IOException, BeanException\r
+    public static Object createBeanFromJSON(Class beanType, Reader jsonReader) throws IOException, BeanException\r
     {\r
         BufferedReader reader = new BufferedReader(jsonReader);\r
         StringWriter buffer = new StringWriter();\r
@@ -1005,17 +1005,17 @@ public class BeanUtil
             buffer.append(line);\r
             buffer.append(StringUtil.newline());\r
         }\r
-        return createBean(beanType, buffer.toString());\r
+        return createBeanFromJSON(beanType, buffer.toString());\r
     }\r
 \r
-    public static Object createBean(Class beanType, String json) throws BeanException\r
+    public static Object createBeanFromJSON(Class beanType, String json) throws BeanException\r
     {\r
         Object bean = createInstance(beanType);\r
-        populateBean(bean, json);\r
+        populateBeanWithJSON(bean, json);\r
         return bean;\r
     }\r
 \r
-    public static Object createBean(Class valueType, Object jsonValue) throws BeanException\r
+    public static Object createBeanFromJSON(Class valueType, Object jsonValue) throws BeanException\r
     {\r
         if (jsonValue == null)\r
             return null;\r
index 694de54..aa45874 100644 (file)
@@ -47,7 +47,7 @@ public class ArraySetter extends BeanBinderBase {
 
         Object[] tmpArray = (Object[]) Array.newInstance(componentType, arrayContent.size());
         for (int i = 0; i < arrayContent.size(); i++) {
-            tmpArray[i] = BeanUtil.createBean(componentType, arrayContent.get(i));
+            tmpArray[i] = BeanUtil.createBeanFromJSON(componentType, arrayContent.get(i));
         }
         invokeMethod(bean, new Object[] { tmpArray });
     }
index 9a4b4da..7a8f0d2 100644 (file)
@@ -138,6 +138,7 @@ public class BeanBindingProcess implements TreeVisitor
      */
 
     private final TreeMap<Integer, Object> contextBeanOfEachLevel = new TreeMap<Integer, Object>();
+    private final TreeMap<Integer, Map> mapAssociatedWitBean = new TreeMap<Integer, Map>();
 
     private int currentLevel = 0;
     private BindRuleGenerator bindRuleGenerator = new BindRuleGeneratorImpl();
@@ -208,7 +209,7 @@ public class BeanBindingProcess implements TreeVisitor
     public void visitNode(String nodeName, TreeWalker walker) throws XerialException
     {
         int nodeLevel = currentLevel++;
-        _logger.trace("visit[" + nodeLevel + "] " + nodeName);
+        //_logger.trace("visit[" + nodeLevel + "] " + nodeName);
 
         // prepare the context bean for this depth
         Object bean = getContextBean(nodeLevel);
@@ -301,7 +302,7 @@ public class BeanBindingProcess implements TreeVisitor
     public void leaveNode(String nodeName, String nodeValue, TreeWalker walker) throws XerialException
     {
         int nodeLevel = --currentLevel;
-        _logger.trace("leave[" + nodeLevel + "] " + nodeName + " value = " + nodeValue);
+        //_logger.trace("leave[" + nodeLevel + "] " + nodeName + " value = " + nodeValue);
 
         Object parentBean = getContextBean(nodeLevel - 1);
         if (parentBean == null)
@@ -355,6 +356,7 @@ public class BeanBindingProcess implements TreeVisitor
                 {
                     KeyValuePair keyValuePair = KeyValuePair.class.cast(valueBean);
                     bindMapElement(parentBean, MapPutter.class.cast(updator), keyValuePair);
+                    mapAssociatedWitBean.remove(parentBean.hashCode());
                 }
                 catch (ClassCastException e)
                 {
@@ -410,11 +412,30 @@ public class BeanBindingProcess implements TreeVisitor
         }
     }
 
+    @SuppressWarnings("unchecked")
     private void bindValue(Object bean, BeanUpdator updator, Class targetType, Object value) throws BeanException
     {
         try
         {
-            updator.getMethod().invoke(bean, convertType(targetType, value));
+            if (value.getClass() == KeyValuePair.class && TypeInformation.isMap(targetType))
+            {
+                Map map = null;
+                if (mapAssociatedWitBean.containsKey(bean.hashCode()))
+                {
+                    map = mapAssociatedWitBean.get(bean.hashCode());
+                }
+                else
+                {
+                    map = Map.class.cast(BeanUtil.createInstance(targetType));
+                    mapAssociatedWitBean.put(bean.hashCode(), map);
+                }
+                KeyValuePair keyValuePair = KeyValuePair.class.cast(value);
+                map.put(keyValuePair.getKey(), keyValuePair.getValue());
+
+                updator.getMethod().invoke(bean, map);
+            }
+            else
+                updator.getMethod().invoke(bean, convertType(targetType, value));
         }
         catch (IllegalArgumentException e)
         {
@@ -437,17 +458,10 @@ public class BeanBindingProcess implements TreeVisitor
     }
 
     @SuppressWarnings("unchecked")
-    public static Object convertType(Class targetType, Object value) throws BeanException
+    public Object convertType(Class targetType, Object value) throws BeanException
     {
         if (targetType.isAssignableFrom(value.getClass()) || targetType == Object.class)
             return value;
-        else if (value.getClass() == KeyValuePair.class && TypeInformation.isMap(targetType))
-        {
-            Map newMapInstance = Map.class.cast(BeanUtil.createInstance(targetType));
-            KeyValuePair keyValuePair = KeyValuePair.class.cast(value);
-            newMapInstance.put(keyValuePair.getKey(), keyValuePair.getValue());
-            return newMapInstance;
-        }
         else
             return convertToBasicType(targetType, value);
     }
index 9e3e209..22fe852 100644 (file)
@@ -56,7 +56,7 @@ public class CollectionAdder extends BeanBinderBase implements BeanUpdator
 
         for (int i = 0; i < collectionContent.size(); i++)
         {
-            Object value = BeanUtil.createBean(elementType, collectionContent.get(i));
+            Object value = BeanUtil.createBeanFromJSON(elementType, collectionContent.get(i));
             invokeMethod(bean, new Object[] { value });
         }
     }
index 783dfb6..bb7fee5 100644 (file)
@@ -58,7 +58,7 @@ public class CollectionSetter extends BeanBinderBase {
 
         Collection tmpCollection = (Collection) BeanUtil.createInstance(collectionType);
         for (int i = 0; i < collectionContent.size(); i++) {
-            tmpCollection.add(BeanUtil.createBean(elementType, collectionContent.get(i)));
+            tmpCollection.add(BeanUtil.createBeanFromJSON(elementType, collectionContent.get(i)));
         }
         invokeMethod(bean, new Object[] { tmpCollection });
     }
index 47db76a..f25e9cb 100644 (file)
@@ -63,8 +63,8 @@ public class MapPutter extends BeanBinderBase implements BeanUpdator
             JSONArray entry = mapContent.getJSONArray(i);
             if (entry != null)
             {
-                Object key = BeanUtil.createBean(keyType, entry.get(0));
-                Object value = BeanUtil.createBean(valueType, entry.get(1));
+                Object key = BeanUtil.createBeanFromJSON(keyType, entry.get(0));
+                Object value = BeanUtil.createBeanFromJSON(valueType, entry.get(1));
                 invokeMethod(bean, new Object[] { key, value });
             }
         }
index 1ca99fa..992c1d1 100644 (file)
@@ -63,8 +63,8 @@ public class MapSetter extends BeanBinderBase {
         for (int i = 0; i < mapContent.size(); i++) {
             JSONArray entry = mapContent.getJSONArray(i);
             if (entry != null) {
-                Object key = BeanUtil.createBean(keyType, entry.get(0));
-                Object value = BeanUtil.createBean(valueType, entry.get(1));
+                Object key = BeanUtil.createBeanFromJSON(keyType, entry.get(0));
+                Object value = BeanUtil.createBeanFromJSON(valueType, entry.get(1));
                 tmpMap.put(key, value);
             }
         }
index 9f06fd6..9a50e73 100644 (file)
@@ -48,7 +48,7 @@ public class Setter extends BeanBinderBase implements BeanUpdator
         if (json == null)
             return;
 
-        Object tmpValue = BeanUtil.createBean(valueType, json);
+        Object tmpValue = BeanUtil.createBeanFromJSON(valueType, json);
         invokeMethod(bean, new Object[] { tmpValue });
     }
 
index 2c31496..5119d1d 100644 (file)
@@ -58,7 +58,7 @@ public class JSONWriterTest
         String jsonData = writer.toString();
         _logger.debug(jsonData);
         Person p = new Person();
-        BeanUtil.populateBean(p, jsonData);
+        BeanUtil.populateBeanWithJSON(p, jsonData);
 
         assertEquals(1, p.getId());
         assertEquals("Leo", p.getName());
@@ -86,7 +86,7 @@ public class JSONWriterTest
         String jsonData = writer.toString();
         _logger.debug(jsonData);
         Person p = new Person();
-        BeanUtil.populateBean(p, jsonData);
+        BeanUtil.populateBeanWithJSON(p, jsonData);
 
         assertEquals(1, p.getId());
         assertEquals("Leo", p.getName());
@@ -136,7 +136,7 @@ public class JSONWriterTest
         String jsonData = writer.toString();
         _logger.debug(jsonData);
         Person p = new Person();
-        BeanUtil.populateBean(p, jsonData);
+        BeanUtil.populateBeanWithJSON(p, jsonData);
 
         assertEquals(1, p.getId());
         assertEquals("leo leo leo", p.getName());
@@ -159,7 +159,7 @@ public class JSONWriterTest
         String jsonData = writer.toString();
         _logger.debug(jsonData);
         Person p = new Person();
-        BeanUtil.populateBean(p, jsonData);
+        BeanUtil.populateBeanWithJSON(p, jsonData);
 
         assertEquals(1, p.getId());
         assertEquals("leo leo leo", p.getName());
index b89e2d8..c861066 100644 (file)
@@ -187,7 +187,7 @@ public class BeanUtilTest
     {
         Person s = new Person(10, "leo");
         Person s2 = new Person();
-        BeanUtil.populateBean(s2, BeanUtil.toJSON(s));
+        BeanUtil.populateBeanWithJSON(s2, BeanUtil.toJSON(s));
         assertEquals(s.getId(), s2.getId());
         assertEquals(s.getName(), s2.getName());
     }
@@ -197,7 +197,7 @@ public class BeanUtilTest
     {
         Book s1 = new Book("Data on the Web", new String[] { "Abiteboul", "Buneman" });
         Book s2 = new Book();
-        BeanUtil.populateBean(s2, BeanUtil.toJSON(s1));
+        BeanUtil.populateBeanWithJSON(s2, BeanUtil.toJSON(s1));
         assertEquals(s1.getTitle(), s2.getTitle());
         assertEquals(s1.getAuthor(), s2.getAuthor());
     }
@@ -206,7 +206,7 @@ public class BeanUtilTest
     public void doNotUsePrivateSetter() throws JSONException, BeanException
     {
         PrivateGetterSetter p = new PrivateGetterSetter(0.0);
-        BeanUtil.populateBean(p, "{\"value\" : 1.34}");
+        BeanUtil.populateBeanWithJSON(p, "{\"value\" : 1.34}");
         assertEquals(0.0, p.getValue());
     }
 
@@ -226,7 +226,7 @@ public class BeanUtilTest
         String json = BeanUtil.toJSON(c);
 
         CollectionParam c2 = new CollectionParam();
-        BeanUtil.populateBean(c2, json);
+        BeanUtil.populateBeanWithJSON(c2, json);
 
         assertEquals(v.size(), c2.getName().size());
         for (int i = 0; i < c.getName().size(); i++)
@@ -244,7 +244,7 @@ public class BeanUtilTest
         Address addr = new Address(new Person(3, "leo"), "world");
         String json = BeanUtil.toJSON(addr);
         Address addr2 = new Address();
-        BeanUtil.populateBean(addr2, json);
+        BeanUtil.populateBeanWithJSON(addr2, json);
 
         assertNotNull(addr2.getPerson());
         assertNotNull(addr2.getAddress());
@@ -266,7 +266,7 @@ public class BeanUtilTest
         String json = BeanUtil.toJSON(pl);
 
         PersonList pl2 = new PersonList();
-        BeanUtil.populateBean(pl2, json);
+        BeanUtil.populateBeanWithJSON(pl2, json);
 
         assertNotNull(pl2.getPerson());
         assertEquals(pl.getPerson().size(), pl2.getPerson().size());
@@ -302,7 +302,7 @@ public class BeanUtilTest
 
         String json = BeanUtil.toJSON(p);
         PrimitiveAdder p2 = new PrimitiveAdder();
-        BeanUtil.populateBean(p2, json);
+        BeanUtil.populateBeanWithJSON(p2, json);
 
         assertEquals(ans.length, p2.getValueList().size());
         int index = 0;
@@ -326,7 +326,7 @@ public class BeanUtilTest
         String json = BeanUtil.toJSON(n1);
 
         NameTable n2 = new NameTable();
-        BeanUtil.populateBean(n2, json);
+        BeanUtil.populateBeanWithJSON(n2, json);
 
         for (int i = 0; i < data.length; i++)
         {
@@ -350,7 +350,7 @@ public class BeanUtilTest
         String json = BeanUtil.toJSON(m1);
 
         Mate m2 = new Mate();
-        BeanUtil.populateBean(m2, json);
+        BeanUtil.populateBeanWithJSON(m2, json);
         assertEquals(m1.getPair(pair1[0]), m2.getPair(pair1[0]));
         assertEquals(m1.getPair(pair2[0]), m2.getPair(pair2[0]));
     }
@@ -377,7 +377,7 @@ public class BeanUtilTest
         _logger.debug(json);
 
         HogeHoge h2 = new HogeHoge();
-        BeanUtil.populateBean(h2, json);
+        BeanUtil.populateBeanWithJSON(h2, json);
         _logger.debug(BeanUtil.toJSON(h2));
 
         assertEquals(h.getMap(), h2.getMap());
@@ -396,7 +396,7 @@ public class BeanUtilTest
         _logger.debug(json);
 
         UnknownTypeMap u2 = new UnknownTypeMap();
-        BeanUtil.populateBean(u2, json);
+        BeanUtil.populateBeanWithJSON(u2, json);
 
         assertEquals(u1.size(), u2.size());
         assertEquals(u1.getMapName(), u2.getMapName());
@@ -417,7 +417,7 @@ public class BeanUtilTest
         _logger.debug(json);
 
         ComplexMap m2 = new ComplexMap();
-        BeanUtil.populateBean(m2, json);
+        BeanUtil.populateBeanWithJSON(m2, json);
 
         String json2 = BeanUtil.toJSON(m2);
         _logger.debug(json2);
@@ -448,7 +448,7 @@ public class BeanUtilTest
         {
             // NonConstructableClass c = new NonConstructableClass();
             ArrayList<NonConstructableClass> a = new ArrayList<NonConstructableClass>();
-            BeanUtil.populateBean(a, "{\"-c\":[{\"name\":\"leo\"}]}");
+            BeanUtil.populateBeanWithJSON(a, "{\"-c\":[{\"name\":\"leo\"}]}");
         }
         catch (BeanException e)
         {
@@ -464,7 +464,7 @@ public class BeanUtilTest
         try
         {
             Map<Integer, NonConstructableClass> m = new TreeMap<Integer, NonConstructableClass>();
-            BeanUtil.populateBean(m, "{\"-m\":[[1, {\"name\":\"leo\"}]]}");
+            BeanUtil.populateBeanWithJSON(m, "{\"-m\":[[1, {\"name\":\"leo\"}]]}");
         }
         catch (BeanException e)
         {
@@ -480,7 +480,7 @@ public class BeanUtilTest
         try
         {
             Map<NonConstructableClass, String> m = new TreeMap<NonConstructableClass, String>();
-            BeanUtil.populateBean(m, "{\"-m\":[[{\"name\":\"leo\"},\"value\"]]}");
+            BeanUtil.populateBeanWithJSON(m, "{\"-m\":[[{\"name\":\"leo\"},\"value\"]]}");
         }
         catch (BeanException e)
         {
@@ -499,7 +499,7 @@ public class BeanUtilTest
         String json = BeanUtil.toJSONObject(pv).toString();
 
         PersonVector pv2 = new PersonVector();
-        BeanUtil.populateBean(pv2, json);
+        BeanUtil.populateBeanWithJSON(pv2, json);
 
         assertEquals(1, pv2.size());
         assertEquals(pv, pv2);
@@ -558,7 +558,7 @@ public class BeanUtilTest
         String json = BeanUtil.toJSON(pl);
 
         PersonList2 pl2 = new PersonList2();
-        BeanUtil.populateBean(pl2, json);
+        BeanUtil.populateBeanWithJSON(pl2, json);
         String json2 = BeanUtil.toJSON(pl2);
         _logger.debug(json2);