{"record":{"id":"508ff3c340996bc0","repo":"google/gson","slug":"contextrawtype-is-not-the-same-as-or-a-subtype","errorCode":null,"errorMessage":"${contextRawType} is not the same as or a subtype of ${supertype}","messagePattern":"(.+?) is not the same as or a subtype of (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/GsonTypes.java","lineNumber":288,"sourceCode":"\n  /**\n   * Returns the generic form of {@code supertype}. For example, if this is {@code\n   * ArrayList<String>}, this returns {@code Iterable<String>} given the input {@code\n   * Iterable.class}.\n   *\n   * @param supertype a superclass of, or interface implemented by, this.\n   */\n  private static Type getSupertype(Type context, Class<?> contextRawType, Class<?> supertype) {\n    if (context instanceof WildcardType) {\n      // Wildcards are useless for resolving supertypes. As the upper bound has the same raw type,\n      // use it instead\n      Type[] bounds = ((WildcardType) context).getUpperBounds();\n      // Currently the JLS only permits one bound for wildcards so using first bound is safe\n      assert bounds.length == 1;\n      context = bounds[0];\n    }\n    if (!supertype.isAssignableFrom(contextRawType)) {\n      throw new IllegalArgumentException(\n          contextRawType + \" is not the same as or a subtype of \" + supertype);\n    }\n    return resolve(\n        context, contextRawType, GsonTypes.getGenericSupertype(context, contextRawType, supertype));\n  }\n\n  /**\n   * Returns the component type of this array type.\n   *\n   * @throws ClassCastException if this type is not an array.\n   */\n  public static Type getArrayComponentType(Type array) {\n    return array instanceof GenericArrayType\n        ? ((GenericArrayType) array).getGenericComponentType()\n        : ((Class<?>) array).getComponentType();\n  }\n\n  /**","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/GsonTypes.java#L270-L306","documentation":"Thrown by GsonTypes.getSupertype() as an internal invariant check: the requested supertype must be assignable from the contextRawType. If supertype.isAssignableFrom(contextRawType) is false, the call is logically invalid. This indicates a bug in type resolution or a custom TypeAdapterFactory passing an incorrect supertype.","triggerScenarios":"An internal or custom call to getSupertype(context, contextRawType, supertype) where the supertype is not actually a supertype of contextRawType — e.g. asking for Number.class as a supertype of String.class.","commonSituations":"Bugs in custom TypeAdapterFactory implementations; incorrect TypeToken hierarchies that don't reflect real inheritance; internal Gson edge cases with unusual generic bounds.","solutions":["Verify the type hierarchy is correct: the supertype argument must be assignable from the raw type","Fix custom TypeAdapterFactory logic to compute the correct supertype","Ensure TypeTokens reflect real class/interface inheritance relationships"],"exampleFix":"// before - String is not a Number\nGsonTypes.getSupertype(type, String.class, Number.class);\n\n// after - Integer IS a Number\nGsonTypes.getSupertype(type, Integer.class, Number.class);","handlingStrategy":"validation","validationCode":"// Before computing a supertype relationship, verify assignability\nClass<?> contextRawType = MyImpl.class;\nClass<?> supertype = SomeInterface.class;\nif (!supertype.isAssignableFrom(contextRawType)) {\n  throw new IllegalArgumentException(\n    contextRawType + \" is not assignable from \" + supertype);\n}\n// safe to resolve supertype","typeGuard":"static boolean isValidSupertype(Class<?> rawType, Class<?> supertype) {\n  return supertype.isAssignableFrom(rawType);\n}","tryCatchPattern":null,"preventionTips":["Always verify supertype.isAssignableFrom(rawType) before calling getSupertype in custom adapters","Ensure TypeToken hierarchies reflect real class/interface inheritance","Unit-test custom TypeAdapterFactories with type pairs that do and don't satisfy the supertype relation"],"tags":["type-resolution","generics","supertype","invariant"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}