You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/08/22 22:00:19 UTC
svn commit: r1516575 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.java
test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java
Author: oheger
Date: Thu Aug 22 20:00:19 2013
New Revision: 1516575
URL: http://svn.apache.org/r1516575
Log:
Removed direct dependency to PropertyConverter.
Data type conversions are now performed by a ConversionHandler object. The
ConversionHandler instance to be used can be passed to the constructor of
DefaultBeanFactory. Alternatively, a default instance is used.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.java?rev=1516575&r1=1516574&r2=1516575&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.java Thu Aug 22 20:00:19 2013
@@ -20,7 +20,8 @@ import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Collections;
-import org.apache.commons.configuration.convert.PropertyConverter;
+import org.apache.commons.configuration.convert.ConversionHandler;
+import org.apache.commons.configuration.convert.DefaultConversionHandler;
/**
* <p>
@@ -28,20 +29,23 @@ import org.apache.commons.configuration.
* </p>
* <p>
* This class creates beans of arbitrary types using reflection. Each time the
- * {@code createBean()} method is invoked, a new bean instance is
- * created. A default bean class is not supported.
+ * {@code createBean()} method is invoked, a new bean instance is created. A
+ * default bean class is not supported.
+ * </p>
+ * <p>
+ * For data type conversions (which may be needed before invoking methods
+ * through reflection to ensure that the current parameters match their declared
+ * types) a {@link ConversionHandler} object is used. An instance of this class
+ * can be passed to the constructor. Alternatively, a default
+ * {@code ConversionHandler} instance is used.
* </p>
* <p>
* An instance of this factory class will be set as the default bean factory for
- * the {@link BeanHelper} class. This means that if not bean
- * factory is specified in a {@link BeanDeclaration}, this
- * default instance will be used.
+ * the {@link BeanHelper} class. This means that if not bean factory is
+ * specified in a {@link BeanDeclaration}, this default instance will be used.
* </p>
*
* @since 1.3
- * @author <a
- * href="http://commons.apache.org/configuration/team-list.html">Commons
- * Configuration team</a>
* @version $Id$
*/
public class DefaultBeanFactory implements BeanFactory
@@ -49,6 +53,44 @@ public class DefaultBeanFactory implemen
/** Stores the default instance of this class. */
public static final DefaultBeanFactory INSTANCE = new DefaultBeanFactory();
+ /** The conversion handler used by this instance. */
+ private final ConversionHandler conversionHandler;
+
+ /**
+ * Creates a new instance of {@code DefaultBeanFactory} using a default
+ * {@code ConversionHandler}.
+ */
+ public DefaultBeanFactory()
+ {
+ this(null);
+ }
+
+ /**
+ * Creates a new instance of {@code DefaultBeanFactory} using the specified
+ * {@code ConversionHandler} for data type conversions.
+ *
+ * @param convHandler the {@code ConversionHandler}; can be <b>null</b>,
+ * then a default handler is used
+ * @since 2.0
+ */
+ public DefaultBeanFactory(ConversionHandler convHandler)
+ {
+ conversionHandler =
+ (convHandler != null) ? convHandler
+ : DefaultConversionHandler.INSTANCE;
+ }
+
+ /**
+ * Returns the {@code ConversionHandler} used by this object.
+ *
+ * @return the {@code ConversionHandler}
+ * @since 2.0
+ */
+ public ConversionHandler getConversionHandler()
+ {
+ return conversionHandler;
+ }
+
/**
* Creates a new bean instance. This implementation delegates to the
* protected methods {@code createBeanInstance()} and
@@ -125,7 +167,7 @@ public class DefaultBeanFactory implemen
* @param data the current bean declaration
* @return an array with constructor arguments
*/
- private static Object[] fetchConstructorArgs(Constructor<?> ctor,
+ private Object[] fetchConstructorArgs(Constructor<?> ctor,
BeanDeclaration data)
{
Class<?>[] types = ctor.getParameterTypes();
@@ -139,7 +181,7 @@ public class DefaultBeanFactory implemen
Object val =
arg.isNestedBeanDeclaration() ? BeanHelper.createBean(arg
.getBeanDeclaration()) : arg.getValue();
- args[idx] = PropertyConverter.to(types[idx], val, (Object[]) null);
+ args[idx] = getConversionHandler().to(val, types[idx], null);
idx++;
}
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java?rev=1516575&r1=1516574&r2=1516575&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java Thu Aug 22 20:00:19 2013
@@ -19,6 +19,7 @@ package org.apache.commons.configuration
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
@@ -28,6 +29,9 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.commons.configuration.convert.ConversionHandler;
+import org.apache.commons.configuration.convert.DefaultConversionHandler;
+import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
@@ -61,6 +65,32 @@ public class TestDefaultBeanFactory
}
/**
+ * Tests whether a correct default conversion handler is set.
+ */
+ @Test
+ public void testDefaultConversionHandler()
+ {
+ assertSame("Wrong default conversion handler",
+ DefaultConversionHandler.INSTANCE,
+ factory.getConversionHandler());
+ }
+
+ /**
+ * Tests whether a custom conversion handler can be passed to the
+ * constructor.
+ */
+ @Test
+ public void testInitWithConversionHandler()
+ {
+ ConversionHandler handler =
+ EasyMock.createMock(ConversionHandler.class);
+ EasyMock.replay(handler);
+ factory = new DefaultBeanFactory(handler);
+ assertSame("Wrong conversion handler", handler,
+ factory.getConversionHandler());
+ }
+
+ /**
* Tests creating a bean.
*/
@Test