Я начинаю использовать BeanUtils для преобразования файлов свойств в JavaBean.Используя BeanUtils.populate, я могу сделать это красиво.Но я могу добиться ретро преобразования из JavaBean в Map правильно (сохраняются только простые значения).
См. Этот пример на основе класса Employee в документации BeanUtils.
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);
}
}
Результат:
MyfirstName
MylastName
MyProCity
MyProStreet
22
MyPersoCity
MyPersoStreet
2
Sub0FirstName
Sub0LastName
Sub1FirstName
Sub1LastName
----------------
{lastName=MylastName, class=class Employee, firstName=MyfirstName}
Чего мне не хватает, чтобы карта2 фактически сохраняла ключи типа "address (pro) .city" или "subordinate [1] .firstName" с помощью метода BeanUtils.describe?