Author: oheger
Date: Sat Sep 7 19:42:17 2013
New Revision: 1520792
URL: http://svn.apache.org/r1520792
Log:
BeanFactory now expects a BeanCreationContext as parameter.
All information required by the createBean() method is now contained in a
BeanCreationContext object. This change of the interface caused some
adaptations on the DefaultBeanFactory and BeanHelper classes.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanFactory.java
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
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/TestBeanHelper.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/BeanFactory.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanFactory.java?rev=1520792&r1=1520791&r2=1520792&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanFactory.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanFactory.java
Sat Sep 7 19:42:17 2013
@@ -32,8 +32,8 @@ package org.apache.commons.configuration
* </p>
* <p>
* The interface itself is quite simple. There is a single method for creating
a
- * bean of a given class. All necessary parameters are obtained from an also
- * passed in {@link BeanDeclaration} object. It is also possible
+ * bean of a given class. All necessary parameters are obtained from a
+ * passed in {@link BeanCreationContext} object. It is also possible
* (but optional) for a bean factory to declare the default class of the bean
it
* creates. Then it is not necessary to specify a bean class in the bean
* declaration.
@@ -48,22 +48,19 @@ package org.apache.commons.configuration
public interface BeanFactory
{
/**
- * Returns a bean instance for the given class. The bean will be
initialized
- * from the specified bean declaration object. It is up to a concrete
- * implementation how the bean will be created and initialized.
+ * Returns a bean instance for the given context object. All information
+ * about the bean to be created are contained in the provided
+ * {@code BeanCreationContext} object. This includes a
+ * {@link BeanDeclaration} defining the properties of the bean. It is up to
+ * a concrete implementation how the bean is created and initialized.
*
- * @param beanClass the class for the bean
- * @param data the bean declaration object containing all data about the
- * bean to be created
- * @param param an additional parameter that may be passed by calling code;
- * it is up to a concrete implementation how this parameter is evaluated
+ * @param bcc the context object for the bean to be created
* @return the new bean instance (should not be <b>null</b>)
* @throws Exception if an error occurs (the helper classes for creating
- * beans will catch this unspecific exception and wrap it in a
configuration
- * exception)
+ * beans will catch this generic exception and wrap it in a
+ * configuration exception)
*/
- Object createBean(Class<?> beanClass, BeanDeclaration data, Object param)
- throws Exception;
+ Object createBean(BeanCreationContext bcc) throws Exception;
/**
* Returns the default bean class of this bean factory. If an
implementation
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=1520792&r1=1520791&r2=1520792&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
Sat Sep 7 19:42:17 2013
@@ -391,10 +391,11 @@ public final class BeanHelper
}
BeanFactory factory = fetchBeanFactory(data);
+ BeanCreationContext bcc =
+ createBeanCreationContext(data, defaultClass, param, factory);
try
{
- return factory.createBean(fetchBeanClass(data, defaultClass,
- factory), data, param);
+ return factory.createBean(bcc);
}
catch (Exception ex)
{
@@ -549,6 +550,54 @@ public final class BeanHelper
}
/**
+ * Creates a {@code BeanCreationContext} object for the creation of the
+ * specified bean.
+ *
+ * @param data the bean declaration
+ * @param defaultClass the default class to use
+ * @param param an additional parameter that will be passed to the bean
+ * factory; some factories may support parameters and behave
+ * different depending on the value passed in here
+ * @param factory the current bean factory
+ * @return the {@code BeanCreationContext}
+ * @throws ConfigurationRuntimeException if the bean class cannot be
+ * determined
+ */
+ private static BeanCreationContext createBeanCreationContext(
+ final BeanDeclaration data, Class<?> defaultClass,
+ final Object param, BeanFactory factory)
+ {
+ final Class<?> beanClass = fetchBeanClass(data, defaultClass, factory);
+ return new BeanCreationContext()
+ {
+ public void initBean(Object bean, BeanDeclaration data)
+ {
+ BeanHelper.initBean(bean, data);
+ }
+
+ public Object getParameter()
+ {
+ return param;
+ }
+
+ public BeanDeclaration getBeanDeclaration()
+ {
+ return data;
+ }
+
+ public Class<?> getBeanClass()
+ {
+ return beanClass;
+ }
+
+ public Object createBean(BeanDeclaration data)
+ {
+ return BeanHelper.createBean(data);
+ }
+ };
+ }
+
+ /**
* Returns a list with all constructors which are compatible with the
* constructor arguments specified by the given {@code BeanDeclaration}.
*
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=1520792&r1=1520791&r2=1520792&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
Sat Sep 7 19:42:17 2013
@@ -98,18 +98,14 @@ public class DefaultBeanFactory implemen
* This makes it easier for derived classes that need to change specific
* functionality of the base class.
*
- * @param beanClass the class of the bean, from which an instance is to be
- * created
- * @param data the bean declaration object
- * @param parameter an additional parameter (ignored by this
implementation)
+ * @param bcc the context object defining the bean to be created
* @return the new bean instance
* @throws Exception if an error occurs
*/
- public Object createBean(Class<?> beanClass, BeanDeclaration data,
- Object parameter) throws Exception
+ public Object createBean(BeanCreationContext bcc) throws Exception
{
- Object result = createBeanInstance(beanClass, data);
- initBeanInstance(result, data);
+ Object result = createBeanInstance(bcc);
+ initBeanInstance(result, bcc);
return result;
}
@@ -129,33 +125,32 @@ public class DefaultBeanFactory implemen
* {@code createBean()}. It uses reflection to create a new instance
* of the specified class.
*
- * @param beanClass the class of the bean to be created
- * @param data the bean declaration
+ * @param bcc the context object defining the bean to be created
* @return the new bean instance
* @throws Exception if an error occurs
*/
- protected Object createBeanInstance(Class<?> beanClass, BeanDeclaration
data)
+ protected Object createBeanInstance(BeanCreationContext bcc)
throws Exception
{
- Constructor<?> ctor = BeanHelper.findMatchingConstructor(beanClass,
data);
- Object[] args = fetchConstructorArgs(ctor, data);
+ Constructor<?> ctor =
+ BeanHelper.findMatchingConstructor(bcc.getBeanClass(),
+ bcc.getBeanDeclaration());
+ Object[] args = fetchConstructorArgs(ctor, bcc);
return ctor.newInstance(args);
}
/**
* Initializes the newly created bean instance. This method is called by
- * {@code createBean()}. It calls the
- * {@link BeanHelper#initBean(Object, BeanDeclaration) initBean()}
- * of {@link BeanHelper} for performing the initialization.
+ * {@code createBean()}. It calls the {@code initBean()} method of the
+ * context object for performing the initialization.
*
* @param bean the newly created bean instance
- * @param data the bean declaration object
+ * @param bcc the context object defining the bean to be created
* @throws Exception if an error occurs
*/
- protected void initBeanInstance(Object bean, BeanDeclaration data)
- throws Exception
+ protected void initBeanInstance(Object bean, BeanCreationContext bcc)
throws Exception
{
- BeanHelper.initBean(bean, data);
+ bcc.initBean(bean, bcc.getBeanDeclaration());
}
/**
@@ -164,22 +159,22 @@ public class DefaultBeanFactory implemen
* conversions.
*
* @param ctor the constructor to be invoked
- * @param data the current bean declaration
+ * @param bcc the context object defining the bean to be created
* @return an array with constructor arguments
*/
private Object[] fetchConstructorArgs(Constructor<?> ctor,
- BeanDeclaration data)
+ BeanCreationContext bcc)
{
Class<?>[] types = ctor.getParameterTypes();
- assert types.length == nullSafeConstructorArgs(data).size() :
+ assert types.length ==
nullSafeConstructorArgs(bcc.getBeanDeclaration()).size() :
"Wrong number of constructor arguments!";
Object[] args = new Object[types.length];
int idx = 0;
- for (ConstructorArg arg : nullSafeConstructorArgs(data))
+ for (ConstructorArg arg :
nullSafeConstructorArgs(bcc.getBeanDeclaration()))
{
Object val =
- arg.isNestedBeanDeclaration() ? BeanHelper.createBean(arg
+ arg.isNestedBeanDeclaration() ? bcc.createBean(arg
.getBeanDeclaration()) : arg.getValue();
args[idx] = getConversionHandler().to(val, types[idx], null);
idx++;
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java?rev=1520792&r1=1520791&r2=1520792&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java
Sat Sep 7 19:42:17 2013
@@ -566,26 +566,25 @@ public class TestBeanHelper
boolean supportsDefaultClass;
- public Object createBean(Class<?> beanClass, BeanDeclaration data,
Object param)
- throws Exception
+ public Object createBean(BeanCreationContext bcc) throws Exception
{
- parameter = param;
- if (BeanCreationTestBean.class.equals(beanClass))
+ parameter = bcc.getParameter();
+ if (BeanCreationTestBean.class.equals(bcc.getBeanClass()))
{
BeanCreationTestBean bean = new BeanCreationTestBean();
- BeanHelper.initBean(bean, data);
+ BeanHelper.initBean(bean, bcc.getBeanDeclaration());
return bean;
}
- else if (BeanCreationTestBeanWithListChild.class.equals(beanClass))
+ else if
(BeanCreationTestBeanWithListChild.class.equals(bcc.getBeanClass()))
{
BeanCreationTestBeanWithListChild bean = new
BeanCreationTestBeanWithListChild();
- BeanHelper.initBean(bean, data);
+ BeanHelper.initBean(bean, bcc.getBeanDeclaration());
return bean;
}
else
{
throw new IllegalArgumentException("Unsupported class: "
- + beanClass);
+ + bcc.getBeanClass());
}
}
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=1520792&r1=1520791&r2=1520792&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
Sat Sep 7 19:42:17 2013
@@ -56,6 +56,45 @@ public class TestDefaultBeanFactory
}
/**
+ * Creates a bean creation context for a create operation.
+ *
+ * @param cls the bean class
+ * @param decl the bean declaration
+ * @return the new creation context
+ */
+ private static BeanCreationContext createBcc(final Class<?> cls,
+ final BeanDeclaration decl)
+ {
+ return new BeanCreationContext()
+ {
+ public void initBean(Object bean, BeanDeclaration data)
+ {
+ BeanHelper.initBean(bean, data);
+ }
+
+ public Object getParameter()
+ {
+ return null;
+ }
+
+ public BeanDeclaration getBeanDeclaration()
+ {
+ return decl;
+ }
+
+ public Class<?> getBeanClass()
+ {
+ return cls;
+ }
+
+ public Object createBean(BeanDeclaration data)
+ {
+ return BeanHelper.createBean(data);
+ }
+ };
+ }
+
+ /**
* Tests obtaining the default class. This should be null.
*/
@Test
@@ -100,8 +139,7 @@ public class TestDefaultBeanFactory
Map<String, Object> props = new HashMap<String, Object>();
props.put("throwExceptionOnMissing", Boolean.TRUE);
decl.setBeanProperties(props);
- Object bean = factory.createBean(PropertiesConfiguration.class,
- decl, null);
+ Object bean =
factory.createBean(createBcc(PropertiesConfiguration.class, decl));
assertNotNull("New bean is null", bean);
assertEquals("Bean is of wrong class", PropertiesConfiguration.class,
bean.getClass());
@@ -122,8 +160,8 @@ public class TestDefaultBeanFactory
args.add(ConstructorArg.forValue("42"));
decl.setConstructorArgs(args);
BeanCreationTestCtorBean bean =
- (BeanCreationTestCtorBean) factory.createBean(
- BeanCreationTestCtorBean.class, decl, null);
+ (BeanCreationTestCtorBean) factory.createBean(createBcc(
+ BeanCreationTestCtorBean.class, decl));
assertEquals("Wrong string property", "test", bean.getStringValue());
assertEquals("Wrong int property", 42, bean.getIntValue());
}
@@ -145,8 +183,8 @@ public class TestDefaultBeanFactory
.forBeanDeclaration(declNested,
BeanCreationTestBean.class.getName())));
BeanCreationTestCtorBean bean =
- (BeanCreationTestCtorBean) factory.createBean(
- BeanCreationTestCtorBean.class, decl, null);
+ (BeanCreationTestCtorBean) factory.createBean(createBcc(
+ BeanCreationTestCtorBean.class, decl));
assertNotNull("Buddy bean was not set", bean.getBuddy());
assertEquals("Wrong property of buddy bean", "test", bean.getBuddy()
.getStringValue());