{"record":{"id":"93181a67efc3f6d3","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/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java#L170-L206","documentation":"Thrown when Gson constructs an EnumSet whose declared type is a ParameterizedType but the element type argument is not a concrete Class (e.g. a TypeVariable or WildcardType). EnumSet.noneOf() requires a concrete enum class, so Gson cannot proceed. The message includes the unresolved type.","triggerScenarios":"Deserializing a generic type like EnumSet<T> where T is an unresolved type variable at deserialization time; a generic wrapper class with an EnumSet<E> field where E cannot be resolved to a concrete enum.","commonSituations":"Generic container classes holding EnumSet<E> fields deserialized without a concrete type binding; subclassing generic types without reifying the type parameter.","solutions":["Use a concrete TypeToken<EnumSet<MyEnum>> when calling fromJson so the element type is a Class","Register an InstanceCreator<EnumSet> that returns EnumSet.noneOf(MyEnum.class)","Register a TypeAdapter for the generic wrapper type that resolves the enum concretely"],"exampleFix":"// before - E is unresolved\nclass Wrapper<E extends Enum<E>> { EnumSet<E> values; }\ngson.fromJson(json, new TypeToken<Wrapper<MyEnum>>(){}.getType());\n\n// after - register InstanceCreator\ngsonBuilder.registerTypeAdapter(EnumSet.class,\n  (InstanceCreator<EnumSet>) type -> EnumSet.noneOf(MyEnum.class));","handlingStrategy":"type-guard","validationCode":"// Ensure EnumSet element type is a concrete Class before deserializing\nType targetType = typeToken.getType();\nif (targetType instanceof ParameterizedType) {\n  Type arg0 = ((ParameterizedType) targetType).getActualTypeArguments()[0];\n  if (!(arg0 instanceof Class)) {\n    throw new IllegalStateException(\"EnumSet element type must be concrete Class, got \" + arg0);\n  }\n}","typeGuard":"static boolean isConcreteEnumSet(Type type) {\n  if (!(type instanceof ParameterizedType)) return false;\n  ParameterizedType pt = (ParameterizedType) type;\n  if (!EnumSet.class.isAssignableFrom((Class<?>) pt.getRawType())) return false;\n  return pt.getActualTypeArguments()[0] instanceof Class\n      && ((Class<?>) pt.getActualTypeArguments()[0]).isEnum();\n}","tryCatchPattern":null,"preventionTips":["Always use concrete TypeToken<EnumSet<MyEnum>> when deserializing EnumSet","Register an InstanceCreator<EnumSet> if your generic wrapper can't resolve the enum at runtime","Avoid EnumSet fields on open generic types without a registered InstanceCreator"],"tags":["enumset","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"}