{"id":"d7cb92c96fccd619","repo":"google/gson","slug":"typetoken-must-be-created-with-a-type-argument-ne","errorCode":null,"errorMessage":"TypeToken must be created with a type argument: new TypeToken<...>() {}; When using code shrinkers (ProGuard, R8, ...) make sure that generic signatures are preserved.\\nSee \" + TroubleshootingGuide.createUrl(\"type-token-raw\")","messagePattern":"TypeToken must be created with a type argument: new TypeToken<\\.\\.\\.>\\(\\) (.+?); When using code shrinkers \\(ProGuard, R8, \\.\\.\\.\\) make sure that generic signatures are preserved\\.\\\\nSee \" \\+ TroubleshootingGuide\\.createUrl\\(\"type-token-raw\"\\)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/reflect/TypeToken.java","lineNumber":110,"sourceCode":"   * Verifies that {@code this} is an instance of a direct subclass of TypeToken and returns the\n   * type argument for {@code T} in {@link GsonTypes#canonicalize canonical form}.\n   */\n  private Type getTypeTokenTypeArgument() {\n    Type superclass = getClass().getGenericSuperclass();\n    if (superclass instanceof ParameterizedType) {\n      ParameterizedType parameterized = (ParameterizedType) superclass;\n      if (parameterized.getRawType() == TypeToken.class) {\n        Type typeArgument = GsonTypes.canonicalize(parameterized.getActualTypeArguments()[0]);\n\n        if (isCapturingTypeVariablesForbidden()) {\n          verifyNoTypeVariable(typeArgument);\n        }\n        return typeArgument;\n      }\n    }\n    // Check for raw TypeToken as superclass\n    else if (superclass == TypeToken.class) {\n      throw new IllegalStateException(\n          \"TypeToken must be created with a type argument: new TypeToken<...>() {}; When using code\"\n              + \" shrinkers (ProGuard, R8, ...) make sure that generic signatures are preserved.\"\n              + \"\\nSee \"\n              + TroubleshootingGuide.createUrl(\"type-token-raw\"));\n    }\n\n    // User created subclass of subclass of TypeToken\n    throw new IllegalStateException(\"Must only create direct subclasses of TypeToken\");\n  }\n\n  private static void verifyNoTypeVariable(Type type) {\n    if (type instanceof TypeVariable) {\n      TypeVariable<?> typeVariable = (TypeVariable<?>) type;\n      throw new IllegalArgumentException(\n          \"TypeToken type argument must not contain a type variable; captured type variable \"\n              + typeVariable.getName()\n              + \" declared by \"\n              + typeVariable.getGenericDeclaration()","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/reflect/TypeToken.java#L92-L128","documentation":"TypeToken requires a captured generic type argument; it inspects its own generic superclass. If the immediate superclass is the raw TypeToken (no parameterization), Gson throws IllegalStateException pointing at ProGuard/R8 stripping generic signatures, or at a raw 'new TypeToken(){}' usage. A troubleshooting URL is included.","triggerScenarios":"Using 'new TypeToken(){}' with no type argument, or running on a code shrinker (ProGuard/R8) that removed the Signature attribute, so the generic superclass is observed as raw TypeToken.","commonSituations":"Android release builds with aggressive ProGuard/R8 rules; hand-written code that omits the generic argument; or tooling that drops Signature attributes.","solutions":["Always create TypeToken with an explicit type argument: 'new TypeToken<List<String>>(){}'.","Add ProGuard/R8 keep rules: '-keepattributes Signature' and keep your TypeToken subclasses.","Prefer the modern TypeToken.getParameterized(...) / TypeToken.get(...) APIs which do not rely on anonymous subclasses."],"exampleFix":"// before\nTypeToken<?> t = new TypeToken(){}; // raw, or signature stripped by R8\n\n// after\nTypeToken<List<String>> t = new TypeToken<List<String>>(){};\n// or, signature-independent:\nTypeToken<List<String>> t = TypeToken.getParameterized(List.class, String.class);\n// proguard-rules.pro:\n// -keepattributes Signature","handlingStrategy":"validation","validationCode":"boolean typeTokenHasArgument(TypeToken<?> t) {\n  Type sc = t.getClass().getGenericSuperclass();\n  return sc instanceof ParameterizedType && ((ParameterizedType) sc).getRawType() == TypeToken.class;\n}","typeGuard":"static boolean isParameterizedTypeTokenSubclass(Class<?> c) {\n  Type sc = c.getGenericSuperclass();\n  return sc instanceof ParameterizedType && ((ParameterizedType) sc).getRawType() == TypeToken.class;\n}","tryCatchPattern":"// Construction-time failure; wrap TypeToken creation in a test rather than runtime try-catch.\ntry {\n  TypeToken<List<String>> t = new TypeToken<List<String>>(){};\n} catch (IllegalStateException e) {\n  // switch to TypeToken.getParameterized(...)\n}","preventionTips":["Always specify the generic argument when creating TypeToken subclasses.","Keep Signature attributes in ProGuard/R8 ('-keepattributes Signature').","Prefer TypeToken.getParameterized / TypeToken.get to avoid anonymous subclasses entirely."],"tags":["gson","type-token","proguard","r8","generics","android"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}