CopiableStructure implements Serializable { public CopiableStructure copy() { try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); @SuppressWarnings("resource") final ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); oos.flush(); final ByteArrayInputStream bin = new ByteArrayInputStream( bos.toByteArray()); @SuppressWarnings("resource") final ObjectInputStream ois = new ObjectInputStream(bin); final CopiableStructure result = (CopiableStructure) ois.readObject(); oos.close(); ois.close(); return result; } catch (final Exception e) { e.printStackTrace(); System.exit(1); } return null; } CopiableStructure(); CopiableStructure add(Serializable item); CopiableStructure copy(); @SuppressWarnings("unchecked") T get(Class<T> c);  }
@Test public void testCopy() { CopiableStructure cs2 = cs.copy(); W w2 = cs2.get(W.class); Z z2 = cs2.get(Z.class); assertNotSame(w2, w); assertSame(w2.x.z, w2.y.z); assertSame(w2.x.z, z2); }