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/02/20 10:16:36 UTC

svn commit: r629391 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/ src/main/java/org/apache/commons/configuration2/beanutils/ xdocs/

Author: ebourg
Date: Wed Feb 20 01:16:32 2008
New Revision: 629391

URL: http://svn.apache.org/viewvc?rev=629391&view=rev
Log:
BeanUtils is no longer required to use DefaultConfigurationBuilder

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

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java?rev=629391&r1=629390&r2=629391&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java Wed Feb 20 01:16:32 2008
@@ -90,8 +90,7 @@
      * @since 1.5
      */
     @SuppressWarnings("unchecked")
-    static <T> T to(Class<T> cls, Object value, Object[] params)
-            throws ConversionException
+    public static <T> T to(Class<T> cls, Object value, Object... params) throws ConversionException
     {
         Object result = null;
 
@@ -175,7 +174,7 @@
                     + cls.getName() + " object");
         }
 
-        return cls.cast(result);
+        return (T) result;
     }
 
     /**

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=629391&r1=629390&r2=629391&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 Feb 20 01:16:32 2008
@@ -14,18 +14,20 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.commons.configuration2.beanutils;
 
-import java.lang.reflect.InvocationTargetException;
+import java.beans.BeanInfo;
+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;
 
-import org.apache.commons.beanutils.BeanUtils;
-import org.apache.commons.beanutils.PropertyUtils;
 import org.apache.commons.configuration2.ConfigurationRuntimeException;
+import org.apache.commons.configuration2.PropertyConverter;
 import org.apache.commons.lang.ClassUtils;
 
 /**
@@ -177,7 +179,7 @@
     }
 
     /**
-     * Sets a property on the given bean using Common Beanutils.
+     * Sets a property on the given bean.
      *
      * @param bean the bean
      * @param propName the name of the property
@@ -185,25 +187,41 @@
      * @throws ConfigurationRuntimeException if the property is not writeable or
      * an error occurred
      */
-    private static void initProperty(Object bean, String propName, Object value)
-            throws ConfigurationRuntimeException
+    private static void initProperty(Object bean, String propName, Object value) throws ConfigurationRuntimeException
     {
-        if (!PropertyUtils.isWriteable(bean, propName))
-        {
-            throw new ConfigurationRuntimeException("Property " + propName + " cannot be set!");
-        }
-
         try
         {
-            BeanUtils.setProperty(bean, propName, value);
+            // 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;
+                }
+            }
+
+            // check if the property is writeable
+            if (descriptor == null || descriptor.getWriteMethod() == null)
+            {
+                throw new ConfigurationRuntimeException("Property " + propName + " cannot be set!");
+            }
+
+            // set the property
+            Class type = descriptor.getPropertyType();
+            Object convertedValue = type.isAssignableFrom(value.getClass()) ? value : PropertyConverter.to(type, value);
+            descriptor.getWriteMethod().invoke(bean, convertedValue);
         }
-        catch (IllegalAccessException iaex)
+        catch (ConfigurationRuntimeException e)
         {
-            throw new ConfigurationRuntimeException(iaex);
+            throw e;
         }
-        catch (InvocationTargetException itex)
+        catch (Exception e)
         {
-            throw new ConfigurationRuntimeException(itex);
+            throw new ConfigurationRuntimeException("Unable to set the property " + propName + " to '" + value + "'", e);
         }
     }
 

Modified: commons/proper/configuration/branches/configuration2_experimental/xdocs/dependencies.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/dependencies.xml?rev=629391&r1=629390&r2=629391&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/xdocs/dependencies.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/xdocs/dependencies.xml Wed Feb 20 01:16:32 2008
@@ -59,10 +59,6 @@
                         </td>
                     </tr>
                     <tr>
-                        <td>DefaultConfigurationBuilder</td>
-                        <td>commons-beanutils</td>
-                    </tr>
-                    <tr>
                         <td>ConfigurationDynaBean</td>
                         <td>commons-beanutils</td>
                     </tr>



Re: svn commit: r629391 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/ src/main/java/org/apache/commons/configuration2/beanutils/ xdocs/

Posted by Emmanuel Bourg <eb...@apache.org>.
sebb a écrit :

> The method signature was also changed to public - was this intended?

Yes this was necessary to access the method from the beanutils package.

Emmanuel Bourg

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r629391 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/ src/main/java/org/apache/commons/configuration2/beanutils/ xdocs/

Posted by sebb <se...@gmail.com>.
On 20/02/2008, Emmanuel Bourg <eb...@apache.org> wrote:
> sebb a écrit :
>
> > The removal of BeanUtils was not the only change.
> >
> > Best to use separate commits for independent changes; if not, please
> > use a log message that reflects all the actual updates.
>
> Actually the change to PropertyConverter was related to this removal to
> avoid a ClassCastException, this exception didn't occur anywhere else
> but from the code that replaced beanutils.
>

It may have been related, but it was certainly not obvious from the
log message or from the other diffs.

The method signature was also changed to public - was this intended?

> Emmanuel Bourg
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r629391 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/ src/main/java/org/apache/commons/configuration2/beanutils/ xdocs/

Posted by Emmanuel Bourg <eb...@apache.org>.
sebb a écrit :

> The removal of BeanUtils was not the only change.
> 
> Best to use separate commits for independent changes; if not, please
> use a log message that reflects all the actual updates.

Actually the change to PropertyConverter was related to this removal to 
avoid a ClassCastException, this exception didn't occur anywhere else 
but from the code that replaced beanutils.

Emmanuel Bourg

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r629391 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/ src/main/java/org/apache/commons/configuration2/beanutils/ xdocs/

Posted by sebb <se...@gmail.com>.
On 20/02/2008, ebourg@apache.org <eb...@apache.org> wrote:
> Author: ebourg
> Date: Wed Feb 20 01:16:32 2008
> New Revision: 629391
>
> URL: http://svn.apache.org/viewvc?rev=629391&view=rev
> Log:
> BeanUtils is no longer required to use DefaultConfigurationBuilder

The removal of BeanUtils was not the only change.

Best to use separate commits for independent changes; if not, please
use a log message that reflects all the actual updates.

>
> Modified:
>     commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java
>     commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/BeanHelper.java
>     commons/proper/configuration/branches/configuration2_experimental/xdocs/dependencies.xml
>
> Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java
> URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java?rev=629391&r1=629390&r2=629391&view=diff
> ==============================================================================
> --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java (original)
> +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertyConverter.java Wed Feb 20 01:16:32 2008
> @@ -90,8 +90,7 @@
>       * @since 1.5
>       */
>      @SuppressWarnings("unchecked")
> -    static <T> T to(Class<T> cls, Object value, Object[] params)
> -            throws ConversionException
> +    public static <T> T to(Class<T> cls, Object value, Object... params) throws ConversionException
>      {
>          Object result = null;
>
> @@ -175,7 +174,7 @@
>                      + cls.getName() + " object");
>          }
>
> -        return cls.cast(result);
> +        return (T) result;
>      }
>
>      /**
>
> 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=629391&r1=629390&r2=629391&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 Feb 20 01:16:32 2008
> @@ -14,18 +14,20 @@
>   * See the License for the specific language governing permissions and
>   * limitations under the License.
>   */
> +
>  package org.apache.commons.configuration2.beanutils;
>
> -import java.lang.reflect.InvocationTargetException;
> +import java.beans.BeanInfo;
> +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;
>
> -import org.apache.commons.beanutils.BeanUtils;
> -import org.apache.commons.beanutils.PropertyUtils;
>  import org.apache.commons.configuration2.ConfigurationRuntimeException;
> +import org.apache.commons.configuration2.PropertyConverter;
>  import org.apache.commons.lang.ClassUtils;
>
>  /**
> @@ -177,7 +179,7 @@
>      }
>
>      /**
> -     * Sets a property on the given bean using Common Beanutils.
> +     * Sets a property on the given bean.
>       *
>       * @param bean the bean
>       * @param propName the name of the property
> @@ -185,25 +187,41 @@
>       * @throws ConfigurationRuntimeException if the property is not writeable or
>       * an error occurred
>       */
> -    private static void initProperty(Object bean, String propName, Object value)
> -            throws ConfigurationRuntimeException
> +    private static void initProperty(Object bean, String propName, Object value) throws ConfigurationRuntimeException
>      {
> -        if (!PropertyUtils.isWriteable(bean, propName))
> -        {
> -            throw new ConfigurationRuntimeException("Property " + propName + " cannot be set!");
> -        }
> -
>          try
>          {
> -            BeanUtils.setProperty(bean, propName, value);
> +            // 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;
> +                }
> +            }
> +
> +            // check if the property is writeable
> +            if (descriptor == null || descriptor.getWriteMethod() == null)
> +            {
> +                throw new ConfigurationRuntimeException("Property " + propName + " cannot be set!");
> +            }
> +
> +            // set the property
> +            Class type = descriptor.getPropertyType();
> +            Object convertedValue = type.isAssignableFrom(value.getClass()) ? value : PropertyConverter.to(type, value);
> +            descriptor.getWriteMethod().invoke(bean, convertedValue);
>          }
> -        catch (IllegalAccessException iaex)
> +        catch (ConfigurationRuntimeException e)
>          {
> -            throw new ConfigurationRuntimeException(iaex);
> +            throw e;
>          }

The above catch clause is now redundant - why not remove it entirely?

> -        catch (InvocationTargetException itex)
> +        catch (Exception e)
>          {
> -            throw new ConfigurationRuntimeException(itex);
> +            throw new ConfigurationRuntimeException("Unable to set the property " + propName + " to '" + value + "'", e);
>          }
>      }
>
>
> Modified: commons/proper/configuration/branches/configuration2_experimental/xdocs/dependencies.xml
> URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/dependencies.xml?rev=629391&r1=629390&r2=629391&view=diff
> ==============================================================================
> --- commons/proper/configuration/branches/configuration2_experimental/xdocs/dependencies.xml (original)
> +++ commons/proper/configuration/branches/configuration2_experimental/xdocs/dependencies.xml Wed Feb 20 01:16:32 2008
> @@ -59,10 +59,6 @@
>                          </td>
>                      </tr>
>                      <tr>
> -                        <td>DefaultConfigurationBuilder</td>
> -                        <td>commons-beanutils</td>
> -                    </tr>
> -                    <tr>
>                          <td>ConfigurationDynaBean</td>
>                          <td>commons-beanutils</td>
>                      </tr>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org