user711413
user711413

Reputation: 777

GWT serialization should not return interfaces: what about parameters and contained objects?

There are already a few questions regarding the fact that methods in GWT RPC should not return an interface like List, but rather a concrete class like ArrayList, because otherwise "GWT needs to include all possible implementations". See e.g. In GWT, why shouldn't a method return an interface?

Here's my question: is this limited to the return type itself? How about parameters of the method? And what if the return object contains an interface, e.g.

public class MyReturnObject implements IsSerializable {
   List<String> listOfUnspecifiedType1;
   List<Long> listOfUnspecifiedType2;
   ...
}

The examples I have seen all talk of the return type itself. I don't see why it would be a problem to return an interface, but not a problem to return an object which just wraps an interface; but maybe I am missing something?

Upvotes: 2

Views: 491

Answers (1)

Thomas Broyer
Thomas Broyer

Reputation: 64541

It's clear from the linked question that it applies recursively (and as soon as you understand why you should use the most derived types as possible, it becomes obvious that it is recursive).

This is also true of method arguments, not only the return types and their fields: if you send a List<X> then GWT has to generate serialization code for all List classes: ArrayList, LinkedList, etc.

And of course the same applies to classes, not only interfaces: AbstractList is no different from List.

And because generation comes before optimization, all possible classes from the source path will be included, not only those that you use in your code; and then they come in the way of the optimization pass, as all those classes are now used by your app.

Therefore, the rule is: use the most specific types as possible. The corollary is: don't fear DTOs, don't try to send your business/domain objects at all cost.

Upvotes: 3

Related Questions