{"id":"a43c23aab24fd83b","repo":"google/gson","slug":"memberdescription-is-not-accessible-and-reflec","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":"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\\.","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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java#L155-L191","documentation":"Thrown by checkAccessible() when a ReflectionAccessFilter returned BLOCK_INACCESSIBLE for a type and a specific field, accessor method, or constructor cannot be reached by the calling code without making it accessible. Gson refuses to call setAccessible(true) when the filter forbids it, so serialization or deserialization of that member aborts with a JsonIOException. The error is not about Java module visibility per se, but about the access policy the filter established.","triggerScenarios":"Occurs when a ReflectionAccessFilter is registered on GsonBuilder (addReflectionAccessFilter) returning FilterResult.BLOCK_INACCESSIBLE, and the target class has private/package-private fields (or an inaccessible record constructor) that Gson then tries to read/write reflectively. Fires from BoundField.write (line 225/229), readIntoField (line 277), or RecordAdapter constructor (line 586).","commonSituations":"Common on JPMS modular projects where types in another module are not exported; library hardening configs that blanket-block inaccessible members; serializing third-party library classes whose fields are private with no public accessors; mixing records with non-public canonical constructors under a restrictive filter.","solutions":["Register a custom TypeAdapter for the declaring type so Gson does not fall back to reflection for it.","Loosen the ReflectionAccessFilter to return ALLOW for that specific type/class, or remove the BLOCK_INACCESSIBLE filter.","Increase visibility of the field (or its class) to public so no setAccessible is required.","Add `opens` directive for the package in module-info.java (JPMS) so reflective deep access is permitted.","For records, ensure the canonical constructor is at least package-accessible and the filter permits it."],"exampleFix":"// before\nGson gson = new GsonBuilder()\n    .addReflectionAccessFilter((c) -> FilterResult.BLOCK_INACCESSIBLE)\n    .create();\ngson.toJson(internalPackageObject); // throws\n\n// after: allow this specific type\n.addReflectionAccessFilter((c) ->\n    c.getType() == MyType.class ? FilterResult.ALLOW : FilterResult.BLOCK_INACCESSIBLE)\n// or register an adapter\n.registerTypeAdapter(MyType.class, new MyTypeAdapter())","handlingStrategy":"validation","validationCode":"// Before building Gson, verify the filter allows your types\nClass<?> target = MyType.class;\nFilterResult r = myFilter.apply(new ReflectionAccessFilter.FilterContext(target));\nif (r == FilterResult.BLOCK_INACCESSIBLE) {\n  Field f = target.getDeclaredField(\"sensitiveField\");\n  if (!ReflectionAccessFilterHelper.canAccess(f, null)) {\n    throw new IllegalStateException(\"Will fail: \" + f + \" is inaccessible under filter\");\n  }\n}","typeGuard":"null","tryCatchPattern":"try {\n  gson.toJson(obj);\n} catch (JsonIOException e) {\n  if (e.getMessage().contains(\"is not accessible and ReflectionAccessFilter\")) {\n    // fall back to a manually-registered adapter or skip the field\n    log.warn(\"Access blocked for {}\", obj.getClass(), e);\n  } else throw e;\n}","preventionTips":["Keep a whitelist of types allowed for reflection and register adapters for everything else.","Document each ReflectionAccessFilter's intent in the GsonBuilder construction.","Test serialization of every type reachable from your public API under the production filter.","Prefer registering TypeAdapters over relying on deep reflection for third-party types."],"tags":["gson","reflection","access-filter","jpms","serialization"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}