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 "
                  + typeArgument

View on GitHub (pinned to 8b8628c656)

Solutions

  1. Make the inner class static so it no longer requires an owner type.
  2. Use a top-level class or a static nested class for the target type.
  3. If you cannot change the class, construct the parameterized type with an explicit owner using GsonTypes.newParameterizedTypeWithOwner(ownerType, rawClass, typeArgs).
  4. 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

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


AI-assisted analysis of google/gson@8b8628c656 (2026-08-04). Data as JSON: /data/errors/afad088cc3c1ceba.json. Report an issue: GitHub.