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

Lambda deserialization fails for Object method references on interfaces

XMLWordPrintable

    • b23
    • Verified

        The example serializes and deserializes a lambda for an Object method reference on an interface.

        This works for JDK 17 and earlier, but fails with JDK 18.

        The regression was introduced by https://bugs.openjdk.java.net/browse/JDK-8272564


        ```
        import java.io.InputStream;
        import java.io.ObjectInputStream;
        import java.io.ObjectOutputStream;
        import java.io.OutputStream;
        import java.io.Serializable;
        import java.nio.file.Files;
        import java.nio.file.Paths;

        public class X {
          interface I extends Serializable {}

          interface F<T, R> extends Serializable {
            R apply(T t);
          }

          @SuppressWarnings("unchecked")
          public static void main(String[] args) throws Exception {
            F<I, Integer> f = I::hashCode;
            try (OutputStream os = Files.newOutputStream(Paths.get("o"));
                ObjectOutputStream oos = new ObjectOutputStream(os)) {
              oos.writeObject(f);
            }
            try (InputStream is = Files.newInputStream(Paths.get("o"));
                ObjectInputStream ois = new ObjectInputStream(is)) {
              f = (F<I, Integer>) ois.readObject();
            }
            System.err.println(f.apply(new I() {}));
          }
        }
        ```

        ```
        $ javac X.java
        $ java X
        ...
        Exception in thread "main" java.io.InvalidObjectException: ReflectiveOperationException during deserialization
                at java.base/java.lang.invoke.SerializedLambda.readResolve(SerializedLambda.java:280)
                at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
                at java.base/java.lang.reflect.Method.invoke(Method.java:577)
                at java.base/java.io.ObjectStreamClass.invokeReadResolve(ObjectStreamClass.java:1321)
                at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2301)
                at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1768)
                at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:543)
                at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:501)
                at X.main(X.java:25)
        Caused by: java.lang.reflect.InvocationTargetException
                at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
                at java.base/java.lang.reflect.Method.invoke(Method.java:577)
                at java.base/java.lang.invoke.SerializedLambda.readResolve(SerializedLambda.java:278)
                at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
                at java.base/java.lang.reflect.Method.invoke(Method.java:577)
                at java.base/java.io.ObjectStreamClass.invokeReadResolve(ObjectStreamClass.java:1321)
                at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2301)
                at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1768)
                at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:543)
                at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:501)
                at X.main(X.java:25)
                at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
                at java.base/java.lang.reflect.Method.invoke(Method.java:577)
                at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:421)
                at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:192)
                at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)
        Caused by: java.lang.IllegalArgumentException: Invalid lambda deserialization
                at X.$deserializeLambda$(X.java:9)
                at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
                ... 15 more
        ```

          There are no Sub-Tasks for this issue.

              jlahoda Jan Lahoda
              cushon Liam Miller-Cushon
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: