{"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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/GsonTypes.java#L474-L510","documentation":"Thrown by GsonTypes.checkNotPrimitive(Type) when one of the type arguments supplied to a parameterized type (or a wildcard bound) is a primitive Class such as int.class, boolean.class, or byte.class. The Java Language Specification forbids primitives as type arguments (e.g. List<int> is illegal; you must use List<Integer>), and Gson enforces that same rule when it constructs its internal ParameterizedType / WildcardType implementations.","triggerScenarios":"Calling TypeToken.getParameterized(Collection.class, int.class), building a ParameterizedTypeImpl with a primitive in typeArguments, or deserializing into a generic field whose declared type argument resolves to a primitive Class. Also fires for wildcard bounds like ? super int passed to Gson's wildcard construction.","commonSituations":"Confusing a primitive Class literal (int.class) with its wrapper (Integer.class); auto-boxing assumptions that do not apply to Class objects; reflective code that reads field generic types and forwards primitives; code generation tools emitting primitive type arguments.","solutions":["Replace the primitive Class with its wrapper: Integer.class, Long.class, Boolean.class, Double.class, etc.","Map primitive classes to wrappers: a helper like Class<?> boxed = Array.get(Array.newInstance(primitive, 0), 0).getClass().","Validate the Type before passing to Gson: if (t instanceof Class && ((Class<?>) t).isPrimitive()) use the wrapper instead."],"exampleFix":"// before\nType t = TypeToken.getParameterized(List.class, int.class).getType();\n\n// after\nType t = TypeToken.getParameterized(List.class, Integer.class).getType();","handlingStrategy":"validation","validationCode":"static Type boxIfPrimitive(Type t) {\n  if (t instanceof Class<?> c && c.isPrimitive()) {\n    // primitive -> wrapper\n    return Array.get(Array.newInstance(c, 0), 0).getClass();\n  }\n  return t;\n}\n// usage: Type safe = boxIfPrimitive(argType);","typeGuard":"static boolean isNonPrimitiveType(Type t) {\n  return !(t instanceof Class<?> c && c.isPrimitive());\n}","tryCatchPattern":"try {\n  TypeToken.getParameterized(raw, arg).getType();\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"Primitive type is not allowed\")) {\n    arg = boxIfPrimitive(arg); // retry with wrapper\n  } else throw e;\n}","preventionTips":["Never use primitive .class literals as type arguments; always use wrappers.","Add a static helper that boxes primitives and route all type-argument construction through it.","Lint: forbid int.class/long.class/etc. near TypeToken usage in code review."],"tags":["gson","generics","reflection","primitive","type-argument"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}