commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1553023 - /commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
Date Sun, 22 Dec 2013 20:35:14 GMT
Author: oheger
Date: Sun Dec 22 20:35:14 2013
New Revision: 1553023

URL: http://svn.apache.org/r1553023
Log:
BeanHelper now manages a BeanUtilsBean instance initialized in a special way.

This instance uses an additional BeanIntrospector with support for fluent
interfaces as used by the concrete builder parameters implementations. The
methods accessing bean properties are now delegating to this BeanUtilsBean
instance.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java?rev=1553023&r1=1553022&r2=1553023&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
Sun Dec 22 20:35:14 2013
@@ -27,8 +27,10 @@ import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 
-import org.apache.commons.beanutils.BeanUtils;
-import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.beanutils.BeanUtilsBean;
+import org.apache.commons.beanutils.ConvertUtilsBean;
+import org.apache.commons.beanutils.FluentPropertyBeanIntrospector;
+import org.apache.commons.beanutils.PropertyUtilsBean;
 import org.apache.commons.configuration.ConfigurationRuntimeException;
 import org.apache.commons.lang3.ClassUtils;
 
@@ -75,6 +77,14 @@ public final class BeanHelper
      */
     public static final BeanHelper INSTANCE = new BeanHelper();
 
+    /**
+     * A special instance of {@code BeanUtilsBean} which is used for all
+     * property set and copy operations. This instance was initialized with
+     * {@code BeanIntrospector} objects which support fluent interfaces. This is
+     * required for handling builder parameter objects correctly.
+     */
+    private static final BeanUtilsBean beanUtilsBean = initBeanUtilsBean();
+
     /** Stores a map with the registered bean factories. */
     private final Map<String, BeanFactory> BEAN_FACTORIES = Collections
             .synchronizedMap(new HashMap<String, BeanFactory>());
@@ -272,7 +282,9 @@ public final class BeanHelper
     {
         try
         {
-            PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(bean, propName);
+            PropertyDescriptor desc =
+                    beanUtilsBean.getPropertyUtils().getPropertyDescriptor(
+                            bean, propName);
             if (desc == null)
             {
                 return null;
@@ -296,7 +308,7 @@ public final class BeanHelper
      */
     private static void initProperty(Object bean, String propName, Object value)
     {
-        if (!PropertyUtils.isWriteable(bean, propName))
+        if (!isPropertyWriteable(bean, propName))
         {
             throw new ConfigurationRuntimeException("Property " + propName
                     + " cannot be set on " + bean.getClass().getName());
@@ -304,7 +316,7 @@ public final class BeanHelper
 
         try
         {
-            BeanUtils.setProperty(bean, propName, value);
+            beanUtilsBean.setProperty(bean, propName, value);
         }
         catch (IllegalAccessException iaex)
         {
@@ -360,7 +372,7 @@ public final class BeanHelper
      */
     public static void setProperty(Object bean, String propName, Object value)
     {
-        if (PropertyUtils.isWriteable(bean, propName))
+        if (isPropertyWriteable(bean, propName))
         {
             initProperty(bean, propName, value);
         }
@@ -452,6 +464,20 @@ public final class BeanHelper
     }
 
     /**
+     * Checks whether the specified property of the given bean instance supports
+     * write access.
+     *
+     * @param bean the bean instance
+     * @param propName the name of the property in question
+     * @return <b>true</b> if this property can be written, <b>false</b>
+     *         otherwise
+     */
+    private static boolean isPropertyWriteable(Object bean, String propName)
+    {
+        return beanUtilsBean.getPropertyUtils().isWriteable(bean, propName);
+    }
+
+    /**
      * Determines the class of the bean to be created. If the bean declaration
      * contains a class name, this class is used. Otherwise it is checked
      * whether a default class is provided. If this is not the case, the
@@ -548,6 +574,21 @@ public final class BeanHelper
     }
 
     /**
+     * Initializes the shared {@code BeanUtilsBean} instance. This method sets
+     * up custom bean introspection in a way that fluent parameter interfaces
+     * are supported.
+     *
+     * @return the {@code BeanUtilsBean} instance to be used for all property
+     *         set operations
+     */
+    private static BeanUtilsBean initBeanUtilsBean()
+    {
+        PropertyUtilsBean propUtilsBean = new PropertyUtilsBean();
+        propUtilsBean.addBeanIntrospector(new FluentPropertyBeanIntrospector());
+        return new BeanUtilsBean(new ConvertUtilsBean(), propUtilsBean);
+    }
+
+    /**
      * An implementation of the {@code BeanCreationContext} interface used by
      * {@code BeanHelper} to communicate with a {@code BeanFactory}. This class
      * contains all information required for the creation of a bean. The methods



Mime
View raw message