Re: Java 1.5 enums - ClassCastException

From: Ryan Holmes (rya..yperstep.com)
Date: Wed Feb 01 2006 - 04:34:16 EST

  • Next message: emre.yilma..r.net: "Data View Modeller"

    Works for me - the only problem is that the generated superclass has a
    compile error according to Eclipse: "The nested type ...User$UserRole
    cannot be referenced using its binary name". This was for a regular,
    non-static inner class. When I made the inner class static (out of
    curiosity), the Eclipse error changed to a "... cannot be resolved to a
    type" error. Note that I'm using JDK 1.5 compliance.

    Of course, if your change means the modeler can always take a FQN rather
    than a binary name, this is a non-issue. And in the meantime, I think I
    can handle changing a few $'s to .'s in the generated source or
    vice-versa in the mapping file ;)

    Some interesting discussions regarding binary names vs FQNs :
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4378381
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4628117

    Thanks for your help!

    -Ryan

    Andrus Adamchik wrote:
    > In fact this wasn't a bug, just a misunderstanding. See my comment
    > attached to the Jira issue:
    >
    > http://objectstyle.org/jira/secure/ViewIssue.jspa?key=CAY-433
    >
    > ...If the inner Enum class name is entered in the Modeler as
    > mypackage.MyClass$MyEnum (notice the "$" sign), Cayenne would happily
    > take it. Still I relaxed the rules adding support for inner classes
    > mapped with "." instead of "$"
    >
    > So, you can use inner enums right away without waiting for the next
    > milestone where the dots are supported.
    >
    > Andrus
    >
    >
    >
    > On Jan 30, 2006, at 1:46 AM, Ryan Holmes wrote:
    >
    >> That was the problem. Thanks for the answer and for opening a bug
    >> report.
    >>
    >> Support for inner enum classes would be nice to have since I think
    >> it's a fairly common idiom (a project at work has at least 100 of
    >> them and moving them to top-level classes would not go over well).
    >> Anyway, Cayenne's automatic enum support is fantastic and moving
    >> enums into regular classes is no problem for the project I'm working on.
    >>
    >> -Ryan
    >>
    >>
    >> Andrus Adamchik wrote:
    >>> Ryan,
    >>>
    >>> What I discovered when testing your case is that Cayenne can't
    >>> handle an Enum which is an inner class. It has to be in its own
    >>> top-level class. Once I moved it to the top, everything started to
    >>> work.
    >>>
    >>> While I was getting a different error (ClassNotFoundException), I
    >>> think the reason may be the same. I'll open a bug report regarding
    >>> Enums as inner classes and see what we can do. In the meantime,
    >>> could you please try the fix I described above.
    >>>
    >>> Andrus
    >>>
    >>>
    >>> On Jan 29, 2006, at 7:27 PM, Ryan Holmes wrote:
    >>>
    >>>> I'm trying to use JDK 1.5 enums in Cayenne 1.2M10 (Windows modeler)
    >>>> with no luck so far. A ClassCastException is thrown when invoking
    >>>> the superclass getter for the enum property.
    >>>>
    >>>> My enum type (com.hyperstep.bulldog.domain.User.UserStatus) is
    >>>> specified as the "Java Type" in the ObjEntity attribute. The
    >>>> corresponding db attribute is a VARCHAR. The relevant parts of the
    >>>> generated superclass look like this:
    >>>>
    >>>> public class _User extends org.objectstyle.cayenne.CayenneDataObject {
    >>>> ...
    >>>> public static final String STATUS_PROPERTY = "status";
    >>>> ...
    >>>> public void
    >>>> setStatus(com.hyperstep.bulldog.domain.User.UserStatus status) {
    >>>> writeProperty("status", status);
    >>>> }
    >>>> public com.hyperstep.bulldog.domain.User.UserStatus getStatus() {
    >>>> return
    >>>> (com.hyperstep.bulldog.domain.User.UserStatus)readProperty("status");
    >>>> }
    >>>> ...
    >>>> }
    >>>>
    >>>> UserStatus is a simple enum inside my User domain class:
    >>>>
    >>>> public class User extends _User {
    >>>> ...
    >>>> public static enum UserStatus {
    >>>> REGISTERED, AUTHORIZED, LOCKED_OUT, BANNED, DELETED
    >>>> }
    >>>> ...
    >>>> }
    >>>>
    >>>> setStatus() works fine and the correct UserStatus name is saved to
    >>>> the db. The ClassCastException occurs after fetching the object and
    >>>> calling getStatus():
    >>>>
    >>>> java.lang.ClassCastException: java.lang.String
    >>>> at
    >>>> com.hyperstep.bulldog.domain.auto._User.getStatus(_User.java:150)
    >>>>
    >>>> I'm probably missing something really simple here, but I'm new to
    >>>> Cayenne and not sure what to try next.
    >>>> FYI, I noticed that the modeler is using version 1.1 templates and
    >>>> I don't know how to specify 1.2 templates or if that's even related
    >>>> to the enum problem.
    >>>
    >>>
    >>
    >>
    >
    >



    This archive was generated by hypermail 2.0.0 : Wed Feb 01 2006 - 04:34:26 EST