How correctly set complex property (ArrayList<POJO>) to the GWT BaseTreeModel? Serialization issues

StackOverflow https://stackoverflow.com/questions/21179484

Question

I want to pass my own POJO class via GWT rpc. This class extengs BaseTreeModel which has set(String value,X property) method.

I create many POJO objects, set their properties but on the client side, when I handle incoming array of POJO I see that only primitive types was serialized correctly. My POJO class

public class Field extends BaseTreeModel implements Serializable
{
      private static final long serialVersionUID = 17832464L;


    public Field()
    {

    }


    public Field(int id,ArrayList<Double> lons,ArrayList<Double> lats)
    {
        set("id",id); 
        set("lons",lons);
        set("lats",lats);
        System.out.println("adding polygon in field constructor "+id);
        for(int i=0;i<lons.size();i++)System.out.println(lons.get(i)+" "+ lats.get(i));
    } 
}

Debug output showed that all values of ArrayList are correct and came from DB as expected.

But problem is on client side - when I print all properties of each POJO class, only primitive property (integer id) is different for all entries, but ArrayList property is the same for all entries!

Client code:

for(Field field:fields)
        {

            System.out.println("adding polygon to map "+field.get("id"));
            ArrayList<Double> lons = (ArrayList<Double>)field.get("lons");
            ArrayList<Double> lats = (ArrayList<Double>)field.get("lats");

            for(int i=0;i<lons.size();i++)
            {
                System.out.println(lons.get(i)+" "+ lats.get(i));
             }
         }

Init debug output is correct and showed me (surely I publish fake coordinates, values doesn't matter)

adding polygon in field constructor 1

1 1

1 1

adding polygon in field constructor 2

2 2

2 2

But recieve debug output showed me

adding polygon in field constructor 1

2 2

2 2

adding polygon in field constructor 2

2 2

2 2

It looks like GWT use some global map for storing BaseTreeModel properties and last ArrayList value simply overwriting previous values when I do set("lons",lons);

But I looked through BaseTreeModel code and saw that each instance uses it's own map for stroring properties. Note, that primitive types works perfectly. Something wrong in the way I work with nested complex type.

UPD:

I still don't solve this issue, but it seems I localized it. Problem in serialization. I definetely should set some property of BaseTreeModel class, but I don't know exact property to change. When I extract data on server side right after setting it with set("lons"lons) everything is ok.

ArrayList<Double> lons = (ArrayList<Double>)field.get("lons"); // works on server side

But on the client side I saw only last ArrayList which somehow overwrited others.

Was it helpful?

Solution

Problem was solved by replacing ArrayList<Double> to Double[]. It looks like this is serialization side effects. I mean serializing primitives or arrays of primitives is available by default while serializing maps,lists and other complex data structures requires playing with properties and some magic)

Anyway, I'm interested how one can perform set("property",ComplexDataStructure) inside BaseTreeModel and I would be very grateful for clearing things up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top