{"id":"9f452bb2c5f9a061","repo":"google/gson","slug":"invalid-enumset-type-type","errorCode":null,"errorMessage":"Invalid EnumSet type: {type}","messagePattern":"Invalid EnumSet type: (.+?)","errorType":"exception","errorClass":"JsonIOException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java","lineNumber":188,"sourceCode":"    return newUnsafeAllocator(rawType);\n  }\n\n  /**\n   * Creates constructors for special JDK collection types which do not have a public no-args\n   * constructor.\n   */\n  private static <T> ObjectConstructor<T> newSpecialCollectionConstructor(\n      Type type, Class<? super T> rawType) {\n    if (EnumSet.class.isAssignableFrom(rawType)) {\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 set = (T) EnumSet.noneOf((Class) elementType);\n            return set;\n          } else {\n            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);","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java#L170-L206","documentation":"ConstructorConstructor builds an EnumSet factory only when the type is a ParameterizedType whose first type argument is a Class (the enum element type). If the argument is a ParameterizedType, TypeVariable, WildcardType, etc., it throws JsonIOException with the offending type.","triggerScenarios":"Deserializing a field declared as EnumSet<? extends MyEnum>, EnumSet<T> with a type variable, or EnumSet with a parameterized element type; reflective construction from generic TypeTokens that lose the concrete enum class.","commonSituations":"Generic utility classes holding EnumSet<E>; wildcard bounds in API models; incorrectly built TypeToken<EnumSet<...>> where the enum type is erased at capture.","solutions":["Declare the field as EnumSet<MyEnum> with a concrete enum class.","Build a TypeToken with the concrete enum: new TypeToken<EnumSet<MyEnum>>(){}.","Register an InstanceCreator<EnumSet> that supplies EnumSet.noneOf(MyEnum.class).","Avoid wildcards/type variables on EnumSet fields used for deserialization."],"exampleFix":"// before\nEnumSet<? extends Color> colors; // fails\n\n// after\nEnumSet<Color> colors; // concrete enum type","handlingStrategy":"validation","validationCode":"if (type instanceof ParameterizedType\n    && ((ParameterizedType) type).getActualTypeArguments()[0] instanceof Class) {\n  // safe to let Gson construct EnumSet\n}\n","typeGuard":"boolean isConcreteEnumSetType(Type t) {\n  return t instanceof ParameterizedType\n      && ((ParameterizedType) t).getActualTypeArguments()[0] instanceof Class;\n}\n","tryCatchPattern":"try {\n  return gson.fromJson(json, type);\n} catch (JsonIOException ex) {\n  if (ex.getMessage().startsWith(\"Invalid EnumSet type\")) {\n    // register InstanceCreator and retry with EnumSet.noneOf(MyEnum.class)\n  }\n  throw ex;\n}\n","preventionTips":["Declare EnumSet fields with a concrete enum type.","Build TypeToken<EnumSet<MyEnum>>(){} explicitly.","Register an InstanceCreator<EnumSet> for generic utilities."],"tags":["deserialization","enum","generic-types","reflection"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}