Strange serialization behavior with HashMap

Consider the following three classes:

  • EntityTransformer contains a map linking Entity to a string
  • Entity is an object containing an identifier (used by equals / hashcode) and containing a reference to EntityTransformer (note the circular dependency)
  • SomeWrapper contains an EntityTransformer and supports a Map binding that binds Entity identifiers to the corresponding Entity object .

The following code will create EntityTransformer and Wrapper, add two objects to Wrapper, serialize it, deserialize and check for two attributes:

public static void main(String[] args)
    throws Exception {

    EntityTransformer et = new EntityTransformer();
    Wrapper wr = new Wrapper(et);

    Entity a1 = wr.addEntity("a1");  // a1 and a2 are created internally by the Wrapper
    Entity a2 = wr.addEntity("a2");

    byte[] bs = object2Bytes(wr);
    wr = (SomeWrapper) bytes2Object(bs);

    System.out.println(wr.et.map);
    System.out.println(wr.et.map.containsKey(a1));
    System.out.println(wr.et.map.containsKey(a2));
}

Conclusion:

{a1 = whatever-a1, a2 = whatever-a2}

True

, , . Entity EntityTransformer, , EntityManager Entity, .

1: , , ?

: , Wrapper, ...??

2: - , ?

Bellow - , :

:)

public class SerializeTest {

public static class Entity
        implements Serializable
 {
    private EntityTransformer em;
    private String id;

    Entity(String id, EntityTransformer em) {
        this.id = id;
        this.em = em;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Entity other = (Entity) obj;
        if ((this.id == null) ? (other.id != null) : !this.id.equals(
            other.id)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    public String toString() {
        return id;
    }
}

public static class EntityTransformer
    implements Serializable
{
    Map<Entity, String> map = new HashMap<Entity, String>();
}

public static class Wrapper
    implements Serializable
{
    EntityTransformer et;
    Map<String, Entity> eMap;

    public Wrapper(EntityTransformer b) {
        this.et = b;
        this.eMap = new HashMap<String, Entity>();
    }

    public Entity addEntity(String id) {
        Entity e = new Entity(id, et);
        et.map.put(e, "whatever-" + id);
        eMap.put(id, e);

        return e;
    }
}

public static void main(String[] args)
    throws Exception {
    EntityTransformer et = new EntityTransformer();
    Wrapper wr = new Wrapper(et);

    Entity a1 = wr.addEntity("a1");  // a1 and a2 are created internally by the Wrapper
    Entity a2 = wr.addEntity("a2");

    byte[] bs = object2Bytes(wr);
    wr = (Wrapper) bytes2Object(bs);

    System.out.println(wr.et.map);
    System.out.println(wr.et.map.containsKey(a1));
    System.out.println(wr.et.map.containsKey(a2));
}



public static Object bytes2Object(byte[] bytes)
    throws IOException, ClassNotFoundException {
    ObjectInputStream oi = null;
    Object o = null;
    try {
        oi = new ObjectInputStream(new ByteArrayInputStream(bytes));
        o = oi.readObject();
    }
    catch (IOException io) {
        throw io;
    }
    catch (ClassNotFoundException cne) {
        throw cne;
    }
    finally {
        if (oi != null) {
            oi.close();
        }
    }

    return o;
}

public static byte[] object2Bytes(Object o)
    throws IOException {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oo = null;
    byte[] bytes = null;
    try {
        baos = new ByteArrayOutputStream();
        oo = new ObjectOutputStream(baos);

        oo.writeObject(o);
        bytes = baos.toByteArray();
    }
    catch (IOException ex) {
        throw ex;
    }
    finally {
        if (oo != null) {
            oo.close();
        }
    }

    return bytes;
}
}

, : http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4957674

, HashMap readObject(), , hashCode() , , .

( ) , --- hashmap:

  •   2. Deserialize HashMap ( )           2-1. HashMap           2-2.           2-3. hashCode()    2b.

2a-3 2b, hashCode() , .

, , HashMap Wrapper EntityTransformer.

+5
3

. Java Serialization , .

AWT, Component (Entity) Container (EntityTransformer). AWT , Component transient.

transient Container parent;

, Component , Container.readObject :

    for(Component comp : component) {
        comp.parent = this;
+4

,

Map<Entity, String> map = new HashMap<>(wr.et.map);
System.out.println(map.containsKey(a1));
System.out.println(map.containsKey(a2));

- .

:

for( Entity a : wr.et.map.keySet() ){
    System.out.println(a.toString());
    System.out.println(wr.et.map.containsKey(a));
}

:

a1
false
a2
true

, . , - . , , .

+3

, , ?

It seems to be pretty trivial to find the EntityTransformer hash key when serializing and using this value instead (perhaps provide a value in a structure called parentKey) and nullify the link. Then, upon reinitialization, you will find the EntityTransformer associated with this key value and assign its reference.

0
source

All Articles