google/gson · error · JsonIOException

Failed making " + description + " accessible; either increas

Error message

Failed making " + description + " accessible; either increase its visibility or write a custom TypeAdapter for its declaring type." + getInaccessibleTroubleshootingSuffix(exception)

What it means

ReflectionHelper.makeAccessible calls setAccessible(true) on a Field/Method/Constructor and catches any exception, wrapping it as JsonIOException. On Java 9+ the typical cause is InaccessibleObjectException because the declaring module does not open the package to Gson; the message appends a troubleshooting URL when that is detected.

Source

Thrown at gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java:71

              : "reflection-inaccessible";
      return "\nSee " + TroubleshootingGuide.createUrl(troubleshootingId);
    }
    return "";
  }

  /**
   * Internal implementation of making an {@link AccessibleObject} accessible.
   *
   * @param object the object that {@link AccessibleObject#setAccessible(boolean)} should be called
   *     on.
   * @throws JsonIOException if making the object accessible fails
   */
  public static void makeAccessible(AccessibleObject object) throws JsonIOException {
    try {
      object.setAccessible(true);
    } catch (Exception exception) {
      String description = getAccessibleObjectDescription(object, false);
      throw new JsonIOException(
          "Failed making "
              + description
              + " accessible; either increase its visibility"
              + " or write a custom TypeAdapter for its declaring type."
              + getInaccessibleTroubleshootingSuffix(exception),
          exception);
    }
  }

  /**
   * Returns a short string describing the {@link AccessibleObject} in a human-readable way. The
   * result is normally shorter than {@link AccessibleObject#toString()} because it omits modifiers
   * (e.g. {@code final}) and uses simple names for constructor and method parameter types.
   *
   * @param object object to describe
   * @param uppercaseFirstLetter whether the first letter of the description should be uppercased
   */
  public static String getAccessibleObjectDescription(

View on GitHub (pinned to 8b8628c656)

Solutions

  1. Add '--add-opens <module>/<package>=com.google.gson' (or ALL-UNNAMED) to your JVM launch args.
  2. If you own the module, add 'opens <package> to com.google.gson;' in module-info.java.
  3. Increase the field's visibility to package-public, or annotate it for Gson access.
  4. Register a custom TypeAdapter for the declaring type to avoid reflection entirely.
  5. Configure ReflectionAccessFilter to allow or block access intentionally.

Example fix

// before: deserializing a class in a non-open module throws
MyType obj = gson.fromJson(json, MyType.class);

// after: open the package at launch
// java --add-opens com.example/com.example.internal=ALL-UNNAMED -jar app.jar
// or in module-info.java of com.example:
// opens com.example.internal to com.google.gson;
Defensive patterns

Strategy: try-catch

Validate before calling

// No code-only validation; check module openness reflectively before Gson use.
boolean canAccess(Class<?> c, Field f) {
  try { f.setAccessible(true); return true; }
  catch (RuntimeException e) { return false; }
  finally { /* module not opened to your code either */ }
}

Type guard

static boolean isLikelyAccessible(Class<?> c) {
  return Modifier.isPublic(c.getModifiers()) || java.util.Arrays.stream(c.getDeclaredFields()).anyMatch(f -> Modifier.isPublic(f.getModifiers()));
}

Try / catch

try {
  T obj = gson.fromJson(json, type);
} catch (JsonIOException e) {
  if (e.getMessage().contains("Failed making") && e.getCause() instanceof InaccessibleObjectException) {
    // add --add-opens or a custom TypeAdapter, then retry
  } else throw e;
}

Prevention

When it happens

Trigger: Deserializing a class whose private fields are in a module (JDK or your own named module) that has not opened its package to Gson, or running under a SecurityManager that denies reflective access.

Common situations: Mapping JDK types (e.g. java.time, java.net) without a custom adapter, migrating to JPMS modules, upgrading to Java 16+ where strong encapsulation is enforced, or libraries that sealed their internals.

Related errors


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