Setting an indexed list property using BeanUtils

I am trying to use BeanUtils to interact with a Java bean, as shown below:

public class Bean {
    private List<Integer> prices = new LinkedList<Integer>();

    public List<Integer> getPrices() {
        return prices;
    }
}

According to the BeanUtils Documentation , BeanUtils supports Lists indexed properties :

As an extension to the JavaBeans specification, the BeanUtils package considers any property for which the underlying data type is java.util.List (or the List implementation), which must also be indexed.

However, let's say I'm trying to do something like the following:

Bean bean = new Bean();

// Add nulls to make the list the correct size
bean.getPrices().add(null);
bean.getPrices().add(null);

BeanUtils.setProperty(bean, "prices[0]", 123);
PropertyUtils.setProperty(bean, "prices[1]", 456);

System.out.println("prices[0] = " + BeanUtils.getProperty(bean, "prices[0]"));
System.out.println("prices[1] = " + BeanUtils.getProperty(bean, "prices[1]"));

Conclusion:

prices[0] = null
prices[1] = 456

Why BeanUtils.setProperty()can’t I set the indexing property, or PropertyUtils.setProperty()maybe? Does BeanUtils not support type conversion for objects inside Lists?

+3
1

BeanUtils setter . Bean setter prices, , : -

public void setPrices(List<Integer> prices) {
    this.prices = prices;
}
+5

Source: https://habr.com/ru/post/1788324/


All Articles