{"record":{"id":"4ac13052a3b0de92","repo":"google/gson","slug":"memberdescription-is-not-accessible-and-reflect","errorCode":null,"errorMessage":"${memberDescription} is not accessible and ReflectionAccessFilter does not permit making it accessible. Register a TypeAdapter for the declaring type, adjust the access filter or increase the visibility of the element and its declaring type.","messagePattern":"(.+?) is not accessible and ReflectionAccessFilter does not permit making it accessible\\. Register a TypeAdapter for the declaring type, adjust the access filter or increase the visibility of the element and its declaring type\\.","errorType":"exception","errorClass":"JsonIOException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java","lineNumber":173,"sourceCode":"      @SuppressWarnings(\"unchecked\")\n      TypeAdapter<T> adapter =\n          (TypeAdapter<T>)\n              new RecordAdapter<>(\n                  raw, getBoundFields(gson, type, raw, blockInaccessible, true), blockInaccessible);\n      return adapter;\n    }\n\n    ObjectConstructor<T> constructor = constructorConstructor.get(type, true);\n    return new FieldReflectionAdapter<>(\n        constructor, getBoundFields(gson, type, raw, blockInaccessible, false));\n  }\n\n  private static <M extends AccessibleObject & Member> void checkAccessible(\n      Object object, M member) {\n    if (!ReflectionAccessFilterHelper.canAccess(\n        member, Modifier.isStatic(member.getModifiers()) ? null : object)) {\n      String memberDescription = ReflectionHelper.getAccessibleObjectDescription(member, true);\n      throw new JsonIOException(\n          memberDescription\n              + \" is not accessible and ReflectionAccessFilter does not permit making it\"\n              + \" accessible. Register a TypeAdapter for the declaring type, adjust the access\"\n              + \" filter or increase the visibility of the element and its declaring type.\");\n    }\n  }\n\n  private BoundField createBoundField(\n      Gson context,\n      Field field,\n      Method accessor,\n      String serializedName,\n      TypeToken<?> fieldType,\n      boolean serialize,\n      boolean blockInaccessible) {\n\n    boolean isPrimitive = Primitives.isPrimitive(fieldType.getRawType());\n","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java#L155-L191","documentation":"When a ReflectionAccessFilter returns BLOCK_INACCESSIBLE, Gson will not call setAccessible(true) on fields/constructors. checkAccessible() then verifies the member is accessible under normal Java access rules; if it is not (e.g. a private field in a non-exported package, or an inaccessible record constructor), it throws JsonIOException. This respects Java module encapsulation (JPMS) and access boundaries rather than forcing accessibility.","triggerScenarios":"Deserializing a type with private/inaccessible fields under a BLOCK_INACCESSIBLE filter; serializing records whose accessor/constructor is not accessible; JPMS strong encapsulation (Java 16+ default) denying access to a package.","commonSituations":"Java 16+ with strong encapsulation; modules that do not 'opens' their packages to Gson; library types with restrictive visibility; switching a filter from ALLOW to BLOCK_INACCESSIBLE.","solutions":["Register a custom TypeAdapter for the declaring type so reflection on its fields is unnecessary","Change the ReflectionAccessFilter to ALLOW for that type (if acceptable under your security model)","Increase visibility of the element and its declaring type (make the field/package-accessible, or add 'opens' in module-info for JPMS)","Annotate the field with @JsonAdapter to supply a non-reflective adapter"],"exampleFix":"// before - private field inaccessible under BLOCK_INACCESSIBLE\nclass Box { private int secret; }\ngson.fromJson(json, Box.class); // throws\n\n// after option 1 - widen visibility\nclass Box { int secret; }\n\n// after option 2 - register a TypeAdapter\nGson gson = new GsonBuilder()\n    .registerTypeAdapter(Box.class, new BoxAdapter())\n    .create();","handlingStrategy":"type-guard","validationCode":"// Pre-check field accessibility before relying on reflective (de)serialization\nField f = MyType.class.getDeclaredField(\"secret\");\nboolean accessible = Modifier.isPublic(f.getModifiers())\n    || (f.canAccess(instance));\nif (!accessible) {\n    throw new IllegalStateException(\"Field not accessible under BLOCK_INACCESSIBLE: \" + f);\n}","typeGuard":"// Confirm a member is accessible under current access rules\nstatic boolean isAccessible(Field f, Object instance) {\n    try {\n        return f.canAccess(instance);\n    } catch (Exception e) {\n        return false;\n    }\n}","tryCatchPattern":"try {\n  MyType obj = gson.fromJson(json, MyType.class);\n} catch (com.google.gson.JsonIOException e) {\n  // '... is not accessible...': widen visibility, open the package (JPMS), or register a TypeAdapter\n}","preventionTips":["Widen field/package visibility or add 'opens' in module-info for types you control","Register a TypeAdapter for types whose members cannot be made accessible","Prefer records with accessible components, or annotate fields with @JsonAdapter"],"tags":["json","reflection","security","access-filter","jpms","encapsulation"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}