You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@harmony.apache.org by Spark Shen <sm...@gmail.com> on 2006/08/15 05:50:09 UTC

[classlib][luni] On RI, constructor of EnumMap -- EnumMap(Class keyType) throws unspecified NullPointerException

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?

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


Re: [classlib][luni] On RI, constructor of EnumMap -- EnumMap(Class keyType) throws unspecified NullPointerException

Posted by Alexey Varlamov <al...@gmail.com>.
2006/8/15, Spark Shen <sm...@gmail.com>:
> Alexey Varlamov 写道:
> > 2006/8/15, Spark Shen <sm...@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


Re: [classlib][luni] On RI, constructor of EnumMap -- EnumMap(Class keyType) throws unspecified NullPointerException

Posted by Spark Shen <sm...@gmail.com>.
Alexey Varlamov 写道:
> 2006/8/15, Spark Shen <sm...@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?

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


Re: [classlib][luni] On RI, constructor of EnumMap -- EnumMap(Class keyType) throws unspecified NullPointerException

Posted by Alexey Varlamov <al...@gmail.com>.
2006/8/15, Spark Shen <sm...@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