arjacsoh
arjacsoh

Reputation: 9232

Use a generic List in a ListDataProvider

I encounter a weird problem with Generics when I want to use them in a GWT application based on the CellTree. I want in particular to hold for every Node in the tree the nodes which belong to it, namely its children Nodes. To this end I have created a generic class NodeData. This have made of course my application more complicated and cumbersome but in term of Nodes it has done its job. The point I could not overcome is the modification (refresh) of the ListDataProviders which correspond to each Node. It is a problem with generics as you can see on the following code:

static class NodeData<T extends BaseProxy> {
NodeData(T data) {
  this.data = data;
}

public T data;
public ListDataProvider<? extends NodeData<? extends BaseProxy>> children;
}

Later in the project I try the lines of code:

NodeData<?> data = (NodeData<?>) value;
List<NodeData<? extends BaseProxy>> newData = new ArrayList<NodeData<? extends BaseProxy>>(data.children.getList());
newData.add(new NodeData<BaseProxy>(customer));
data.children.setList(newData);

Although I do not get a compile error, I get a runtime error regarding the last line, which seems quite odd to me because the compiler does not detect it:

The method setList(List<capture#9-of ? extends CustomTreeModel.NodeData<? extends BaseProxy>>) in the type ListDataProvider<capture#9-of ? extends CustomTreeModel.NodeData<? extends BaseProxy>> is not applicable for the arguments (List<CustomTreeModel.NodeData<? extends BaseProxy>>)

It is the same List which I get from the ListDataProvider, but it cannot accept it back. What should I change/do ?

Upvotes: 2

Views: 265

Answers (1)

gontard
gontard

Reputation: 29510

This is weird...

The following code compiles and runs without any problems.

public class CustomTreeModel {

    public static void main(final String[] args) {
        final BaseProxy customer = new Customer();
        final Object value = new NodeData<BaseProxy>(new Customer());
        final NodeData<?> data = (NodeData<?>) value;
        final List<NodeData<? extends BaseProxy>> newData = new ArrayList<NodeData<? extends BaseProxy>>(
                data.children.getList());
        newData.add(new NodeData<BaseProxy>(customer));
        data.children.setList(newData);
    }

    private static class Customer implements BaseProxy {

    }

    public static class NodeData<T extends BaseProxy> {
        NodeData(final T data) {
            this.data = data;
        }

        public T data;
        public ListDataProvider<? extends NodeData<? extends BaseProxy>> children = new ListDataProvider<NodeData<? extends BaseProxy>>();

    }
}

Could you try it ?

Are you sure that all the source code is compiled ?

May be you should provide more source code.

Upvotes: 2

Related Questions