Lew a écrit :
> Sigfried wrote:
>> Hendrik Maryns a écrit :
>>> Sigfried schreef:
>>>> If you use name() to save an enum reference, you won't be able to
>>>> refactor the java [sic] names of the enum constants. So i would
>>>> advice to
>>>> assign an int to each enum constant and serialize that int.
>>>>
>>>> What do you think ?
>>>
>>> Enums are serializable, so why bother? But see
>>> http://forums.sun.com/thread.jspa?th...sageID=4264687
>>
>> The point of your post is: use serialisation but look, serialization
>> doesn't work that much ?
>
> Huh? Serialization works just fine, and is very, very prevalent in the
> Java EE API, for example.
Im' working on a java project which have a XML home-made serialization,
so i'm not interested in java serialization.
> Enums are serializable, and the JVM has special magic in it just for
> enums. Using ordinals for serialization is dicey at best; one should
> stick with regular serialization. Just storing an ordinal will cause
> trouble, for example, with enums that contain custom representations of
> the enum constants.
>
> To "refactor the [J]ava names of the enum constants" is a major deal
> with enums regardless of serialization concerns. You have to make sure
> all clients account for the new range of constants. 'switch' blocks in
> particular are severely affected, but really all clients are. You just
> don't undertake changing an enum lightly.
I was talking about the names:
enum { A, B, C }
=>
enum { One, Two, Three }
You must be talking about opened API, which is not my concern.
> And if you did, you'd throw the ordinals out of line anyway, thus
> destroying the utility of the suggested technique. Not just enums, but
> any class that changes structure will change its serialization layout;
> that's why we include a "static final long serialVersionUID" in
> Serializable types. Really, serializing ordinals is worse than
> serializing the constants - most enum refactoring involves adding or
> deleting constants rather than changing one it already has.
The ordinals would not change:
enum { A(1), B(2), C(3) }
=>
enum { One(1), Two(2), Three(3) }
I was talkink about having XML files with "A" in it, and wanting to have
"One" in the java source for the next refactoring. I just can't, or i
must add a "translator" in the XML reader.
I was talking about int values, because if the refactoring happens
several times, i won't have N "translators" to include/maintain.