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

Regression in lambda serialization

XMLWordPrintable

    • b16

      Recent changes to lambda code generation affected how the lambda in the following example captures state, and result in a lambda that was serializable no longer being serializable.

      ```
      import java.io.ByteArrayOutputStream;
      import java.io.IOException;
      import java.io.ObjectOutputStream;
      import java.io.Serializable;
      import java.util.function.Supplier;

      public class X {

        public static void main(String[] args) throws IOException {
          F f = new X().f(42).get();
          var baos = new ByteArrayOutputStream();
          try (var oos = new ObjectOutputStream(baos)) {
            oos.writeObject(f);
          }
          System.err.println(baos.toByteArray().length);
        }

        interface F extends Runnable, Serializable {}

        Supplier<F> f(int x) {
          return new Supplier<F>() {
            @Override
            public F get() {
              return () -> System.err.println(x);
            }
          };
        }
      }
      ```

      Before:

      $ javap -v -p `X$1`
      ...
        public X$F get();
          descriptor: ()LX$F;
          flags: (0x0001) ACC_PUBLIC
          Code:
            stack=1, locals=1, args_size=1
               0: aload_0
               1: getfield #1 // Field val$x:I
               4: invokedynamic #13, 0 // InvokeDynamic #0:run:(I)LX$F;
               9: areturn
            LineNumberTable:
              line 21: 0


      After:

      $ javap -v -p `X$1`
      ...
        public X$F get();
          descriptor: ()LX$F;
          flags: (0x0001) ACC_PUBLIC
          Code:
            stack=1, locals=1, args_size=1
               0: aload_0
               1: invokedynamic #13, 0 // InvokeDynamic #0:run:(LX$1;)LX$F;
               6: areturn
            LineNumberTable:
              line 21: 0


      Before:

      $ java X.java
      569

      After:

      $ java X.java
      Exception in thread "main" java.io.NotSerializableException: X$1
      at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1201)
      at java.base/java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1395)
      at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1191)
      at java.base/java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1586)
      at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1543)
      at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1452)
      at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1195)
      at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:359)
      at X.main(X.java:13)

            mcimadamore Maurizio Cimadamore
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: