Expose elementType of EnumSet and EnumMap
forax at univ-mlv.fr
Sat Sep 21 18:15:44 UTC 2013
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.
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.
More information about the core-libs-dev