google/gson · error · JsonIOException

Failed making {} accessible; either increase its visibility

Error message

Failed making {} accessible; either increase its visibility or write a custom TypeAdapter for its declaring type.{}

What it means

Thrown by ReflectionHelper.makeAccessible when AccessibleObject.setAccessible(true) throws on a Field/Method/Constructor. On Java 9+ this is most commonly InaccessibleObjectException because the declaring module does not open the package to gson (or to all unnamed). The message appends a troubleshooting URL pointing at the reflection-inaccessible guide.

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 310ac341f2)

Solutions

  1. Add `opens your.package to gson` (or `opens your.package to ALL-UNNAMED`) in your module-info.java.
  2. Launch with --add-opens your.package/com.example=gson (or =ALL-UNNAMED) JVM flags for the affected packages.
  3. Make the field/constructor/method non-private (increase visibility) so reflection is not needed.
  4. Register a custom TypeAdapter or InstanceCreator for the affected declaring type so Gson never reflects on it.
  5. Use Gson's ReflectionAccessFilter to block or control reflection on problematic types.

Example fix

// before: module-info.java without opens -> fails for private fields
module my.app { requires gson; }

// after: open the package to gson
module my.app {
  requires gson;
  opens com.example.model to gson; // or to ALL-UNNAMED
}

// or JVM launch flag (quick fix without module-info edit):
java --add-opens my.app/com.example.model=gson -jar app.jar
Defensive patterns

Strategy: validation

Validate before calling

// fail fast at startup: open a known private field reflectively to verify access
Field f = MyModel.class.getDeclaredField("id");
try {
  f.setAccessible(true);
} catch (InaccessibleObjectException ex) {
  throw new IllegalStateException("Module does not open package to gson; add 'opens' to module-info or --add-opens", ex);
}

Try / catch

try {
  return gson.fromJson(json, MyModel.class);
} catch (JsonIOException e) {
  if (e.getMessage().startsWith("Failed making ") && e.getMessage().contains("accessible")) {
    // surface a clear operational hint with the path/module to open
    throw new IllegalStateException("Reflection blocked by JPMS; open the package to gson", e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Gson reflectively accesses a private field/constructor/method of a class in a JPMS module whose package is not 'opens' to gson. Triggered during deserialization (or serialization) of any type Gson falls back to reflection on, when the JVM enforces strong encapsulation.

Common situations: Java 9+ applications using modules without `opens ... to gson` (or `opens ... to ALL-UNNAMED`); libraries consumed as automatic modules whose internals Gson needs; records/classes with private no-arg constructors; running on modern JDK without --add-opens flags; Spring Boot fat jars that lose module openness.

Related errors


AI-assisted analysis of google/gson@310ac341f2 (2026-08-10). Data as JSON: /api/errors/84733f532ce9fc32. Report an issue: GitHub.