You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2008/04/30 13:31:15 UTC

svn commit: r652338 - /commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java

Author: ebourg
Date: Wed Apr 30 04:31:15 2008
New Revision: 652338

URL: http://svn.apache.org/viewvc?rev=652338&view=rev
Log:
Minor Java 5 syntax change in BeanHelper
Added a getPropertyDescriptor() method to make the code more readable

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java?rev=652338&r1=652337&r2=652338&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java Wed Apr 30 04:31:15 2008
@@ -18,11 +18,11 @@
 package org.apache.commons.configuration2.beanutils;
 
 import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
 import java.beans.Introspector;
 import java.beans.PropertyDescriptor;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 
@@ -161,22 +161,20 @@
      */
     public static void initBean(Object bean, BeanDeclaration data) throws ConfigurationRuntimeException
     {
-        Map properties = data.getBeanProperties();
+        Map<String, Object> properties = data.getBeanProperties();
         if (properties != null)
         {
-            for (Iterator it = properties.keySet().iterator(); it.hasNext();)
+            for (String propName : properties.keySet())
             {
-                String propName = (String) it.next();
                 initProperty(bean, propName, properties.get(propName));
             }
         }
 
-        Map nestedBeans = data.getNestedBeanDeclarations();
+        Map<String, BeanDeclaration> nestedBeans = data.getNestedBeanDeclarations();
         if (nestedBeans != null)
         {
-            for (Iterator it = nestedBeans.keySet().iterator(); it.hasNext();)
+            for (String propName : nestedBeans.keySet())
             {
-                String propName = (String) it.next();
                 initProperty(bean, propName, createBean((BeanDeclaration) nestedBeans.get(propName), null));
             }
         }
@@ -196,17 +194,7 @@
         try
         {
             // find the descriptor for the property requested
-            BeanInfo info = Introspector.getBeanInfo(bean.getClass());
-            PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
-            PropertyDescriptor descriptor =  null;
-            for (PropertyDescriptor d : descriptors)
-            {
-                if (d.getName().equals(propName))
-                {
-                    descriptor = d;
-                    break;
-                }
-            }
+            PropertyDescriptor descriptor =  getPropertyDescriptor(bean.getClass(), propName);
 
             // check if the property is writeable
             if (descriptor == null || descriptor.getWriteMethod() == null)
@@ -230,6 +218,32 @@
     }
 
     /**
+     * Returns the PropertyDescriptor of the class for the specified property name.
+     *
+     * @param cls          the class to be introspected
+     * @param propertyName the name of the property
+     * @return the descriptor, or null if no property matches the name specified
+     * @throws IntrospectionException
+     */
+    private static PropertyDescriptor getPropertyDescriptor(Class cls, String propertyName) throws IntrospectionException
+    {
+        // find the descriptor for the property requested
+        BeanInfo info = Introspector.getBeanInfo(cls);
+        PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
+        PropertyDescriptor descriptor = null;
+        for (PropertyDescriptor d : descriptors)
+        {
+            if (d.getName().equals(propertyName))
+            {
+                descriptor = d;
+                break;
+            }
+        }
+
+        return descriptor;
+    }
+
+    /**
      * The main method for creating and initializing beans from a configuration.
      * This method will return an initialized instance of the bean class
      * specified in the passed in bean declaration. If this declaration does not