JDK-6431617 : (coll) AbstractList$Itr is not serializable
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2006-05-30
  • Updated: 2012-10-08
  • Resolved: 2007-07-02
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.4.2_09"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_09-b05)
Java HotSpot(TM) Client VM (build 1.4.2_09-b05, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux ******* 2.4.20-46.7.legacysmp #1 SMP Sat Mar 4 14:58:59 EST 2006 i686 unknown

A DESCRIPTION OF THE PROBLEM :
Attempt to serialize an object with an instance of java.util.AbstractList$Itr reachable from it results in a java.io.NotSerializableException

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(new ArrayList().iterator());

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
silently succeed

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.io.NotSerializableException: java.util.AbstractList$Itr

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Test
{
    public static void main(String[] args) throws IOException
    {
        new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(new ArrayList().iterator());
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Store the list and an index instead of the iterator. This workaround doesn't work well with sequentially accessible lists, and breaks down completely with nonlist collections though.

Comments
EVALUATION See the evaluation of 4782922: ArrayList.subList() does not return a serializable object Iterators are designed to have a transient existence, and not to be serializable. If serializing them was possible, the entire backing collection would have to be serialized as well. Non-view Collections have more permanence, and are generally serializable.
02-07-2007

EVALUATION Iterators are not intended to be serializable. Probably.... Not a Defect.
30-05-2006