Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#744 enums can't appear in EnumSets

4: Serious
open
Dan Smith
5
2009-03-19
2009-03-19
Dan Smith
No

Due to the way DynamicJava-defined enums are implemented, the EnumSet API class doesn't recognize them as enums and throws an exception when trying to create a set of that type.

> enum Foo { A, B, C};
> EnumSet set
> set = EnumSet.allOf(Foo.class)
java.lang.ClassCastException: class Foo not an enum
at java.util.EnumSet.noneOf(EnumSet.java:93)
at java.util.EnumSet.allOf(EnumSet.java:110)
>

Also got this type error, which should be looked at more closely:

> EnumSet<Foo> set
Static Error: Type arguments are invalid

Discussion

  • Mathias Ricken
    Mathias Ricken
    2009-08-10

    Still exists in revision 4978 (except the second error is "Static Error: Type has incompatible bounds").

     
  • Mathias Ricken
    Mathias Ricken
    2010-04-10

    In 5211, I'm getting

    Welcome to DrJava. Working directory is /home/mgricken/temp
    > enum Foo { A, B, C};
    > EnumSet set
    Static Error: Undefined class 'EnumSet'
    > import java.util.EnumSet; // auto-import
    EnumSet set
    > set = EnumSet.allOf(Foo.class)
    Static Error: No method in static raw EnumSet with name 'allOf' matches this invocation
    Arguments: (Class<Foo>)
    Expected return type: raw EnumSet
    Candidate signatures: <E> EnumSet<E> allOf(Class<E>) [E <: Enum<E>]
    > EnumSet<Foo> set2
    > set2 = EnumSet.allOf(Foo.class)
    java.lang.ClassCastException: class Foo not an enum
    at java.util.EnumSet.noneOf(EnumSet.java:93)
    at java.util.EnumSet.allOf(EnumSet.java:110)
    > Foo.class.getSuperclass()
    class java.lang.Enum
    > Foo.class.isEnum()
    false
    >

    Since Class.isEnum() returns false (which does a this.getModifiers() & ENUM) != 0, and getModifiers() is native), it will be difficult to implement this.