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

Incorrect deserialization of a method reference with additional marker interface

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      If some class is a capturing class for several method references to the same method, method references with additional marker interfaces might be deserialized incorrectly.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the given source code.



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      This code should write the following to standard output:

      y1 IS Marker1
      y1 IS NOT Marker2
      y2 IS NOT Marker1
      y2 IS Marker2
      ACTUAL -
      Actual output is:

      y1 IS Marker1
      y1 IS NOT Marker2
      y2 IS Marker1
      y2 IS NOT Marker2

      ---------- BEGIN SOURCE ----------
      import java.io.*;

      public class Test {
          public interface IFoo extends Serializable {
              void foo();
          }

          public interface Marker1 {
          }

          public interface Marker2 {
          }

          public static void foo() {
          }

          public static void main(String[] args) throws IOException, ClassNotFoundException {
              Object x1 = (IFoo & Marker1) Test::foo;
              Object y1 = roundtrip(x1);

              Object x2 = (IFoo & Marker2) Test::foo;
              Object y2 = roundtrip(x2);

              check(y1, "y1");
              check(y2, "y2");
          }

          private static void check(Object y, String name) {
              if (y instanceof Marker1) {
                  System.out.println(name + " IS Marker1");
              } else {
                  System.out.println(name + " IS NOT Marker1");
              }

              if (y instanceof Marker2) {
                  System.out.println(name + " IS Marker2");
              } else {
                  System.out.println(name + " IS NOT Marker2");
              }
          }

          private static Object roundtrip(Object x) throws IOException, ClassNotFoundException {
              ByteArrayOutputStream out1 = new ByteArrayOutputStream();
              new ObjectOutputStream(out1).writeObject(x);
              return new ObjectInputStream(new ByteArrayInputStream(out1.toByteArray())).readObject();
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: