views:

36

answers:

1

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.

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?

+5  A: 

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.

Colin Hebert
Thanks -- I'm reading up on readObject() right now -- this is called before valueOf upon deserial?
Michael
This is a method used to rewrite the way your object is deserialized.
Colin Hebert
Would this be a problem? "The process by which enum constats are serialized cannot be customized: any class-specific writeObject, readObject... defined by enum types are ignored during serialization and desrialization" http://download.oracle.com/javase/6/docs/platform/serialization/spec/serial-arch.html
Michael
Or is what you are saying -- override the readObject in the classes that use the enums
Michael
And one other thing! The spring framework which I am using handles the deserialization -- so I may have to modify how the class loader handles this?
Michael