[PATCH] Simplification of TreeMap

Michael Kuhlmann jdk at fiolino.de
Thu Sep 6 07:27:01 UTC 2018


Hi Sergey and Sergey ;),

that's totally correct! Serialization really is a beast sometimes, you
have to take care that instances that have been serialized even with
Java 1.2 still get deserialized correctly in the current version.

So it's not sufficient to only look at the constructors when the class
is Serializable. In the case of TreeMap, the only solutions I see are
either making 'comparator' mutable and setting it in readObject(), or
again always check for null values when accessing it - which would make
your idea rather useless. As Martin already mentioned, "if it's not
broken, don't fix it" - either option is not worth the change.

What would make sense is to remove valEquals() in favor of
Objects::equals, so at least a minor patch would remain. ;)

-Michael


Am 05.09.2018 um 21:22 schrieb Sergey:
> Hi Sergey,
> 
> Michael might correct me if I’ve missed something, but 
> problem with your test case is that you’re serializing already 
> patched version. That makes sense if you want to test current 
> behavior. However the case you truly want to test is how your
> patched TreeMap deserializes pre-patched TreeMaps.
> 
> What you have currently just tests if patched map could be
> deserialized without any problems.
> 
> Cheers,
> su -
> 
> On Wed, 5 Sep 2018 at 20:30, Сергей Цыпанов <sergei.tsypanov at yandex.ru
> <mailto:sergei.tsypanov at yandex.ru>> wrote:
> 
>     Hi Michael,
> 
>     thanks for pointing out this serialization concern, I didn't think
>     about it at all.
> 
>     I've wrote a simple test for serialization of patched TreeMap and it
>     works without errors for both no-args constructor and constructor
>     with comparator:
> 
>     public class TreeMapSerialization {
>         public static void main(String[] args) throws Exception {
>             TreeMap<Integer, String> serialized = new
>     TreeMap<>(Comparator.reverseOrder());
>             serialized.put(1, "1");
>             serialized.put(2, "2");
> 
>             ByteArrayOutputStream baos = new ByteArrayOutputStream();
>             ObjectOutputStream oos = new ObjectOutputStream(baos);
> 
>             oos.writeObject(serialized);
>             oos.flush();
>             baos.flush();
>             oos.close();
>             baos.close();
> 
>             ByteArrayInputStream bais = new
>     ByteArrayInputStream(baos.toByteArray());
>             ObjectInputStream ois = new ObjectInputStream(bais);
> 
>             TreeMap<Integer, String> deserialized = (TreeMap<Integer,
>     String>) ois.readObject();
>             deserialized.put(3, "3");
> 
>             System.out.println(deserialized);
>         }
>     }
> 
> 
>     I hope I don't miss anything, so there shouldn't be any
>     serialization issues.
> 
>     Regards,
>     Sergey Tsypanov
> 



More information about the core-libs-dev mailing list