Add EnumMap.keyType() and EnumSet.elementType() (original) (raw)
Andrej Golovnin andrej.golovnin at gmail.com
Thu Dec 7 11:40:21 UTC 2017
- Previous message: FW: RFR(M): 8189102: All tools should support -?, -h and --help
- Next message: Add EnumMap.keyType() and EnumSet.elementType()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi all,
it would be nice if we would have access to the class of the enum type used to create an EnumMap or an EnumSet.
This is usefull when you write a custom serialization library and would like to serialize/deserialize an empty EnumMap or an empty EnumSet. For the empty EnumSet there is a workaround to get the enum class:
EnumSet empty = EnumSet.noneOf(MyEnum.class); EnumSet tmp = EnumSet.complementOf(empty); Class<?> elementType = tmp.iterator().next().getClass();
But this only works when the enum class has at least one enum. For EnumMap there is no such workaround at all and we have to use the Reflection API. And you know the warnings since Java 9 :-) :
WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by c.r.r.k.EnumMapSerializer to field java.util.EnumMap.keyType WARNING: Please consider reporting this to the maintainers of c.r.r.k.EnumMapSerializer WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release
So to avoid this warning and to avoid that our application stops to work with a future release of Java I would like to propose to add the following two methods:
EnumMap: /** * Returns the {@code Class} object of the key type for this enum map. * * @return the {@code Class} object of the key type for this enum map. * * @since 10 */ public Class keyType()
EnumSet: /** * Returns the {@code Class} object of all the elements of this set. * * @return the {@code Class} object of all the elements of this set. * * @since 10 */ public Class elementType()
The suggested change is attached as diff.
Best reagrds, Andrej Golovnin -------------- next part -------------- diff --git a/src/java.base/share/classes/java/util/EnumMap.java b/src/java.base/share/classes/java/util/EnumMap.java --- a/src/java.base/share/classes/java/util/EnumMap.java +++ b/src/java.base/share/classes/java/util/EnumMap.java @@ -179,6 +179,17 @@ } }
- /**
* Returns the {@code Class} object of the key type for this enum map.
*
* @return the {@code Class} object of the key type for this enum map.
*
* @since 10
*/
- public Class keyType() {
return keyType;
- }
// Query Operations /**
diff --git a/src/java.base/share/classes/java/util/EnumSet.java b/src/java.base/share/classes/java/util/EnumSet.java --- a/src/java.base/share/classes/java/util/EnumSet.java +++ b/src/java.base/share/classes/java/util/EnumSet.java @@ -365,6 +365,17 @@ }
/**
* Returns the {@code Class} object of all the elements of this set.
*
* @return the {@code Class} object of all the elements of this set.
*
* @since 10
*/
- public Class elementType() {
return elementType;
- }
- /**
- Adds the specified range to this enum set, which is empty prior
- to the call. */
- Previous message: FW: RFR(M): 8189102: All tools should support -?, -h and --help
- Next message: Add EnumMap.keyType() and EnumSet.elementType()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]