{"id":"db66c5aa19e6f324","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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java#L188-L224","documentation":"The EnumMap factory requires a ParameterizedType whose first argument is a Class (the key enum type). If the key argument is itself generic/parameterized/wildcard, the enum class cannot be determined and JsonIOException is thrown.","triggerScenarios":"Field declared EnumMap<MyEnum<?>, V>, EnumMap<K, V> with K a type variable, or a TypeToken losing the concrete enum key class; reflective construction where the key type is captured generically.","commonSituations":"Generic map utilities; wildcard key bounds; frameworks binding EnumMap<K,V> generically.","solutions":["Use a concrete enum key: EnumMap<MyEnum, String>.","Build an explicit TypeToken: new TypeToken<EnumMap<MyEnum, String>>(){}.","Register an InstanceCreator<EnumMap<MyEnum, V>>.","Avoid wildcard/type-variable keys on EnumMap fields."],"exampleFix":"// before\nEnumMap<? extends Color, Integer> counts;\n\n// after\nEnumMap<Color, Integer> counts;","handlingStrategy":"validation","validationCode":"if (type instanceof ParameterizedType\n    && ((ParameterizedType) type).getActualTypeArguments()[0] instanceof Class) {\n  // safe EnumMap construction\n}\n","typeGuard":"boolean isConcreteEnumMapType(Type t) {\n  if (!(t instanceof ParameterizedType)) return false;\n  Type k = ((ParameterizedType) t).getActualTypeArguments()[0];\n  return k instanceof Class && ((Class<?>) k).isEnum();\n}\n","tryCatchPattern":"try {\n  return gson.fromJson(json, type);\n} catch (JsonIOException ex) {\n  if (ex.getMessage().startsWith(\"Invalid EnumMap type\")) {\n    // register InstanceCreator and retry\n  }\n  throw ex;\n}\n","preventionTips":["Use concrete enum keys on EnumMap fields.","Build explicit TypeToken<EnumMap<MyEnum, V>>(){}.","Register an InstanceCreator for generic map utilities."],"tags":["deserialization","enum","map","generic-types","reflection"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}