{"record":{"id":"33a2290da320e4e0","repo":"google/gson","slug":"expected-a-class-parameterizedtype-or-genericarr","errorCode":null,"errorMessage":"Expected a Class, ParameterizedType, or GenericArrayType, but <${type}> is of type ${className}","messagePattern":"Expected a Class, ParameterizedType, or GenericArrayType, but <(.+?)> is of type (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/GsonTypes.java","lineNumber":158,"sourceCode":"\n    } else if (type instanceof GenericArrayType) {\n      Type componentType = ((GenericArrayType) type).getGenericComponentType();\n      return Array.newInstance(getRawType(componentType), 0).getClass();\n\n    } else if (type instanceof TypeVariable) {\n      // we could use the variable's bounds, but that won't work if there are multiple.\n      // having a raw type that's more general than necessary is okay\n      return Object.class;\n\n    } else if (type instanceof WildcardType) {\n      Type[] bounds = ((WildcardType) type).getUpperBounds();\n      // Currently the JLS only permits one bound for wildcards so using first bound is safe\n      assert bounds.length == 1;\n      return getRawType(bounds[0]);\n\n    } else {\n      String className = type == null ? \"null\" : type.getClass().getName();\n      throw new IllegalArgumentException(\n          \"Expected a Class, ParameterizedType, or GenericArrayType, but <\"\n              + type\n              + \"> is of type \"\n              + className);\n    }\n  }\n\n  private static boolean equal(Object a, Object b) {\n    return Objects.equals(a, b);\n  }\n\n  /** Returns true if {@code a} and {@code b} are equal. */\n  public static boolean equals(Type a, Type b) {\n    @SuppressWarnings(\"ReferenceEquality\")\n    boolean areSame = a == b;\n    if (areSame) {\n      // also handles (a == null && b == null)\n      return true;","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/GsonTypes.java#L140-L176","documentation":"Thrown by GsonTypes.getRawType() when the given Type is none of the supported implementations (Class, ParameterizedType, GenericArrayType, TypeVariable, WildcardType). This indicates an unsupported or custom Type implementation was passed to Gson's type-resolution machinery. The message names the type and its class.","triggerScenarios":"Passing a custom java.lang.reflect.Type implementation that doesn't implement any of the standard subinterfaces to Gson's type resolution, TypeToken, or reflection-based constructors.","commonSituations":"Custom Type implementations from third-party libraries; incorrect TypeToken subclassing; hand-built Type objects that bypass the standard interfaces.","solutions":["Use standard types via TypeToken: new TypeToken<MyClass<String>>(){}.getType()","Register a TypeAdapter for the type to bypass raw type resolution entirely","Replace the custom Type with a standard ParameterizedType produced by GsonTypes.newParameterizedTypeWithOwner"],"exampleFix":"// before\ngson.fromJson(json, myCustomTypeImpl); // custom Type\n\n// after\ngson.fromJson(json, new TypeToken<MyClass<String>>(){}.getType());","handlingStrategy":"type-guard","validationCode":"// Validate that a Type is one Gson's getRawType supports before using it\nstatic boolean isSupportedRawType(Type type) {\n  return type instanceof Class\n      || type instanceof ParameterizedType\n      || type instanceof GenericArrayType\n      || type instanceof TypeVariable\n      || type instanceof WildcardType;\n}\nif (!isSupportedRawType(myType)) {\n  throw new IllegalArgumentException(\"Unsupported Type implementation: \" + type.getClass());\n}","typeGuard":"static boolean isStandardType(Type type) {\n  return type == null\n      || type instanceof Class\n      || type instanceof ParameterizedType\n      || type instanceof GenericArrayType\n      || type instanceof TypeVariable\n      || type instanceof WildcardType;\n}","tryCatchPattern":null,"preventionTips":["Always use TypeToken to create Type instances — it produces standard ParameterizedType implementations","Never pass hand-rolled or third-party Type objects to Gson without registering a TypeAdapter","If you must use a custom Type, register a TypeAdapter for it to bypass getRawType"],"tags":["type-resolution","generics","reflection"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}