{"record":{"id":"84733f532ce9fc32","repo":"google/gson","slug":"failed-making-accessible-either-increase-its-v","errorCode":null,"errorMessage":"Failed making {} accessible; either increase its visibility or write a custom TypeAdapter for its declaring type.{}","messagePattern":"Failed making (.+?) accessible; either increase its visibility or write a custom TypeAdapter for its declaring type\\.(.+?)","errorType":"exception","errorClass":"JsonIOException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java","lineNumber":71,"sourceCode":"              : \"reflection-inaccessible\";\n      return \"\\nSee \" + TroubleshootingGuide.createUrl(troubleshootingId);\n    }\n    return \"\";\n  }\n\n  /**\n   * Internal implementation of making an {@link AccessibleObject} accessible.\n   *\n   * @param object the object that {@link AccessibleObject#setAccessible(boolean)} should be called\n   *     on.\n   * @throws JsonIOException if making the object accessible fails\n   */\n  public static void makeAccessible(AccessibleObject object) throws JsonIOException {\n    try {\n      object.setAccessible(true);\n    } catch (Exception exception) {\n      String description = getAccessibleObjectDescription(object, false);\n      throw new JsonIOException(\n          \"Failed making \"\n              + description\n              + \" accessible; either increase its visibility\"\n              + \" or write a custom TypeAdapter for its declaring type.\"\n              + getInaccessibleTroubleshootingSuffix(exception),\n          exception);\n    }\n  }\n\n  /**\n   * Returns a short string describing the {@link AccessibleObject} in a human-readable way. The\n   * result is normally shorter than {@link AccessibleObject#toString()} because it omits modifiers\n   * (e.g. {@code final}) and uses simple names for constructor and method parameter types.\n   *\n   * @param object object to describe\n   * @param uppercaseFirstLetter whether the first letter of the description should be uppercased\n   */\n  public static String getAccessibleObjectDescription(","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java#L53-L89","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add `opens your.package to gson` (or `opens your.package to ALL-UNNAMED`) in your module-info.java.","Launch with --add-opens your.package/com.example=gson (or =ALL-UNNAMED) JVM flags for the affected packages.","Make the field/constructor/method non-private (increase visibility) so reflection is not needed.","Register a custom TypeAdapter or InstanceCreator for the affected declaring type so Gson never reflects on it.","Use Gson's ReflectionAccessFilter to block or control reflection on problematic types."],"exampleFix":"// before: module-info.java without opens -> fails for private fields\nmodule my.app { requires gson; }\n\n// after: open the package to gson\nmodule my.app {\n  requires gson;\n  opens com.example.model to gson; // or to ALL-UNNAMED\n}\n\n// or JVM launch flag (quick fix without module-info edit):\njava --add-opens my.app/com.example.model=gson -jar app.jar","handlingStrategy":"validation","validationCode":"// fail fast at startup: open a known private field reflectively to verify access\nField f = MyModel.class.getDeclaredField(\"id\");\ntry {\n  f.setAccessible(true);\n} catch (InaccessibleObjectException ex) {\n  throw new IllegalStateException(\"Module does not open package to gson; add 'opens' to module-info or --add-opens\", ex);\n}","typeGuard":null,"tryCatchPattern":"try {\n  return gson.fromJson(json, MyModel.class);\n} catch (JsonIOException e) {\n  if (e.getMessage().startsWith(\"Failed making \") && e.getMessage().contains(\"accessible\")) {\n    // surface a clear operational hint with the path/module to open\n    throw new IllegalStateException(\"Reflection blocked by JPMS; open the package to gson\", e);\n  }\n  throw e;\n}","preventionTips":["Declare `opens <package> to gson` (or ALL-UNNAMED) in module-info for model packages.","Run a startup smoke test that constructs a Gson instance and round-trips a sample object.","Prefer non-private constructors/fields for classes Gson must reflect on, or supply InstanceCreators.","Keep --add-opens flags documented in deployment scripts alongside the JVM launch line."],"tags":["gson","reflection","java-modules","jpms","inaccessible-object-exception","json-io-exception"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}