Convert a JavaBean to key/value Map with nested name using commons-beans BeanUtils

Stephane Toussaint picture Stephane Toussaint · Nov 17, 2011 · Viewed 7.7k times · Source

I start using BeanUtils to convert a Properties files to a JavaBean. Using BeanUtils.populate, I'm able to do this nicely. But I can achieve the retro conversion from the JavaBean to the Map correctly (only simple values are store).

See this sample based on the Employee Class form the BeanUtils documentation.

import org.apache.commons.beanutils.BeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Employee {

    private Map<String, Address> addressMap = new HashMap<String, Address>();
    private List<Employee> subordinateList = new ArrayList<Employee>();

    private String firstName;
    private String lastName;

    public Address getAddress(String type) {
        if (!addressMap.containsKey(type)) {
            addressMap.put(type, new Address());
        }

        return addressMap.get(type);
    }

    public void setAddress(String type, Address address) {
        addressMap.put(type, address);
    }

    public Employee getSubordinate(int index) {
        if (subordinateList.size() <= index) {
            subordinateList.add(new Employee());
        }

        return subordinateList.get(index);
    }

    public void setSubordinate(int index, Employee subordinate) {
        subordinateList.add(index, subordinate);
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public class Address {
        private String city;
        private String street;
        private int number;

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getStreet() {
            return street;
        }

        public void setStreet(String street) {
            this.street = street;
        }

        public int getNumber() {
            return number;
        }

        public void setNumber(int number) {
            this.number = number;
        }
    }

    public static void main(String[] args) throws Exception {
        Map<String, Object> map = new TreeMap<String, Object>();

        map.put("firstName", "MyfirstName");
        map.put("lastName", "MylastName");
        map.put("address(pro).city", "MyProCity");
        map.put("address(pro).street", "MyProStreet");
        map.put("address(pro).number", 22);

        map.put("subordinate[1].firstName", "Sub1FirstName");
        map.put("subordinate[1].lastName", "Sub1LastName");

        map.put("address(perso).city", "MyPersoCity");
        map.put("address(perso).street", "MyPersoStreet");
        map.put("address(perso).number", 2);

        map.put("subordinate[0].firstName", "Sub0FirstName");
        map.put("subordinate[0].lastName", "Sub0LastName");


        Employee employee = new Employee();
        BeanUtils.populate(employee, map);

        System.out.println(employee.getFirstName());
        System.out.println(employee.getLastName());

        System.out.println(employee.getAddress("pro").city);
        System.out.println(employee.getAddress("pro").street);
        System.out.println(employee.getAddress("pro").number);

        System.out.println(employee.getAddress("perso").city);
        System.out.println(employee.getAddress("perso").street);
        System.out.println(employee.getAddress("perso").number);

        System.out.println(employee.getSubordinate(0).firstName);
        System.out.println(employee.getSubordinate(0).lastName);

        System.out.println(employee.getSubordinate(1).firstName);
        System.out.println(employee.getSubordinate(1).lastName);

        Map<String, Object> map2 = BeanUtils.describe(employee);

        System.out.println("----------------");

        System.out.println(map2);


    }

}

The result :

MyfirstName
MylastName
MyProCity
MyProStreet
22
MyPersoCity
MyPersoStreet
2
Sub0FirstName
Sub0LastName
Sub1FirstName
Sub1LastName
----------------
{lastName=MylastName, class=class Employee, firstName=MyfirstName}

What am I missing so that the map2 stores actually keys like "address(pro).city" or "subordinate[1].firstName" using the BeanUtils.describe method ?

Answer

Stephane Toussaint picture Stephane Toussaint · Nov 18, 2011

Finally I find a way to solve this. First of all, I need to retrieve every nested propertyName based on my current bean instance, and this recursively. So I've wrote a simple method to do this :

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class MyPropertyUtils {

    public List<String> listNestedPropertyName(Object objectSource) throws Exception {
        List<String> nodeNameList = new ArrayList<String>();

        if (Serializable.class.isAssignableFrom(objectSource.getClass())) {
            nodeNameList.add(objectSource.toString());
            return nodeNameList;
        }

        PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(objectSource.getClass());

        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {

            Method method = propertyDescriptor.getReadMethod();
            if (propertyDescriptor.getReadMethod() == null) {
                continue;
            }

            if (method.getGenericParameterTypes().length > 0) {
                continue;
            }

            String name = propertyDescriptor.getName();

            Object value = method.invoke(objectSource);

            if (value == null) {
                continue;
            }

            if (Map.class.isAssignableFrom(value.getClass())) { // Mapped name
                Map map = ((Map) value);
                name = StringUtils.substringBeforeLast(name, "Map");

                for (Object key : map.keySet()) {
                    String mappedName = name + "(" + key.toString() + ")";
                    List<String> nestedNames = listNestedPropertyName(map.get(key));

                    for (String nestedName : nestedNames) {
                        nodeNameList.add(mappedName + "." + nestedName);
                    }
                }

            } else if (List.class.isAssignableFrom(value.getClass())) { // Indexed name
                List list = ((List) value);
                name = StringUtils.substringBeforeLast(name, "List");

                for (int i = 0; i < list.size(); i++) {
                    String indexedName = name + "[" + i + "]";
                    List<String> nestedNames = listNestedPropertyName(list.get(i));

                    for (String nestedName : nestedNames) {
                        nodeNameList.add(indexedName + "." + nestedName);
                    }
                }
            } else if (Serializable.class.isAssignableFrom(value.getClass())) { // Simple Value
                nodeNameList.add(name);
            } else { // Nested Value
                List<String> nestedNames = listNestedPropertyName(value);

                for (String nestedName : nestedNames) {
                    nodeNameList.add(name + "." + nestedName);
                }
            }
        }

        return nodeNameList;
    }
}

And then I iterate other those names to retrieve property value and then set them in the Map.

Map<String, Object> map = new HashMap<String, Object>();

MyPropertyUtils myPropertyUtils = new MyPropertyUtils();
List<String> names = myPropertyUtils.listNestedPropertyName(employee);
for (String name : names) {
    map.put(name, PropertyUtils.getNestedProperty(employee, name));
}

This work well for my use case. I simply had add an accessor in my source object for accessing the Map or the List with a conventional name (propertyName + "Map" or "List").

Maybe this could interest someone. Anyway, if there is something more obvious to do it, let me know.