[jira] [Created] (BEANUTILS-409) BeanUtils - 'describe' method returning Incorrect array value

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Created] (BEANUTILS-409) BeanUtils - 'describe' method returning Incorrect array value

ASF GitHub Bot (Jira)
BeanUtils - 'describe' method returning Incorrect array value
-------------------------------------------------------------

                 Key: BEANUTILS-409
                 URL: https://issues.apache.org/jira/browse/BEANUTILS-409
             Project: Commons BeanUtils
          Issue Type: Bug
    Affects Versions: 1.8.3
         Environment: commons-beanutils 1.8.3, jdk 1.6.0_20
            Reporter: benny
            Priority: Critical


I want to convert a bean class to a map (key=the name of the member,value=the value of the member).
I'm using the method BeanUtils.describe(beanClass);
(I'm using commons-beanutils 1.8.3, jdk 1.6.0_20, on commons-beanutils 1.5 it works)
The problem is that the return value is incorrect, (the map contain only the first item from the array),

the code:
public class Demo {
        private ArrayList<String> myList = new ArrayList<String>();
        public Demo() {
            myList.add("first_value");
            myList.add("second_value");
        }
 
        public ArrayList<String> getMyList() {
            return myList;
        }
 
        public void setMyList(ArrayList<String> myList) {
            this.myList = myList;
        }
 
        public static void main(String[] args) {
            Demo myBean = new Demo();
            try {
                Map describe = BeanUtils.describe(myBean);
                Iterator it = describe.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry pairs = (Map.Entry) it.next();
                    System.out.println(String.format("key=%s,value=%s", (String) pairs.getKey(), (String) pairs.getValue()));
 
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

 •The expected output:
 
key=myList,value=[first_value,second_value]
key=class,value=class $Demo

 •But the real output is:
 
key=myList,value=[first_value]
key=class,value=class $Demo

As you can see the array contains two values but the output(and the map) contains only one,why??


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira


Reply | Threaded
Open this post in threaded view
|

[jira] [Commented] (BEANUTILS-409) BeanUtils - 'describe' method returning Incorrect array value

ASF GitHub Bot (Jira)

    [ https://issues.apache.org/jira/browse/BEANUTILS-409?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13222154#comment-13222154 ]

Senthil Kumar Balakrishnan commented on BEANUTILS-409:
------------------------------------------------------

Hi Benny,

The Code is deliberately returning first value of the collection, though i don't understand why it should. below is the code,

org.apache.commons.beanutils.converters.AbstractConverter.java

   /**
     * Return the first element from an Array (or Collection)
     * or the value unchanged if not an Array (or Collection).
     *
     * N.B. This needs to be overriden for array/Collection converters.
     *
     * @param value The value to convert
     * @return The first element in an Array (or Collection)
     * or the value unchanged if not an Array (or Collection)
     */
    protected Object convertArray(Object value) {
        if (value == null) {
            return null;
        }
        if (value.getClass().isArray()) {
            if (Array.getLength(value) > 0) {
                return Array.get(value, 0);
            } else {
                return null;
            }
        }
        if (value instanceof Collection) {
            Collection collection = (Collection)value;
            if (collection.size() > 0) {
                //Note: Looks like this is done deliberately.
                return collection.iterator().next();
            } else {
                return null;
            }
        }
        return value;
    }

Thanks,
-Senthil Balakrishnan
               

> BeanUtils - 'describe' method returning Incorrect array value
> -------------------------------------------------------------
>
>                 Key: BEANUTILS-409
>                 URL: https://issues.apache.org/jira/browse/BEANUTILS-409
>             Project: Commons BeanUtils
>          Issue Type: Bug
>    Affects Versions: 1.8.3
>         Environment: commons-beanutils 1.8.3, jdk 1.6.0_20
>            Reporter: benny
>            Priority: Critical
>              Labels: describe
>
> I want to convert a bean class to a map (key=the name of the member,value=the value of the member).
> I'm using the method BeanUtils.describe(beanClass);
> (I'm using commons-beanutils 1.8.3, jdk 1.6.0_20, on commons-beanutils 1.5 it works)
> The problem is that the return value is incorrect, (the map contain only the first item from the array),
> the code:
> public class Demo {
>         private ArrayList<String> myList = new ArrayList<String>();
>         public Demo() {
>             myList.add("first_value");
>             myList.add("second_value");
>         }
>  
>         public ArrayList<String> getMyList() {
>             return myList;
>         }
>  
>         public void setMyList(ArrayList<String> myList) {
>             this.myList = myList;
>         }
>  
>         public static void main(String[] args) {
>             Demo myBean = new Demo();
>             try {
>                 Map describe = BeanUtils.describe(myBean);
>                 Iterator it = describe.entrySet().iterator();
>                 while (it.hasNext()) {
>                     Map.Entry pairs = (Map.Entry) it.next();
>                     System.out.println(String.format("key=%s,value=%s", (String) pairs.getKey(), (String) pairs.getValue()));
>  
>                 }
>             } catch (Exception e) {
>                 e.printStackTrace();
>             }
>         }
>     }
>  •The expected output:
>  
> key=myList,value=[first_value,second_value]
> key=class,value=class $Demo
>  •But the real output is:
>  
> key=myList,value=[first_value]
> key=class,value=class $Demo
> As you can see the array contains two values but the output(and the map) contains only one,why??

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira