[
https://issues.apache.org/jira/browse/CONFIGURATION-546?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13680797#comment-13680797
]
Justin Couch commented on CONFIGURATION-546:
--------------------------------------------
In my case I have an external processor that loads configurations into my own
implementation of BeanDeclaration (one example has the original definitions are
coming from database information, not a text file) that is then used to build
the appropriate beans.
A simpler example is something like this (hope Jira doesn't mess the formatting
:) ):
{code}
<ThermalModel>
<TemperaturePoints>
<FahrenheitPoint id="123" type="read"/>
<CelsiusPoint id="abc" type="read/>
</TemperaturePoints>
</ThermalModel>
{code}
The ThermalModel has a property that are a list of points that it may read from
to do some averaging of them, for example without actually caring what the
concrete class is. We use the Bean helper to take this configuration and load
the implementations. When not driven by the database, we use domain-specific
XML or JSON definitions for the beans so that end users that are not software
developers can build system configurations. As such, we don't have any interest
in the rest of the framework around bean declarations etc. I could probably
have a look into adding the capabilities, but that's going to be a bit in my
spare time :)
> ClassCastException in BeanHelper constructing beans with a list of child beans
> ------------------------------------------------------------------------------
>
> Key: CONFIGURATION-546
> URL: https://issues.apache.org/jira/browse/CONFIGURATION-546
> Project: Commons Configuration
> Issue Type: Bug
> Components: Type conversion
> Affects Versions: 1.8, 1.9
> Reporter: Justin Couch
> Priority: Minor
> Attachments: BeanCreateTestBeanWithListChild-BUG564.patch,
> BeanCreationTestBeanWithListChild.java,
> BeanCreationTestBeanWithListChild.java, BeanHelper-BUG564.patch,
> BeanHelper.java, TestBeanHelper-BUG564.patch, TestBeanHelper.java,
> TestBeanHelper.java
>
>
> If you wish to create a bean that has a collection as a property where that
> collection contains multiple beans, the code crashes as it assumes that you
> can only ever have single bean declarations as children.
> For example the following brief construct is not possible to build
> class A {
> String name;
> }
> class B {
> List<A> children;
> }
> The result is the following stack trace
> Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast
> to org.apache.commons.configuration.beanutils.BeanDeclaration
> at
> org.apache.commons.configuration.beanutils.BeanHelper.initBean(BeanHelper.java:212)
> at
> org.apache.commons.configuration.beanutils.TestBeanHelper$TestBeanFactory.createBean(TestBeanHelper.java:579)
> at
> org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:342)
> ... 33 more
> It's pretty easy to see why when you look at the code beginning line 208:
> for (Map.Entry<String, Object> e : nestedBeans.entrySet())
> {
> String propName = e.getKey();
> Class<?> defaultClass = getDefaultClass(bean, propName);
> initProperty(bean, propName, createBean(
> (BeanDeclaration) e.getValue(), defaultClass));
> }
>
> Note the silly assumption that e.getValue() only ever has BeanDeclaration
> instances, where it could be BeanDeclaration[], or
> Collection<BeanDeclaration> as other possible options - which is what the
> above example show.
> Extended version of the existing unit test to follow that illustrates the
> problem.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira