反序列化ClassCastException

反序列化ClassCastException

本文介绍了序列化/反序列化ClassCastException:x无法转换为java.io.ObjectStreamClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java的本机序列化,间歇性地看到ClassCastExceptions

  java.lang.ClassCastException:myCompany.MyClass $ MembershipServiceMethod无法被强制转换为java.io.ObjectStreamClass 

或(不那么频繁)

  java.lang.ClassCastException:无法将java.lang.String强制转换为java.io.ObjectStreamClass 


。也就是说,总是为特定的序列化表示形式引发异常,但是大多数对象可以成功地进行序列化和反序列化。

  public最终类ServiceInteractionImpl实现ServiceInteraction,可序列化{
private static final long serialVersionUID = 1L;

私人最终InteractionSource来源;
私人最终长启动时间;
私人最终InteractionType类型;
私有最终ServiceMethod方法;
私人决赛的长期期限;
私有最终String伴随消息;

public ServiceInteractionImpl(final ServiceMethod方法,
最终InteractionSource源,
最终InteractionType类型,
最终长startTime,
最终长持续时间,
最后的String伴随消息){
this.source = source;
this.startTime = startTime;
this.duration =持续时间;
this.type = type;
this.method =方法;
this.accompanyingMessage = furnishedMessage;
}

...

吸气剂和规范方法
}

其中InteractionSource,InteractionType和ServiceMethod是枚举。
我无法在运行junit测试以对数百万个对象进行序列化和反序列化的本地环境中复制此代码。



这里是编写代码

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

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

final OutputStream fileOutputStream = new FileOutputStream(recordFileName,true);
最终OutputStream缓冲区= new BufferedOutputStream(fileOutputStream);

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

int localSerializedInteractionsCount = 0;

尝试{
for(最终ServiceInteraction交互:tempCollection){
output.writeObject(interaction);
output.flush();
localSerializedInteractionsCount ++;
rollBackCollection.remove(interaction);
}
}最后{
output.close();
serializedInteractionCount + = localSerializedInteractionsCount;
}
} catch(IOException e){
LOGGER.error(无法写入文件,e);
//一些回滚代码
}最后{
fileLock.unlock();
}

请参见



任何帮助都是值得的。

解决方案

如果在不更新serialVersionUID的情况下更改了类定义,则可能是这些错误来自基于旧类定义的陈旧对象实例。


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.

解决方案

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

这篇关于序列化/反序列化ClassCastException:x无法转换为java.io.ObjectStreamClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:24