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 09:29:48 GMT
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.

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


Mime
View raw message