Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT and Generics

Tags:

java

generics

gwt

We are having a SerializationException error when sending a list of objects using RPC and Java Generics.

I'm creating this widget to show the error:

public class Test<T> {

    ListDataProvider<T> ldp = new ListDataProvider<T>();

    public void setItems(List<T> list){
        for(T t :list){
            ldp.getList().add(t);
        }
    }

    public List<T> getItems(){
        return ldp.getList();

    }

}

This is the code for creating the Test widget and passing a list of POJOs (where ExporterFormKey is the POJO object)

List<ExporterFormKey> list = new ArrayList<ExporterFormKey>();
ExporterFormKey key = new ExporterFormKey();
key.setKey("key1");
list.add(key);

Test<ExporterFormKey> test = new Test<ExporterFormKey>();
test.setItems(list);

At the end the next code throws a SerializationException:

service.sendList(test.getList(), new AsyncCallback...);

While the next one does fine:

service.sendList(list, new AsyncCallback...);

-----Edit----

I found that doing the next code also works

List<ExporterFormKey> newList = new ArrayList<ExporterFormKey>();
newList.add(test.getItems().get(0));
service.sendList(newList , new AsyncCallback...);

Or this also works

List<ExporterFormKey> newList = new ArrayList<ExporterFormKey>(test.getItems());

I also found this change on Test works!

public List<T> getItems(){
    return new ArrayList<T>(ldp.getList());
}
like image 307
ramon_salla Avatar asked Dec 19 '12 08:12

ramon_salla


People also ask

Is it a good idea to use generics in collections?

By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.

Why are generics used in collections?

The generic collections disable the type-casting and there is no use of type-casting when it is used in generics. The generic collections are type-safe and checked at compile-time. These generic collections allow the datatypes to pass as parameters to classes.

What does T stand for in generics?

< T > is a conventional letter that stands for "Type", and it refers to the concept of Generics in Java. You can use any letter, but you'll see that 'T' is widely preferred. WHAT DOES GENERIC MEAN? Generic is a way to parameterize a class, method, or interface.

What is the difference between T and in Java generics?

6 Answers. Show activity on this post. Well there's no difference between the first two - they're just using different names for the type parameter ( E or T ). The third isn't a valid declaration - ? is used as a wildcard which is used when providing a type argument, e.g. List<?>


1 Answers

http://blog.rubiconred.com/2011/04/gwt-serializationexception-on-rpc-call.html

As izaera suggested the ListDataProvider uses a non-serializable list implementation (ListWrapper) which cannot be sent directly across the wire.

Wrapping the response from ListDataProvider's getList() method into a new ArrayList as you have suggested in your post is the simplest way to workaround the issue.

like image 70
Josh Avatar answered Oct 05 '22 18:10

Josh