PDA

View Full Version : Heterogeneous serializable containers - how to?



obender
11 Jun 2008, 10:58 AM
Let say there is a following class:


public class DataRow implements Serializable
{
public void addValue( Object o ) {
values.add( (Serializable)o );
}

public Object getValue( int idx ) {
return values.get( idx );
}

private List<Serializable> values = new ArrayList();
}



And here is the service:


public interface TestService extends RemoteService {
// Sample interface method of remote interface
DataRow getDataRow();
}

public class TestServiceImpl extends RemoteServiceServlet implements TestService {
// Implementation of sample interface method
public DataRow getDataRow()
{
DataRow dr = new DataRow();
dr.addValue( "xxx" );
dr.addValue( new Date() );

return dr;
}
}


I'm getting serialization warnings when I call the service from my code.
It is different every time but in goes somewhat like this:


...
Analyzing the fields of type 'lient.DataRow' that qualify for serialization
private java.util.List<java.io.Serializable> values
java.util.List<java.io.Serializable>
Checking type argument 0 of type 'java.util.List<E>' because it is directly exposed in this type or in one of its subtypes
java.io.Serializable
Analyzing subclasses:
java.util.LinkedHashSet<E> <-- this is different from time to time
Checking parameters of 'java.util.LinkedHashSet<E>'
Checking param 'E'
Checking type argument 0 of type 'java.util.LinkedHashSet<E>' because it is directly exposed in this type or in one of its subtypes
java.lang.Object
In order to produce smaller client-side code, 'Object' is not allowed; consider using a more specific type


Any suggestions on how to implement heterogeneous serializable containers?

gslender
11 Jun 2008, 1:00 PM
I read somewhere (can't find it now) that GWT 1.5 RC1 has issues with objects not inherriting the serializable-ness of their parent classess... its possible that is your problem.

obender
11 Jun 2008, 1:08 PM
Well I don't think it is the case here. I put a String and a Date in to this DataRow for testing.

gslender
11 Jun 2008, 1:48 PM
private List<Serializable> values = new ArrayList();

isn't that an object that you are relying on to inherrit Serializable to the ArrayList?

obender
11 Jun 2008, 3:01 PM
I'm not sure I'm following you on this one.
Btw, I've tried to replace List<Serializable> with ArrayList<Serializable> but the issue still present.

jraue
14 Aug 2008, 3:33 AM
Can only offer my vague guess: By using List<Serializable>, you allow any object of type Object in the list that fullfills Serializable. I would assume the warnings would disappear if you instead use a different class that is more precise that Object, and have that class implement Serializable.

darrellmeyer
18 Aug 2008, 10:14 PM
jraue is correct. GWT is telling you that using Object or Serializable as a field type or method parameter is a bad idea as the GWT compiler would have to create serialization code for every Serializable type which would make your code size unnecessarily large. Consider using a empty tag interface to mark classes as an alternative to Object or Serializable.