Serialization/deserialization ClassCastException: x cannot be cast to java.io.ObjectStreamClass

坚强是说给别人听的谎言 提交于 2020-01-04 13:24:13

问题


Using Java's native serialization, I'm intermittently seeing ClassCastExceptions

java.lang.ClassCastException: myCompany.MyClass$MembershipServiceMethod cannot be cast to java.io.ObjectStreamClass

or (less frequently)

java.lang.ClassCastException: java.lang.String cannot be cast to java.io.ObjectStreamClass

when I deserialize objects of a particular immutable class. That is to say, the exception is always thrown for particular serialized representations, but most objects can be successfully serialized and deserialized.

public final class ServiceInteractionImpl implements ServiceInteraction, Serializable {
private static final long serialVersionUID = 1L;

private final InteractionSource source;
private final long startTime;
private final InteractionType type;
private final ServiceMethod method;
private final long duration;
private final String accompanyingMessage;

public ServiceInteractionImpl(final ServiceMethod method,
                                  final InteractionSource source,
                                  final InteractionType type,
                                  final long startTime,
                                  final long duration,
                                  final String accompanyingMessage) {
            this.source = source;
            this.startTime = startTime;
            this.duration = duration;
            this.type = type;
            this.method = method;
            this.accompanyingMessage = accompanyingMessage;
    }

    ...

    getters and canonical methods
}

where InteractionSource, InteractionType and ServiceMethod are enums. I can't replicate this in my local environment running junit tests that serialize and deserialize millions of objects.

Here is the writing code

    fileLock.lock();
    try {
        final File recordFile = new File(recordFileName);
        boolean appendToFile = false;

        if (recordFile.exists()) {
            appendToFile = true;
        }

        final OutputStream fileOutputStream = new FileOutputStream(recordFileName, true);
        final OutputStream buffer = new BufferedOutputStream(fileOutputStream);

        ObjectOutput output;
        if (appendToFile) {
            output = new AppendableObjectOutputStream(buffer);
        } else {
            output = new ObjectOutputStream(buffer);
        }

        int localSerializedInteractionsCount = 0;

        try {
            for (final ServiceInteraction interaction : tempCollection) {
                output.writeObject(interaction);
                output.flush();
                localSerializedInteractionsCount++;
                rollBackCollection.remove(interaction);
            }
        } finally {
            output.close();
            serializedInteractionCount += localSerializedInteractionsCount;
        }
    } catch (IOException e) {
        LOGGER.error("could not write to file", e);
        //some roll-back code
    } finally {
        fileLock.unlock();
    }

see Appending to an ObjectOutputStream for the AppendableObjectOutputStream

Any help much appreciated.


回答1:


If the class definition changed without updating serialVersionUID then its possible these errors are from stale object instances based on the old class definition.



来源:https://stackoverflow.com/questions/10036603/serialization-deserialization-classcastexception-x-cannot-be-cast-to-java-io-ob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!