[concurrency-interest] Backport: serialization bug in Collections

Xavier Le Vourch xavier at brittanysoftware.com
Thu Aug 24 16:54:19 EDT 2006


I've just used FindBugs on my code and it shows an error in Collections:

=============
Se: Class is Serializable but its superclass doesn't define a void
constructor

This class implements the Serializable interface and its superclass does
not. When such an object is deserialized, the fields of the superclass
need to be initialized by invoking the void constructor of the
superclass. Since the superclass does
 not have one, serialization and deserialization will fail at runtime.
=============


The test case below works with the 5.0 Collections class but fails with
the backport version.

When I made the CheckedMap class implement Serializable, the code worked
and all tests in the test suite still passed.



=============
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import edu.emory.mathcs.backport.java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class Bug {

  public static void main(String args[]) throws Exception {
    SortedMap<String, String> m = new TreeMap<String, String>();

    m = Collections.checkedSortedMap(m, String.class, String.class);

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(bytes);

    os.writeObject(m);
    os.close();

    Object o = new ObjectInputStream(
        new ByteArrayInputStream(bytes.toByteArray())).readObject();

    System.out.println("Type: " + o.getClass());
  }
}
=============



-- 
Xavier Le Vourch
Brittany Software, Inc.
<xavier at brittanysoftware.com>

PGP Key: http://brittanysoftware.com/gpg_key.asc


More information about the Concurrency-interest mailing list