java - 方法被重写后,类不可序列化

标签 java serialization overriding

我在测试用例中重写了 createSocket() 方法,以将其传递到模拟的 Socket 中。执行此操作后,对象将不再可序列化。

下面是一个不起作用的示例。

Foo.java

import java.io.Serializable;

public class Foo implements Serializable {
    private static final long serialVersionUID = 3109852436898487119L;

    public void bar() {
        System.out.println("Foo");
    }
}

FooTest.java

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import junit.framework.TestCase;

import org.junit.Test;

public class FooTest extends TestCase {

    // this passes
    @Test
    public void testFooIsSerializable() throws IOException {
        Foo foo = new Foo();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(baos);
        out.writeObject(foo);
    }

    // this throws a java.io.NotSerializableException
    @Test
    public void testFooIsStillSerializableAfterBarIsOverridden()
            throws IOException {

        // Eclipse gives me the warning "The serializable class  does not declare a static final serialVersionUID field of type long"
        // Adding it doesn't help
        Foo foo = new Foo() {
            @Override
            public void bar() {
                System.out.println("Bar");
            }
        };

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(baos);
        out.writeObject(foo);
    }
}

使用 JUnit 运行 FooTest 时的堆栈跟踪:

java.io.NotSerializableException: FooTest
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
    at FooTest.testFooIsStillSerializableAfterBarIsOverridden(FooTest.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

这有什么问题吗?我必须承认,我并没有深入研究 Java 的 Serialized 接口(interface),或多或少只是遵循了 Eclipse 的快速修复。

更具体地说明我的实现:

我有一个类应该通过 ObjectOutputStream 发送其自身的实例。

这是一种根本错误的方法吗?

最佳答案

问题是您有一个匿名内部类,根据定义它包含对创建它的外部类实例 (FooTest) 的(编译器生成的)引用。由于默认情况下序列化包括对象的所有成员,因此运行时也会尝试序列化 FooTest 对象,但该对象不可序列化(因为它本来就不应该序列化)。错误消息证明了这一点:

java.io.NotSerializableException: FooTest

因此,如果您将匿名类转变为显式静态内部类,问题就会消失。

关于java - 方法被重写后,类不可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6662529/

相关文章:

php - 无法覆盖 magento 核心配置模型

java - 管理泛型外部类及其内部泛型内部类实例化以及声明各自引用的规则是什么?

Java - Maven - 在表单中设置图标图像

java - 如何在android中为日历事件设置多个闹钟?

python - Cython 重载 "no suitable method found"

css - 如何在子主题中覆盖简单的数字下载 css 文件?

java - 获取另一种语言的 JSON Google Directions 输出?

java - 序列化会保存父类(super class)字段吗?

java - 在同一个文件中序列化多个对象

DjangoRestFramework ModelSerializer DateTimeField仅在创建对象时转换为当前时区