commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hughes_sh...@jpmorgan.com
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:

Old:

        } 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)
converters.get(String.class);
                return ((String) converter.convert(String.class, value));
            }
        } else {

New:

// 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();
             sb.append("[");
             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)
converters.get(String.class);
                     sb.append(((String) converter.convert(String.class,
val2)) + arrayEntrySeparator);
                 }
             }
             // Delete final comma and space, no more entries
             sb.delete(sb.length()-2,sb.length());
             sb.append("]");
            return sb.toString();
        } else {

Caveats:
  - 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.

Shawn.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message