harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alexey Varlamov" <alexey.v.varla...@gmail.com>
Subject Re: [classlib][luni] On RI, constructor of EnumMap -- EnumMap(Class<K> keyType) throws unspecified NullPointerException
Date Tue, 15 Aug 2006 12:34:34 GMT
2006/8/15, Spark Shen <smallsmallorgan@gmail.com>:
> Alexey Varlamov 写道:
> > 2006/8/15, Spark Shen <smallsmallorgan@gmail.com>:
> >> Hi All:
> >> Constructor of EnumMap -- EnumMap(Class<K> keyType) behaves a little
> >> odd:
> >>
> >> I first synthesis an enum type with class body as below:
> >> enum color {
> >> blue{},
> >> red{},
> >> }
> >>
> >> Then constructs java.util.EnumMap(Class<K>) using this Enum type:
> >>
> >> import java.util.EnumMap;
> >>
> >>
> >> public class ConstructEnumMap {
> >> enum color {
> >> blue{},
> >> red{},
> >> }
> >>
> >> enum fruit {
> >> apple,
> >> }
> >>
> >> @SuppressWarnings("unchecked")
> >> public static void main(String[] args) {
> >> new EnumMap(fruit.apple.getClass());
> >> *new EnumMap(color.blue.getClass());*
> >> }
> >>
> >> }
> >>
> >> NullPointerException will be thrown out from the bolded line. While
> >> using enum type without a class body, no such exception
> >> will be thrown out. This behavior is unspecified on spec. IMO, this is a
> >> bug of RI.
> >>
> >> What is your opinion about this behavior?
> >>
> >
> > I have the following explanation for this behavior:
> > even though spec says the constructor should throw NPE if type is
> > null, implementation actually enforces that type is enum, like this:
> > if (!type.isEnum()) throw NPE;
> >
> > This is proved by the following fact, the compiler generated several
> > classes (ConstructEnumMap$1.class, ConstructEnumMap$color$1.class,
> > ConstructEnumMap$color$2.class) for empty implementation of color
> > items, and they are not marked ACC_ENUM. The same way, NPE is thrown
> > on any non-enum class passed to that constructor.
> Hi:
> Thank you very much for your information.
> But isn't it a little odd that Enum with body is not a Enum?
> Why making design decision like this? Will making Enum with body a Enum
> type brings any risks?

Seems this is a rhetorical question :).
Actually I think this is well-weighed decision - as long as each enum
item has unique body definition, it must be of unique class, and that
class is evidently not your enum. At most, it could be another enum
containing just single item - but this is not what you'd expect.
Just replace getClass() with getDeclaringClass() or use explicit class
literal (i.e. color.class) and be cool ;)

--
Alexey

>
> Best regards
> >
> > I consider this runtime behavior quite reasonable, except throwable
> > type - certainly IAE suits better than NPE... And this is hardly
> > compiler bug - there is special j.l.Enum.getDeclaringClass() method
> > for using in such cases.
> >
> > --
> > Regards,
> > Alexey
> >
> >> Best regards
> >>
> >> --
> >> Spark Shen
> >> China Software Development Lab, IBM
> >>
> >>
> >> ---------------------------------------------------------------------
> >> Terms of use : http://incubator.apache.org/harmony/mailing.html
> >> To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
> >> For additional commands, e-mail: harmony-dev-help@incubator.apache.org
> >>
> >>
> >
> > ---------------------------------------------------------------------
> > Terms of use : http://incubator.apache.org/harmony/mailing.html
> > To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
> > For additional commands, e-mail: harmony-dev-help@incubator.apache.org
> >
> >
>
>
> --
> Spark Shen
> China Software Development Lab, IBM
>
>
> ---------------------------------------------------------------------
> Terms of use : http://incubator.apache.org/harmony/mailing.html
> To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
> For additional commands, e-mail: harmony-dev-help@incubator.apache.org
>
>

---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Mime
View raw message