Expose elementType of EnumSet and EnumMap

Remi Forax forax at univ-mlv.fr
Sat Sep 21 20:21:57 UTC 2013

On 09/21/2013 08:43 PM, Marcin Wiśnicki wrote:
> First of all EnumSet/EnumMap are not general purpose collections, they
> are already implementation specific.
> Also, since you can't subclass enum, class of contained values will be
> the same as declaring class.
>  From EnumSet documentation: "All of the elements in an enum set must
> come from a single enum type".

No, you can add methods to one value of an enum using the anonymous 
class syntax.

> I don't see how exposing getElementType() can hinder reification. I'm
> going to say that it doesn't. Please provide counterexample if you
> think I'm mistaken.

elementType and the reified type are the same information.
by the way, elementType and the class of the array (universe) are also 
the same information.

> Here are your use cases:
> 1. Remove ugly hacks from serialization libraries that were known to
> cause problems with different JVM implementations in the past (for
> example see this code from jackson: http://goo.gl/yudBps)

I've taken a look to the code, it can be changed to avoid to use 
the parameterized type is not propagated when deserializing.
see the comment here:

> 2. A Visual editor of some sort may display EnumSet as a list of
> checkboxes if it knows what type of enum values can be contained
> within this set.

again the declared type can be propagated.


> On Sat, Sep 21, 2013 at 8:15 PM, Remi Forax <forax at univ-mlv.fr> wrote:
>> On 09/21/2013 05:27 PM, Marcin Wiśnicki wrote:
>>> Third party serialization libraries and other tools need to know the
>>> type of EnumSet elementType. Currently this field is package-private
>>> and said libraries have to resort to non-portable reflection hacks.
>>> Please add EnumSet#getElementType() and EnumMap#getKeyType() methods.
>>> I've submitted this request to bugs.sun.com last month, where it
>>> received Bug Id: 9006352. There was no further communication and that
>>> bug is still not visible which doesn't surprise me as I've never had
>>> any luck with bugs.sun.com even when submitting actual bug :(
>>> PS. Please keep me CC'ed.
>> Hi Marcin,
>> I've read the bug file but failed to find a description of a compelling use
>> case for these methods.
>> Adding these two methods goes against an important rule:
>> try to avoid for sub-types of Collection, methods that are implementation
>> specific, the history has proven multiple times that implementations of
>> collections change a lot. This is a general rule, that we may want to ignore
>> but it has to worth it.
>> Also, I think it worth to understand what's elementType represent.
>> elementType is not the class of the values contained in the set but the
>> declaring class (Enum.getDeclaringClass) of the enum values. So despite
>> being a Class, elementType is a type information that only exists has a
>> field because generics are not reified. This has two consequences, the first
>> one is that if you want to create an EnumSet, you need a way to have an
>> elementType and the second one is that it may make the work of the people
>> that want to reify generics in a future version of Java harder.
>> and don't think I'm the dragon that keep the treasure, this is just my
>> humble opinion and I try to do my best to explain why adding these methods
>> is not obvious.
>> regards,
>> Rémi

More information about the core-libs-dev mailing list