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

Lambda deserialization regression involving Enum method reference

XMLWordPrintable

    • b33
    • Verified

        In the following example, serializing a lambda and reading it back in succeeds on JDK 19 and fails on JDK 20.

        I think the culprit is JDK-8059632.

        ```
        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;
        import java.util.Arrays;
        import java.util.function.Predicate;

        @SuppressWarnings("unchecked")
        public class T {

          enum E {
            ONE,
            TWO,
            THREE
          }

          public static void main(String[] args) throws Exception {
            E e = E.ONE;
            Predicate<E> p = (Serializable & Predicate<E>) e::equals;
            try (OutputStream os = Files.newOutputStream(Paths.get("o"));
                ObjectOutputStream oos = new ObjectOutputStream(os)) {
              oos.writeObject(p);
            }
            try (InputStream is = Files.newInputStream(Paths.get("o"));
                ObjectInputStream ois = new ObjectInputStream(is)) {
              p = (Predicate<E>) ois.readObject();
            }
            Arrays.stream(E.values()).filter(p).forEachOrdered(System.err::println);
          }
        }
        ```

        $ java -fullversion
        openjdk full version "19.0.2+7-44"
        $ javac T.java
        $ java T
        $ java T
        ONE

        $ java -fullversion
        openjdk full version "20-ea+31-2311"
        $ javac T.java
        $ java T
        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:578)
                at java.base/java.io.ObjectStreamClass.invokeReadResolve(ObjectStreamClass.java:1190)
                at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2289)
                at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1760)
                at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:538)
                at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:496)
                at T.main(T.java:29)
        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:578)
                at java.base/java.lang.invoke.SerializedLambda.readResolve(SerializedLambda.java:278)
                ... 8 more
        Caused by: java.lang.IllegalArgumentException: Invalid lambda deserialization
                at T.$deserializeLambda$(T.java:11)
                at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
                ... 10 more

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

                Created:
                Updated:
                Resolved: