{"record":{"id":"9eb0716caa00bdde","repo":"google/gson","slug":"primitive-type-is-not-allowed","errorCode":null,"errorMessage":"Primitive type is not allowed","messagePattern":"Primitive type is not allowed","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/GsonTypes.java","lineNumber":492,"sourceCode":"      if (toFind.equals(array[i])) {\n        return i;\n      }\n    }\n    throw new NoSuchElementException();\n  }\n\n  /**\n   * Returns the declaring class of {@code typeVariable}, or {@code null} if it was not declared by\n   * a class.\n   */\n  private static Class<?> declaringClassOf(TypeVariable<?> typeVariable) {\n    GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();\n    return genericDeclaration instanceof Class ? (Class<?>) genericDeclaration : null;\n  }\n\n  static void checkNotPrimitive(Type type) {\n    if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) {\n      throw new IllegalArgumentException(\"Primitive type is not allowed\");\n    }\n  }\n\n  /**\n   * Whether an {@linkplain ParameterizedType#getOwnerType() owner type} must be specified when\n   * constructing a {@link ParameterizedType} for {@code rawType}.\n   *\n   * <p>Note that this method might not require an owner type for all cases where Java reflection\n   * would create parameterized types with owner type.\n   */\n  public static boolean requiresOwnerType(Type rawType) {\n    if (rawType instanceof Class<?>) {\n      Class<?> rawTypeAsClass = (Class<?>) rawType;\n      return !Modifier.isStatic(rawTypeAsClass.getModifiers())\n          && rawTypeAsClass.getDeclaringClass() != null;\n    }\n    return false;\n  }","sourceCodeStart":474,"sourceCodeEnd":510,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/GsonTypes.java#L474-L510","documentation":"Thrown by GsonTypes.checkNotPrimitive when a Type argument supplied to a ParameterizedTypeImpl is a primitive Class (int.class, boolean.class, etc.). Reflection Type mirrors cannot legally represent primitive types; only their boxed/wrapper types or reference component types are valid in parameterized positions. Gson enforces this so it never builds an internally inconsistent Type token.","triggerScenarios":"Constructing a parameterized Type whose actual type argument is a primitive Class, e.g. passing int.class as a type argument into GsonTypes$ParameterizedTypeImpl (called at GsonTypes.java:531). Reachable via TypeToken.getParameterized(raw, int.class), TypeToken<Collection<int>>, or a manually built ParameterizedType with a primitive in the args array.","commonSituations":"Building TypeTokens for collections/primitive arrays by hand; porting code that assumed int.class was a valid Type argument; reflective generics code that iterates Class objects and forgets to box primitives before constructing a parameterized type.","solutions":["Replace the primitive Class with its wrapper, e.g. int.class -> Integer.class, boolean.class -> Boolean.class.","For primitive arrays use the array type int[].class (an Object subclass) rather than a primitive class inside a parameterized position.","If generating types reflectively, normalize with a helper: Class<?> boxed = raw.isPrimitive() ? box(raw) : raw before passing as a type argument."],"exampleFix":"// before\nType t = TypeToken.getParameterized(Collection.class, int.class).getType();\n// after\nType t = TypeToken.getParameterized(Collection.class, Integer.class).getType();","handlingStrategy":"validation","validationCode":"private static boolean isValidTypeArg(Type t) {\n  return !(t instanceof Class<?> c && c.isPrimitive());\n}\n// before building: if (!isValidTypeArg(arg)) arg = box((Class<?>) arg);","typeGuard":"static Type boxIfPrimitive(Type t) {\n  if (t instanceof Class<?> c && c.isPrimitive()) {\n    if (c == int.class) return Integer.class;\n    if (c == long.class) return Long.class;\n    if (c == boolean.class) return Boolean.class;\n    if (c == double.class) return Double.class;\n    if (c == float.class) return Float.class;\n    if (c == short.class) return Short.class;\n    if (c == byte.class) return Byte.class;\n    if (c == char.class) return Character.class;\n    if (c == void.class) return Void.class;\n  }\n  return t;\n}","tryCatchPattern":null,"preventionTips":["Never pass primitive .class literals as generic type arguments; always use their wrappers.","When building TypeTokens reflectively, run every Class through a box() helper before use.","Prefer TypeToken.getParameterized over manually constructing parameterized types."],"tags":["gson","reflection","generics","type-token"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}