{"record":{"id":"8d49f0c734711801","repo":"google/gson","slug":"must-specify-owner-type-for-rawtype","errorCode":null,"errorMessage":"Must specify owner type for ${rawType}","messagePattern":"Must specify owner type for (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/GsonTypes.java","lineNumber":523,"sourceCode":"      Class<?> rawTypeAsClass = (Class<?>) rawType;\n      return !Modifier.isStatic(rawTypeAsClass.getModifiers())\n          && rawTypeAsClass.getDeclaringClass() != null;\n    }\n    return false;\n  }\n\n  private static final class ParameterizedTypeImpl implements ParameterizedType {\n    private final Type ownerType;\n\n    private final Type rawType;\n\n    private final Type[] typeArguments;\n\n    ParameterizedTypeImpl(Type ownerType, Class<?> rawType, Type... typeArguments) {\n      requireNonNull(rawType);\n\n      if (ownerType == null && requiresOwnerType(rawType)) {\n        throw new IllegalArgumentException(\"Must specify owner type for \" + rawType);\n      }\n\n      this.ownerType = ownerType == null ? null : canonicalize(ownerType);\n      this.rawType = canonicalize(rawType);\n      this.typeArguments = typeArguments.clone();\n      for (int t = 0, length = this.typeArguments.length; t < length; t++) {\n        requireNonNull(this.typeArguments[t]);\n        checkNotPrimitive(this.typeArguments[t]);\n        this.typeArguments[t] = canonicalize(this.typeArguments[t]);\n      }\n    }\n\n    @Override\n    public Type[] getActualTypeArguments() {\n      return typeArguments.clone();\n    }\n\n    @Override","sourceCodeStart":505,"sourceCodeEnd":541,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/GsonTypes.java#L505-L541","documentation":"Thrown when building a ParameterizedTypeImpl for a non-static inner (member) class without supplying an ownerType. Java's type system requires an enclosing instance type for non-static nested classes (GsonTypes.requiresOwnerType returns true when the class has a declaring class and is not static). Gson refuses to silently fabricate one because doing so would create an ambiguous, possibly incorrect Type.","triggerScenarios":"Calling TypeToken.getParameterized(InnerNonStaticClass.class, ...) or otherwise constructing a ParameterizedTypeImpl where rawType has a non-null declaring class and lacks the static modifier, with ownerType == null (GsonTypes.java:522).","commonSituations":"Modeling JSON onto non-static inner classes; refactoring a static nested class to a (non-static) inner class and forgetting to add a TypeToken owner; reflecting on thread-state or context objects that nest as inner classes.","solutions":["Provide an explicit owner type when constructing the parameterized type, e.g. new ParameterizedTypeImpl(ownerType, rawType, args).","Make the nested class static (preferred) so no owner is required: add the `static` modifier to the inner class.","Restructure the model so nested types are top-level or static nested classes when you do not need the enclosing instance."],"exampleFix":"// before\nclass Outer { class Inner<T> {} }\nType t = TypeToken.getParameterized(Outer.Inner.class, String.class).getType(); // throws\n// after (option A: static)\nclass Outer { static class Inner<T> {} }\n// after (option B: give owner)\nType t = new ParameterizedTypeImpl(Outer.class, Outer.Inner.class, String.class).getType();","handlingStrategy":"validation","validationCode":"boolean needsOwner(Class<?> raw) {\n  return !Modifier.isStatic(raw.getModifiers()) && raw.getDeclaringClass() != null;\n}\n// before building: if (needsOwner(raw) && owner == null) owner = raw.getDeclaringClass();","typeGuard":"static boolean requiresOwner(Class<?> c) {\n  return com.google.gson.internal.GsonTypes.requiresOwnerType(c);\n}","tryCatchPattern":null,"preventionTips":["Make nested types static by default; only use inner classes when you need the enclosing instance.","When constructing parameterized types programmatically, check requiresOwnerType and supply an owner.","Document the owner requirement on any non-static nested generic type you expose."],"tags":["gson","reflection","generics","type-token","inner-class"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}