Maverick
unread,Sep 15, 2011, 6:23:12 AM9/15/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Google Web Toolkit
I'm having a strange behavior (at least, something that I don't
understand) during derialization/deserialization, which causes values
to be duplicated to a List.
I have a custom Map that basically adds key order preserving through
an ArrayList. Each time a key/value pair is put into the map, the key
is also added to an ArrayList. When a class instance is passed through
a RPC the map is serialized correctly (or at least the data is the
same before and after); but the list seems to be deserialized twice
and two copies of the keys exist after.
In the code below, I instantiate the CustomMap and add the key "10"; I
expect to have one key received on the server and one key sent back to
the client. However, the output is the following:
Before sending: [10]
On server: [10, 10]
Received back: [10, 10, 10]
Am I missing some GWT programming principle?
How could I implement such feature without suffering that problem?
Thanks
Here the code:
/* The custom map; to every put, the key is also added to the list.
*/
public class CustomMap<K, V> extends HashMap<K, V> implements
Serializable, IsSerializable
{
private static final long serialVersionUID = -1780691944208423396L;
ArrayList<K> list = new ArrayList<K>();
public CustomMap()
{
}
@Override
public V put( K key, V value )
{
return put( key, value, this.list.size() );
}
public V put( K key, V value, int index )
{
this.list.add( index, key );
return super.put( key, value );
}
public V get( int n )
{
return get( key( n ) );
}
public int indexOf( K key )
{
return this.list.indexOf( key );
}
public K key( int n )
{
return this.list.get( n );
}
public ArrayList<K> getData()
{
return this.list;
}
}
/* The RPC call; just send a CustomMap forth and back
*/
public void onModuleLoad()
{
CustomMap<Long, String> map = new CustomMap<Long, String>();
map.put( new Long( 10 ), "a value" );
System.out.println( "Before sending: " + map.getData() );
service.transfer( map, new AsyncCallback<CustomMap<Long, String>>(){
@Override
public void onSuccess( CustomMap<Long, String> result )
{
System.out.println( "Received back: " + result.getData() );
}
@Override
public void onFailure( Throwable caught ) {}
} );
}
/* Server-side code, for completeness
*/
public CustomMap<Long, String> transfer( CustomMap<Long, String>
map )
{
System.out.println( "On server: " + map.getData() );
return map;
}