As recommended by Sonar rule RSPEC-1452,
Generic wildcard types should not be used in return types
How can I define setter method of a property that accepts a List<? extends Item>, but getter method that returns List<Item>?
Consider the below contract of Cart interface:
interface Cart {
    public List<Item> getItems();
}
interface Item {
    public int getCost();
}
Clients of this contract do not need to know about any known implementations of Cart or Item interfaces, e.g.
void getValue(Cart cart) {
    int sum = cart.getItems().stream().mapToInt(Item::getCost).sum();
}
However, the library that implements the Cart interface obviously has to deal with concrete classes. Assume it (or its Builder) also has to expose a setItems method so that another internal class can set the entire list of items. Upper bound of Interface allows to pass a List<DefaultItem> to setItems method.
static class DefaultCart implements Cart {
    private List<Item> items;
    public void setItems(List<? extends Item> items) {
        this.items = new ArrayList<Item>(items);
    }
    @Override
    public List<Item> getItems() {
        return items;
    }
}
I am not happy with the above setItems method as it creates another copy of the list. What is a better way of dealing with this?
 
    