1

I have a GWT shared-package class as follows:

public class MyCustomClass extends TreeSet<MyCustomType> implements Serializable, IsSerializable {
     // ... a whole bunch of methods
}

I am trying to send an instance of the class as an object encapsulated in another class, through RPC.

The problem is the TreeSet, as GWT refuses to serialize it, no matter what I do. I get an error during runtime:

SEVERE: my-service: An IncompatibleRemoteServiceException was thrown while processing this call. com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException: java.lang.ClassNotFoundException: http: at com.google.gwt.user.server.rpc.RPC.decodeRequest(RPC.java:323)

So, I have all the serialization pre-requisites (default constructor, no final fields, getters and setters, all my instance variables are serializable, implement interfaces, etc.), but this continues to happen.

The thing is, when I switch from TreeSet to using an ArrayList instead, everything works fine. In my understanding, this is because I am already using ArrayLists in a number of services, so GWT knows to whitelist it in the serialization policies.

That understanding came because of this thread. And I've tried the proposed solution from the accepted answer (create a "dummy" service and put a TreeSet there, I even created a dummy class in my client package and put a TreeSet as a field), but no luck.

I checked my .gwt.xml just in case, the TreeSet is not added as a serialization exception.

So, I am pretty much stuck at this point, my workaround is to use the ArrayList and re-pack it into a transient set, but I do not really like that approach.

Any help would be highly appreciated.

(using GWT 2.6)

Update: MyCustomType implements the Comparable<MyCustomType> interface, and there is no custom comparator provided to the TreeSet.

Community
  • 1
  • 1
Miloš Ranđelović
  • 444
  • 1
  • 5
  • 13

1 Answers1

0

May be MyCustomType doesn't implement java.lang.Comparable

It works fine for ArrayList<MyCustomType> but not for TreeSet<MyCustomType>


--EDIT--

HashSet<MyCustomType> or LinkedHashSet<MyCustomType> are working fine.

After getting data at client side just use

new TreeSet<MyCustomType>(<retured HashSet<MyCustomType>)

There is no need of iterating it because it is ordered by <Comparable<MyCustomType>.

Braj
  • 46,415
  • 5
  • 60
  • 76
  • It does implement Comparable, so it must be something else :/ – Miloš Ranđelović Mar 19 '14 at 22:58
  • OK Let me figure it out. – Braj Mar 19 '14 at 23:01
  • All the model classes are implementing `IsSerializable` and `Serializable`, Right? – Braj Mar 19 '14 at 23:03
  • Yes, they are. Like I said, as soon as I switch from TreeSet to ArrayList (everything else remains the same), everything works. – Miloš Ranđelović Mar 19 '14 at 23:12
  • `HashSet` or `LinkedHashSet` are working fine. – Braj Mar 19 '14 at 23:39
  • Hmm... that doesn't really give me any advantage over the ArrayList, except for removing duplicates. The HashSet doesn't guarantee iteration order, and the LinkedHashSet only keeps insertion order. I need to often use an Iterator to go through my elements, and potentially remove them, if I re-pack the collection into a sorted TreeSet that doesn't really help me much... – Miloš Ranđelović Mar 20 '14 at 00:12
  • There is no need of iterating it because it is ordered by ``. See updates in my answer. Its working fine. May be you are doing something wrong. – Braj Mar 20 '14 at 06:40
  • Your problem statement *I need to often use an Iterator to go through my elements, and potentially remove them* is resolved by using any of `Set` and What is harm of finally putting data back to `TreeSet` at client side. – Braj Mar 20 '14 at 06:45
  • Sorry, had been away for a few days. Anyway - ok, I _can_ find a workaround, without a doubt, but that still doesn't solve the original issue - why can't a pure TreeSet-extending-class be serialized normally, like all other collections? – Miloš Ranđelović Mar 25 '14 at 04:58