google/gson · error · IllegalArgumentException
Raw type " + rawClass.getName() + " is not supported because
Error message
Raw type " + rawClass.getName() + " is not supported because it requires specifying an owner type
What it means
Thrown by TypeToken.getParameterized when the raw class requires an owner type (GsonTypes.requiresOwnerType returns true). Inner non-static member classes and certain nested types carry an implicit owner instance, and Gson cannot construct a correct parameterized type without that owner being specified. Rather than emit a malformed type, getParameterized rejects it up front.
Source
Thrown at gson/src/main/java/com/google/gson/reflect/TypeToken.java:416
int expectedArgsCount = typeVariables.length;
int actualArgsCount = typeArguments.length;
if (actualArgsCount != expectedArgsCount) {
throw new IllegalArgumentException(
rawClass.getName()
+ " requires "
+ expectedArgsCount
+ " type arguments, but got "
+ actualArgsCount);
}
// For legacy reasons create a TypeToken(Class) if the type is not generic
if (typeArguments.length == 0) {
return get(rawClass);
}
// Check for this here to avoid misleading exception thrown by ParameterizedTypeImpl
if (GsonTypes.requiresOwnerType(rawType)) {
throw new IllegalArgumentException(
"Raw type "
+ rawClass.getName()
+ " is not supported because it requires specifying an owner type");
}
for (int i = 0; i < expectedArgsCount; i++) {
Type typeArgument =
Objects.requireNonNull(typeArguments[i], "Type argument must not be null");
Class<?> rawTypeArgument = GsonTypes.getRawType(typeArgument);
TypeVariable<?> typeVariable = typeVariables[i];
for (Type bound : typeVariable.getBounds()) {
Class<?> rawBound = GsonTypes.getRawType(bound);
if (!rawBound.isAssignableFrom(rawTypeArgument)) {
throw new IllegalArgumentException(
"Type argument "
+ typeArgumentView on GitHub (pinned to 8b8628c656)
Solutions
- Make the inner class static so it no longer requires an owner type.
- Use a top-level class or a static nested class for the target type.
- If you cannot change the class, construct the parameterized type with an explicit owner using GsonTypes.newParameterizedTypeWithOwner(ownerType, rawClass, typeArgs).
- Deserialize into the raw class via TypeToken.get(rawClass) if generics are not needed.
Example fix
// before
class Outer {
class Inner<T> { T value; } // non-static, needs owner
}
TypeToken.getParameterized(Outer.Inner.class, String.class); // throws
// after
class Outer {
static class Inner<T> { T value; } // static, no owner needed
}
TypeToken.getParameterized(Outer.Inner.class, String.class); Defensive patterns
Strategy: validation
Validate before calling
import java.lang.reflect.Modifier;
boolean isStaticOrTopLevel(Class<?> c) {
return c.getEnclosingClass() == null || Modifier.isStatic(c.getModifiers());
}
// before calling getParameterized:
if (!isStaticOrTopLevel(rawClass)) {
throw new IllegalArgumentException(rawClass + " requires an owner; make it static or use newParameterizedTypeWithOwner");
} Type guard
static boolean isOwnerFree(Class<?> c) {
return c.getEnclosingClass() == null || Modifier.isStatic(c.getModifiers());
} Prevention
- Prefer static nested classes or top-level classes for Gson target types.
- Audit inner classes used as deserialization targets and add the 'static' modifier where possible.
- Use GsonTypes.newParameterizedTypeWithOwner when an owner type is genuinely required.
- Add a test that fails if a target class fails the owner-free check.
When it happens
Trigger: Passing a non-static inner class (e.g. an inner class Outer.Inner) as rawType to getParameterized. Because the class is declared as a member of Outer, its ParameterizedTypeImpl would need an owner type, which getParameterized always sets to null (TypeToken.java:443).
Common situations: Deserializing into a non-static inner class whose enclosing instance cannot be supplied by Gson; using model classes that were accidentally declared non-static (common in DTO/VO code reviews); working with nested builder classes.
Related errors
- Must specify owner type for {rawType}
- TypeToken type argument must not contain a type variable; ca
- TypeToken captured `null` as type argument; probably a compi
- rawType must be of type Class, but was " + rawType
- Primitive type is not allowed
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/afad088cc3c1ceba.json.
Report an issue: GitHub.