Announcement Announcement Module
Collapse
No announcement yet.
Spring enums and serialization Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring enums and serialization

    Hi.

    I need enums support for a project on jdk 1.4 and looked at Spring enums. However I've found some issues:

    1. serialization support - if an enum is serialized and deserialized a new instance is created. Retrieving the same instance would be prefered as different instances of the same enum inside the same classloader don't make much sense.

    2. retrieving the declared enums - to discover the declared enums a different object (i.e. enumResolver) is required. Is it possible to encapsulate this functionality inside the enum itself ? (I know that in commons-lang each enum declaration can contain such methods but they have to be written manually by the developer).

    Are these issues going to be addressed in the future or they were not a requirement?

    Thanks.

  • #2
    Re: Spring enums and serialization

    Originally posted by costin
    1. serialization support - if an enum is serialized and deserialized a new instance is created. Retrieving the same instance would be prefered as different instances of the same enum inside the same classloader don't make much sense.
    I haven't used the enums yet. From a look it seems that the enum classes are supposed to be subclassed. In that case your subclasses could implement Serializable and define the readResolve() method to refer to an existing instance. This way duplication within the same classloader will not happen.

    Hope that helps,
    Andreas

    Comment


    • #3
      Indeed but it would be a lot better to have this feature already inside the enum base class (AbstractLabeledEnum) not to mention that I am not a Serialization expert .
      Plus I would have to mirror the current enums flavors (Short, String, Letter) and add Serialization support (either in each one or in a base class but that's just the same).

      I've seen a solution to this issue inside Effective Java but it's 'ugly' - you have to manually add the enums in a list inside the enum. The enums package from commons-lang solves this problem nicely apparently also.

      Comment


      • #4
        Originally posted by costin
        I've seen a solution to this issue inside Effective Java but it's 'ugly' - you have to manually add the enums in a list inside the enum.
        That's where I have it from :wink:
        If I recall correctly, there is also the serialization issue covered.
        I haven't used commons-lang yet. If it can be used to hide the details, that seems good. I think I'll have a look at it.

        Regards,
        Andreas

        Comment

        Working...
        X