Class EnumMapUtil
- java.lang.Object
-
- com.google.gwt.user.server.rpc.EnumMapUtil
-
public class EnumMapUtil extends java.lang.ObjectA utility to extract the key type of anyEnumMap, even if it is empty. Some frameworks have tried this by using reflection on the map's private fieldkeyType, but that approach breaks with Java 17 where reflection on private elements of anyjava.*class is forbidden unless the--add-opensVM argument is used which is a fairly unsafe thing to do.Use like this:
private static enum MyBoolean { TRUE, FALSE }; EnumMapenumMap = new EnumMap<>(MyBoolean.class); Class> c = EnumMapUtil.getKeyType(enumMap); Implementation note: If the map passed to
getKeyType(java.util.EnumMap)is not empty, the first key is obtained from thekey setand its type is determined and returned. If the map is empty, it is serialized into anObjectOutputStreamwriting to aByteArrayOutputStreamfrom which it is read again with a specializedObjectInputStreamwhere theresolveClassmethod is overridden. Upon reading a class descriptor for classEnumMap, instead of regularly resolving the class descriptor a package-protected classEnumMapis returned which has a serial version UID equal to that ofEnumMapand the same set of fields. By having the same simple name (despite living in a different package) theObjectInputStreamconsiders this class to be compatible toEnumMapand de-serializes the stream contents into an instance ofEnumMapwhich in turn offers a public getterEnumMap.getKeyType()for the key type. Through this getter the key type is then determined and returned.
-
-
Constructor Summary
Constructors Constructor Description EnumMapUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <K extends java.lang.Enum<K>,V>
java.lang.Class<K>getKeyType(java.util.EnumMap<K,V> enumMap)
-