Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8143987

Serialization methods such as readResolve() not found if defined as default

XMLWordPrintable

      FULL PRODUCT VERSION :
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
      Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin paul.internal 15.0.0 Darwin Kernel Version 15.0.0: Sat Sep 19 15:53:46 PDT 2015; root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      The serialisation methods have not been updated to handle default methods yet.

      It is not possible to define the serialisation methods such as readResolve() in an interface (as a default method).

      A workaround is to define it in the class as well. E.g.:

      @Override
       public Object readResolve()
       {
           return DefaultInstancePattern.super.readResolve();
       }

      but that is what we are trying to avoid having to do by putting it in an interface.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      (1) Define a readResolve() method as default in an interface and implement that interface in a class.
      (2) Serialise/deserialize the class and the readResolve() method will never be called.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected default readResolve() from the interface to be called when deserializing a class implementing the interface.
      ACTUAL -
      The default readResolve() from the interface was not called when deserializing a class implementing the interface.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public interface InstancePattern extends Serializable {
          default Object readResolve() {
             return ReflectionUtils.getStaticInstance(this);
          }
      }

      public HelperClass implments InstancePattern {
          int i = 666;

         public void main(String[] args) {
           serializeAndDeserialize(new HelperClass());
         }

         private Serializable serializeAndDeserialize(Serializable object)
          {
              ByteArrayOutputStream bytes = new ByteArrayOutputStream();
              ObjectOutputStream objectOutputStream;
              
              try
              {
                  objectOutputStream = new ObjectOutputStream(bytes);
                  objectOutputStream.writeObject(object);
                  objectOutputStream.close();
                  
                  byte[] actualBytes = bytes.toByteArray();
                  ByteArrayInputStream byteStream = new ByteArrayInputStream(actualBytes);
                  
                  ObjectInputStream objectInputStream = new ObjectInputStream(byteStream);
                  return (Serializable) objectInputStream.readObject();
              }
              catch (IOException | ClassNotFoundException ex)
              {
                  throw new AssertionError("Something exceptional", ex);
              }
          }
      }


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

            chegar Chris Hegarty
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: