{"record":{"id":"5a93f0e9239af470","repo":"google/gson","slug":"invalid-enummap-type-type","errorCode":null,"errorMessage":"Invalid EnumMap type: ${type}","messagePattern":"Invalid EnumMap type: (.+?)","errorType":"exception","errorClass":"JsonIOException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java","lineNumber":206,"sourceCode":"            throw new JsonIOException(\"Invalid EnumSet type: \" + type);\n          }\n        } else {\n          throw new JsonIOException(\"Invalid EnumSet type: \" + type);\n        }\n      };\n    }\n    // Only support creation of EnumMap, but not of custom subtypes; for them type parameters\n    // and constructor parameter might have completely different meaning\n    else if (rawType == EnumMap.class) {\n      return () -> {\n        if (type instanceof ParameterizedType) {\n          Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];\n          if (elementType instanceof Class) {\n            @SuppressWarnings({\"unchecked\", \"rawtypes\"})\n            T map = (T) new EnumMap((Class) elementType);\n            return map;\n          } else {\n            throw new JsonIOException(\"Invalid EnumMap type: \" + type);\n          }\n        } else {\n          throw new JsonIOException(\"Invalid EnumMap type: \" + type);\n        }\n      };\n    }\n\n    return null;\n  }\n\n  private static <T> ObjectConstructor<T> newDefaultConstructor(\n      Class<? super T> rawType, FilterResult filterResult) {\n    // Cannot invoke constructor of abstract class\n    if (Modifier.isAbstract(rawType.getModifiers())) {\n      return null;\n    }\n\n    Constructor<? super T> constructor;","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java#L188-L224","documentation":"Thrown when Gson constructs an EnumMap whose type is a ParameterizedType but the key type argument is not a concrete Class (e.g. a TypeVariable). EnumMap's constructor requires a concrete enum class for the key. Only raw EnumMap (not subclasses) is supported. The message includes the type.","triggerScenarios":"Deserializing EnumMap<K, V> where K is an unresolved type variable; a generic wrapper with an EnumMap<K, V> field where the key cannot be resolved to a concrete enum class.","commonSituations":"Generic container classes with EnumMap<K,V> fields; type parameters that can't be resolved through the type hierarchy.","solutions":["Use a concrete TypeToken<EnumMap<MyEnum, String>> when deserializing","Register an InstanceCreator<EnumMap> returning new EnumMap<>(MyEnum.class)","Register a TypeAdapter for the wrapper that handles EnumMap construction"],"exampleFix":"// before - K unresolved\nclass Wrapper<K extends Enum<K>, V> { EnumMap<K, V> map; }\n\n// after - register InstanceCreator\ngsonBuilder.registerTypeAdapter(EnumMap.class,\n  (InstanceCreator<EnumMap>) type -> new EnumMap<>(MyEnum.class));","handlingStrategy":"type-guard","validationCode":"// Ensure EnumMap key type is a concrete Class before deserializing\nType targetType = typeToken.getType();\nif (targetType instanceof ParameterizedType) {\n  Type keyType = ((ParameterizedType) targetType).getActualTypeArguments()[0];\n  if (!(keyType instanceof Class)) {\n    throw new IllegalStateException(\"EnumMap key type must be concrete Class, got \" + keyType);\n  }\n}","typeGuard":"static boolean isConcreteEnumMap(Type type) {\n  if (!(type instanceof ParameterizedType)) return false;\n  ParameterizedType pt = (ParameterizedType) type;\n  if (pt.getRawType() != EnumMap.class) return false;\n  return pt.getActualTypeArguments()[0] instanceof Class\n      && ((Class<?>) pt.getActualTypeArguments()[0]).isEnum();\n}","tryCatchPattern":null,"preventionTips":["Always use concrete TypeToken<EnumMap<MyEnum, V>> when deserializing EnumMap","Register an InstanceCreator<EnumMap> if the key type can't be resolved at runtime","Note: only raw EnumMap (not subclasses) is supported by Gson's special constructor"],"tags":["enummap","generics","deserialization","constructor"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}