{"id":"2407f73e1c8cc08f","repo":"google/gson","slug":"contextrawtype-is-not-the-same-as-or-a-subtype-o","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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/GsonTypes.java#L270-L306","documentation":"GsonTypes.getSupertype asserts that the supplied supertype is actually assignable from the context's raw type; otherwise it throws IllegalArgumentException. It is an internal invariant violation in generic-supertype resolution — the caller claimed a supertype relationship that does not hold.","triggerScenarios":"Custom TypeAdapterFactory / InstanceCreator invoking reflection helpers with an incorrect supertype/class pairing; programmatic TypeToken.getSupertype() with a class that is not a superclass/interface; bytecode/manipulation producing inconsistent Type metadata.","commonSituations":"Bugs in user-written TypeAdapterFactories; refactoring a class hierarchy without updating TypeToken supertype calls; proxy/AOP frameworks altering class metadata; misconfigured generic hierarchies.","solutions":["Verify the supertype argument: ensure supertype.isAssignableFrom(rawType) before calling getSupertype.","Fix the TypeAdapterFactory to use the correct supertype from getGenericSuperclass()/getGenericInterfaces().","Add a unit test asserting the hierarchy assumption holds for the target types.","Avoid reflecting across unrelated class hierarchies in custom adapters."],"exampleFix":"// before\nType t = GsonTypes.getSupertype(context, Dog.class, Vehicle.class); // Dog !<: Vehicle\n\n// after\nType t = GsonTypes.getSupertype(context, Dog.class, Animal.class); // Dog <: Animal","handlingStrategy":"validation","validationCode":"if (!supertype.isAssignableFrom(rawType)) {\n  throw new IllegalArgumentException(rawType + \" not a \" + supertype);\n}\n","typeGuard":"boolean isSupertypeOf(Class<?> rawType, Class<?> supertype) {\n  return supertype.isAssignableFrom(rawType);\n}\n","tryCatchPattern":"try {\n  return GsonTypes.getSupertype(context, rawType, supertype);\n} catch (IllegalArgumentException ex) {\n  // pick the correct superclass/interface from rawType.getGenericSuperclass()/getGenericInterfaces()\n  throw ex;\n}\n","preventionTips":["Assert supertype.isAssignableFrom(rawType) before calling getSupertype.","Derive supertype arguments from the class's own generic metadata.","Unit-test hierarchy assumptions in custom TypeAdapterFactories."],"tags":["type-resolution","generic-types","reflection","illegal-argument","invariant"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}