{"id":"0e09f0868ce0a61c","repo":"google/gson","slug":"rawclass-getname-requires-expectedargsco","errorCode":null,"errorMessage":"rawClass.getName() + \" requires \" + expectedArgsCount + \" type arguments, but got \" + actualArgsCount","messagePattern":"rawClass\\.getName\\(\\) \\+ \" requires \" \\+ expectedArgsCount \\+ \" type arguments, but got \" \\+ actualArgsCount","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/reflect/TypeToken.java","lineNumber":401,"sourceCode":"   *     type arguments are invalid for the raw type\n   */\n  public static TypeToken<?> getParameterized(Type rawType, Type... typeArguments) {\n    Objects.requireNonNull(rawType);\n    Objects.requireNonNull(typeArguments);\n\n    // Perform basic validation here because this is the only public API where users\n    // can create malformed parameterized types\n    if (!(rawType instanceof Class)) {\n      // See also https://bugs.openjdk.org/browse/JDK-8250659\n      throw new IllegalArgumentException(\"rawType must be of type Class, but was \" + rawType);\n    }\n    Class<?> rawClass = (Class<?>) rawType;\n    TypeVariable<?>[] typeVariables = rawClass.getTypeParameters();\n\n    int expectedArgsCount = typeVariables.length;\n    int actualArgsCount = typeArguments.length;\n    if (actualArgsCount != expectedArgsCount) {\n      throw new IllegalArgumentException(\n          rawClass.getName()\n              + \" requires \"\n              + expectedArgsCount\n              + \" type arguments, but got \"\n              + actualArgsCount);\n    }\n\n    // For legacy reasons create a TypeToken(Class) if the type is not generic\n    if (typeArguments.length == 0) {\n      return get(rawClass);\n    }\n\n    // Check for this here to avoid misleading exception thrown by ParameterizedTypeImpl\n    if (GsonTypes.requiresOwnerType(rawType)) {\n      throw new IllegalArgumentException(\n          \"Raw type \"\n              + rawClass.getName()\n              + \" is not supported because it requires specifying an owner type\");","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/reflect/TypeToken.java#L383-L419","documentation":"Thrown by TypeToken.getParameterized when the number of type arguments supplied does not equal the number declared by the raw class (rawClass.getTypeParameters().length). This is an explicit count check before any attempt to build the parameterized type, giving a clear error instead of a downstream ArrayIndexOutOfBoundsException.","triggerScenarios":"Calling getParameterized with too few or too many arguments, e.g. TypeToken.getParameterized(Map.class, String.class) (Map needs 2) or TypeToken.getParameterized(List.class, String.class, Integer.class) (List needs 1).","commonSituations":"Wrapping collection types in helper methods where the arity is dynamic; copy-paste errors when changing the raw type (e.g. switching from List to Map) without updating the arguments; passing a varargs array of unknown length.","solutions":["Match the count to rawClass.getTypeParameters().length; e.g. for Map pass exactly two type arguments.","If the type arguments are dynamic, read rawClass.getTypeParameters().length first and validate before calling.","For a non-generic class pass no type arguments (the method delegates to get(rawClass)).","Double-check imports: using Map vs Map.Entry changes the expected arity."],"exampleFix":"// before\nTypeToken.getParameterized(Map.class, String.class); // Map needs 2\n\n// after\nTypeToken.getParameterized(Map.class, String.class, Integer.class);","handlingStrategy":"validation","validationCode":"void requireArity(Class<?> rawClass, Type... args) {\n  int expected = rawClass.getTypeParameters().length;\n  if (args.length != expected) {\n    throw new IllegalArgumentException(rawClass.getName() + \" needs \" + expected + \" args, got \" + args.length);\n  }\n}\n// call before TypeToken.getParameterized(rawClass, args)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Read rawClass.getTypeParameters().length to confirm arity before calling getParameterized.","Write helper methods that bundle a raw class with exactly its declared number of arguments.","Add compile-time tests asserting correct arity for each token you build.","Remember non-generic classes take zero type arguments."],"tags":["gson","typetoken","generics","validation"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}