简体   繁体   English

Java枚举和valueOf的反序列化

[英]Java de-serialization of enums and valueOf

I've got a distributed system with a serializable enum class with constants that might vary across the system. 我有一个具有可序列化的枚举类的分布式系统,该枚举类的常量在整个系统中可能有所不同。

Because these classes may be different, valueOf could potentially be called upon deserialization on a constant that doesn't exist, throwing a runtime exception. 由于这些类可能不同,因此可能需要在不存在的常量反序列化时调用valueOf,从而抛出运行时异常。

I don't believe valueOf can be overridden, or another method custom inserted before valueOf is called during deserialization. 我不相信valueOf可以被覆盖,或者在反序列化期间在valueOf之前调用的另一种方法自定义插入。

Is there a way to avoid this problem with enums? 有没有办法避免枚举出现此问题?

The main problem is the fact that you have enum with inconsistant content on your distributed system. 主要问题是您的分布式系统上的枚举内容不一致。 The best thing to do is looking at this first. 最好的办法是首先看一下。

A palliative would be to use a readObject() method in your sensitive classes and to catch exceptions dues to bad enum values. 一种缓解方法是在敏感类中使用readObject()方法,并由于枚举值错误而捕获异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM