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 ex开发者_JS百科ist, throwing a runtime exception.
I don't believe valueOf can be overridden, or another method custom inserted before valueOf is called during deserialization.
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.
精彩评论