Results 1 to 4 of 4

Thread: Spring enums and serialization

  1. #1
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default 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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default Re: Spring enums and serialization

    Quote 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

  3. #3
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •