Expose elementType of EnumSet and EnumMap

Remi Forax forax at univ-mlv.fr
Sun Sep 22 09:17:27 UTC 2013

On 09/21/2013 11:32 PM, Marcin Wiśnicki wrote:
> On Sat, Sep 21, 2013 at 10:21 PM, Remi Forax <forax at univ-mlv.fr> wrote:
>> No, you can add methods to one value of an enum using the anonymous class
>> syntax.
> Thanks for pointing this out. This is something that can be clarified
> in javadoc.
> Though javadocs of existing factory methods of EnumSet are not so
> pedantic about "elementType" argument.
>> 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.
> I fail to see a problem here. In fact this is a good case why such
> method is necessary - if generics get reified in Java 9 and designers
> of EnumSet decide to remove redundant elementType field, all software
> depending on existence of this field will break. If on the other hand
> there was a getElementType() method in Java 8, it could be safely
> changed to obtain element type through whatever new mechanism is
> available and existing software using getElementType() keeps working
> correctly.

There is a lot of code that cast to T or to EnumSet<T> that will start 
to fail at runtime if generics are reified. One possible way to avoid 
that is to create a new collection API, another is to try to reuse 
existing implementations with an opt-in mecanism. In the later case, the 
same class will be seen refiied
or not depending on how the collection is created, so enabling 
reification will be done incrementally in that case.
getElementType is a way to export a refied type, it will add burden to 
mixed mode implementation.

>> I've taken a look to the code, it can be changed to avoid to use
>> getElementType(),
>> the parameterized type is not propagated when deserializing.
>> see the comment here:
>> https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java#L49
> That was just an example, there is more code like that out there,
> including my own and it's not always easy to restructure it in a way
> that would allow propagating declared type.

You have to propagate declared types otherwise your code is not typesafe.
If you don't propagate type that you can de-serialize a List of String 
without checking that the elements inside the List are String.


More information about the core-libs-dev mailing list