Uploaded image for project: 'Commons Configuration'
  1. Commons Configuration
  2. CONFIGURATION-546

ClassCastException in BeanHelper constructing beans with a list of child beans

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.8, 1.9
    • Fix Version/s: 2.0, 1.10
    • Component/s: Type conversion
    • Labels:
      None

      Description

      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.

        Attachments

        1. TestBeanHelper-BUG564.patch
          5 kB
          Justin Couch
        2. BeanCreateTestBeanWithListChild-BUG564.patch
          2 kB
          Justin Couch
        3. BeanHelper-BUG564.patch
          3 kB
          Justin Couch
        4. BeanHelper.java
          23 kB
          Justin Couch
        5. TestBeanHelper.java
          22 kB
          Justin Couch
        6. BeanCreationTestBeanWithListChild.java
          2 kB
          Justin Couch
        7. TestBeanHelper.java
          22 kB
          Justin Couch
        8. BeanCreationTestBeanWithListChild.java
          2 kB
          Justin Couch

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              justinc Justin Couch
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: