为什么序列化包含循环自引用的 lambda 表达式的顺序很重要并且可能引发 ClassCastException?
当反序列化 lambda 表达式(通过其表达式参数对其自身包含循环引用)时,java8 会抛出 ClassCastException 。
仅当 lambda 在创建循环引用的对象之前序列化时才会引发异常。
在 openjdk 版本“1.8.0_292”和“11.0.14.1”上测试。请查找有关如何重现的测试代码:
import java.io.*;
public class TestLambdaSerialization
{
interface SerRunnable extends Runnable, Serializable
{
}
static class A implements Serializable
{
SerRunnable runnable;
A()
{
runnable = () -> System.out.println(this);
}
}
public static void main(String args[]) throws Exception
{
System.out.println(System.getProperty("java.version"));
A a = new A();
serializeDeserialize(a);
serializeDeserialize(a.runnable);
}
public static void serializeDeserialize(Object o1) throws Exception
{
System.out.printf("Starting object %s%n", o1);
try (ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(outBytes)) {
out.writeObject(o1);
out.flush();
try (ByteArrayInputStream inBytes = new ByteArrayInputStream(outBytes.toByteArray());
ObjectInputStream in = new ObjectInputStream(inBytes)) {
Object o2 = in.readObject();
System.out.printf("Deserialized object %s%n", o2);
}
catch (ClassCastException e)
{
e.printStackTrace();
}
}
}
}
输出:
1.8.0_292
Starting object TestLambdaSerialization$A@b4c966a
Deserialized object TestLambdaSerialization$A@7106e68e
Starting object TestLambdaSerialization$A$$Lambda$1/1480010240@34a245ab
java.lang.ClassCastException: cannot assign instance of java.lang.invoke.SerializedLambda to field TestLambdaSerialization$A.runnable of type TestLambdaSerialization$SerRunnable in instance of TestLambdaSerialization$A
at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2301)
at java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1431)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2411)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2329)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2187)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1667)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:2093)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1655)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2405)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2329)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2187)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1667)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:503)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:461)
at TestLambdaSerialization.serializeDeserialize(TestLambdaSerialization.java:36)
at TestLambdaSerialization.main(TestLambdaSerialization.java:24)
对象 a 的反序列化不会引发异常,当 lambda 表达式为起点时,会引发 ClassCastException
。
lambda 表达式的序列化顺序重要吗?如果是,除了避免 lambda(用匿名内部类替换它们)之外,还有其他解决方法吗?
ClassCastException
is thrown by java8 when a lambda expression which contains a cirular reference via its expression arguments to itself, is deserialized.
The exception only is thrown when the lambda is serialized before the object creating the circular reference.
Tested on openjdk version "1.8.0_292" and "11.0.14.1". Please find test code on how to reproduce:
import java.io.*;
public class TestLambdaSerialization
{
interface SerRunnable extends Runnable, Serializable
{
}
static class A implements Serializable
{
SerRunnable runnable;
A()
{
runnable = () -> System.out.println(this);
}
}
public static void main(String args[]) throws Exception
{
System.out.println(System.getProperty("java.version"));
A a = new A();
serializeDeserialize(a);
serializeDeserialize(a.runnable);
}
public static void serializeDeserialize(Object o1) throws Exception
{
System.out.printf("Starting object %s%n", o1);
try (ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(outBytes)) {
out.writeObject(o1);
out.flush();
try (ByteArrayInputStream inBytes = new ByteArrayInputStream(outBytes.toByteArray());
ObjectInputStream in = new ObjectInputStream(inBytes)) {
Object o2 = in.readObject();
System.out.printf("Deserialized object %s%n", o2);
}
catch (ClassCastException e)
{
e.printStackTrace();
}
}
}
}
Output:
1.8.0_292
Starting object TestLambdaSerialization$A@b4c966a
Deserialized object TestLambdaSerialization$A@7106e68e
Starting object TestLambdaSerialization$A$Lambda$1/1480010240@34a245ab
java.lang.ClassCastException: cannot assign instance of java.lang.invoke.SerializedLambda to field TestLambdaSerialization$A.runnable of type TestLambdaSerialization$SerRunnable in instance of TestLambdaSerialization$A
at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2301)
at java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1431)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2411)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2329)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2187)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1667)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:2093)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1655)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2405)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2329)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2187)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1667)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:503)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:461)
at TestLambdaSerialization.serializeDeserialize(TestLambdaSerialization.java:36)
at TestLambdaSerialization.main(TestLambdaSerialization.java:24)
Deserialization of object a doesn't throw the exception, when the lambda expression is the starting point a ClassCastException
is thrown.
Does the order of serialization in case of lambda expressions matter? If yes is there a workaround beside avoiding lambda (replacing them by anonymous inner class)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论