This discussion is archived
6 Replies Latest reply: Dec 19, 2010 7:01 PM by 712739 RSS

Enums with constant-specific method implementation

753675 Newbie
Currently Being Moderated
Just faced the following problem. I have a persistent class with one of the fields of it is enum with constant-specific method implementation:
@Persistent
public class Message
{
   static public enum Type
   {
       DEFAULT
       { 
           @Override
           public String getDescription() { return "Some description"; }
       };
       public abstract String getDescription();
   }
   ...
   private Type type;
   ...
}
When I try to store the record, I get the exception:
java.lang.IllegalArgumentException: Class could not be loaded or is not persistent: messages.Message$Type$1
The problem seem to be in that compiler creates a separate class, namely Message$Type$1.class for the DEFAULT instance and this class is not known by BDB..

If I remove the constant-specific method from enum, everything's working fine (as the ..$1.class is not created by compiler).. Except the fact that I'd like to have constant-specific methods there..
Any ideas on this? Maybe it's a bad idea to create constant-specific methods if it means that each constant would get own class file (and they would bloat the storage routines)?
  • 1. Re: Enums with constant-specific method implementation
    greybird Expert
    Currently Being Moderated
    Hi Mikhail,

    I recreated the problem here and you're right, constant-specific methods aren't working. This isn't something we thought about, to be honest, or tested.

    Just based on an initial quick look I see that for the compiler generated class, the Class.isEnum method returns false, which is why we don't recognize this as an enum and eventually why we throw the exception you're seeing. But assuming that we can identify the class as an enum (that shouldn't be too difficult) I don't know what other problems we will run into in trying to support this.

    For now I think the best thing is to avoid using the constant-specific methods. I have opened a ticket (#18357) so that we'll remember to look into this in more detail and see whether it can be supported in the future.

    If we are able to support it, then I'm hoping that we won't to store extra metadata for constants that have methods. In other words, I'm hoping that we won't have to add any extra storage or processing overhead.

    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 2. Re: Enums with constant-specific method implementation
    753675 Newbie
    Currently Being Moderated
    Hello Mark,

    Thanks for the information. I think I'll use a case-based workaround for now..

    Is there a way I can monitor the ticket #18357 in the tracker system to get a notify if it's fixed? I was unable to find a link to the tracker system..

    -- mikhail
  • 3. Re: Enums with constant-specific method implementation
    greybird Expert
    Currently Being Moderated
    Hi,

    No, sorry, Oracle does not made their bug tracking systems (or source control systems) public. Asking a question on this forum is the best way to find the status.

    --mark                                                                                                                                                                                                                                                                                                                                                                       
  • 4. Re: Enums with constant-specific method implementation
    753675 Newbie
    Currently Being Moderated
    Hi again,

    Are there any news or ETA on this issue?
    It's surely not a vital problem, but when I add constants to enums I'd really like compiler to check if I implemented all the methods there, instead of trying to remember to add a case to every switch..
  • 5. Re: Enums with constant-specific method implementation
    greybird Expert
    Currently Being Moderated
    No, I'm sorry, there is no progress or ETA on this.
    --mark                                                                                                                                                                                                       
  • 6. Re: Enums with constant-specific method implementation
    712739 Newbie
    Currently Being Moderated
    Hi,

    This bug will be fixed on the next JE release.

    Thanks.

    Eric Wang
    BDB JE Team

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points