Ken,
I have not worked with this material before, but I've just been reading Bloch's description of typesafe enums, Item 21 in <http://java.sun.com/developer/Books/effectivejava/Chapter5.pdf>.
One thing I notice Bloch says is that a String such as your String myValue "can and should be made transient" (page 107), and it seems you have not done this -- if that would matter.
Also I notice that your usage differs from the pattern Bloch shows in that you evidently have a non-private constructor in your Code class, since I see expressions like 'new Code(0,"Exception")' in your surrounding OperationResult class. Bloch's example, to the extent that I understand it, would place your constants like
public static final Code EXCEPTION = new Code(0,"Exception");
inside the Code class, but not outside in the OperationResult class.
Rich Hammer
Kenneth Sizer wrote:
The context:
I have a serializable class. It has 3 attributes. When deserialized on the other end of an RMI connection, 2 of the attributes are intact, one is null. The one that is lost is a typesafe enum (al la Bloch) and a static inner class.
I'm assuming that either: (a) I'm simply ignorant of something obvious and stupid (e.g., a private instance variable with a name ending in "e" is implicitly transient), or (b) There is something clever and subtle about the combination of serialization, typesafe enums and static inner classes.
Either way, if anyone out there has fallen into this same pit, I'd appreciate a word of wisdom about now.
Thanks, Ken
P.S.
Sample code follows...
public class OperationResult implements Serializable {
// constants
public static final Code EXCEPTION = new Code(0,"Exception"); public static final Code FILTER_DENY = new Code(1,"Filter Deny"); public static final Code NOT_EMPTY = new Code(2,"Not Empty");
// attributes private String myPathUrlStr; // works fine
private Code myCode; // is always null on deserialization! private Exception myException; // works fine :
// getters, toString and such
:
// typesafe enum inner class
public static final class Code implements Serializable {
private static final Code[] PRIVATE_VALUES = { EXCEPTION, FILTER_DENY, NOT_EMPTY };
private final int myIndex; private final String myValue;
: :
private Object readResolve() throws java.io.ObjectStreamException {
Object retVal = PRIVATE_VALUES[myIndex]; return retVal;
}
}
}
_______________________________________________ Juglist mailing list [EMAIL PROTECTED] http://trijug.org/mailman/listinfo/juglist_trijug.org
_______________________________________________ Juglist mailing list [EMAIL PROTECTED] http://trijug.org/mailman/listinfo/juglist_trijug.org
