commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [BeanUtils]: describe method bug with array as bean property
Date Fri, 25 Apr 2003 14:48:40 GMT
I've read the "Roles & Responsibilities" document and would like to
contribute this change.  Not sure if there's an easier way so here goes...

The BeanUtils.describe method does not fully describe the bean when an
array property exists.  It only shows the first entry of the array.

I've made the changes to org.apache.commons.beanutils.ConvertUtils to
support array property description.  It works on Object and primitive
arrays as well as recursively on arrays of arrays.

Here's the modified section of the
org.apache.commons.beanutils.ConvertUtils.convert() method:


        } else if (value.getClass().isArray()) {
            if (Array.getLength(value) < 1) {
                return (null);
            value = Array.get(value, 0);
            if (value == null) {
                return ((String) null);
            } else {
                Converter converter = (Converter)
                return ((String) converter.convert(String.class, value));
        } else {


// New constant in class
protected static final String arrayEntrySeparator = ", ";

        } else if (value.getClass().isArray()) {
            if (Array.getLength(value) < 1) {
                return (null);
            StringBuffer sb = new StringBuffer();
             int length = Array.getLength(value);
             for (int i=0; i<length; i++) {
               Object val2 = Array.get(value, i);
               if (val2.getClass().isArray()) {
                    sb.append(convert(val2) + arrayEntrySeparator);
               } else if (val2 == null) {
                    sb.append("null" + arrayEntrySeparator);
                 } else {
                     Converter converter = (Converter)
                     sb.append(((String) converter.convert(String.class,
val2)) + arrayEntrySeparator);
             // Delete final comma and space, no more entries
            return sb.toString();
        } else {

  - I followed the ArrayList.toString() output format [a1, a2] for arrays.
If a new JDK release changes this (doubtful but possible) this code will
require modification for consistency purposes.
  - The result could get large if the internal arrays are large.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message