google/gson · error · JsonIOException
Unable to invoke no-args constructor of ${rawType}; construc
Error message
Unable to invoke no-args constructor of ${rawType}; constructor is not accessible and ReflectionAccessFilter does not permit making it accessible. Register an InstanceCreator or a TypeAdapter for this type, change the visibility of the constructor or adjust the access filter. What it means
Thrown when a no-args constructor exists but is not accessible (private/protected/package-private) and the ReflectionAccessFilter does not permit making it accessible (BLOCK_INACCESSIBLE or BLOCK_ALL). The filter prevents the setAccessible(true) call that Gson would otherwise use.
Source
Thrown at gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java:424
* ObjectConstructor}, which would then choose another way of creating the object. And it supports
* types which are only serialized but not deserialized (compared to directly throwing an
* exception when the {@code ObjectConstructor} is requested), e.g. when the runtime type of an
* object is inaccessible, but the compile-time type is accessible.
*/
private static final class ThrowingObjectConstructor<T> implements ObjectConstructor<T> {
private final String exceptionMessage;
ThrowingObjectConstructor(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
}
@Override
public T construct() {
// New exception is created every time to avoid keeping a reference to an exception with
// potentially long stack trace, causing a memory leak
// (which would happen if the exception was already created when the
// `ThrowingObjectConstructor` is created)
throw new JsonIOException(exceptionMessage);
}
}
private static final class InstanceCreatorConstructor<T> implements ObjectConstructor<T> {
private final InstanceCreator<T> instanceCreator;
private final Type type;
InstanceCreatorConstructor(InstanceCreator<T> instanceCreator, Type type) {
this.instanceCreator = instanceCreator;
this.type = type;
}
@Override
public T construct() {
return instanceCreator.createInstance(type);
}
}
}View on GitHub (pinned to 310ac341f2)
Solutions
- Change the no-args constructor visibility to public so no setAccessible is needed
- Register an InstanceCreator for the type to bypass reflective construction
- Adjust the ReflectionAccessFilter to ALLOW the specific type
- Add a module 'opens' directive so Gson can access the package
Example fix
// before
class Foo { private Foo() {} }
// + filter returning BLOCK_INACCESSIBLE
// after
class Foo { public Foo() {} } Defensive patterns
Strategy: validation
Validate before calling
// Check constructor accessibility given the active filter
Constructor<?> ctor = MyType.class.getDeclaredConstructor();
FilterResult result = myFilter.check(MyType.class);
boolean accessibleByDefault = ReflectionAccessFilterHelper.canAccess(ctor, null);
if (!accessibleByDefault && result != FilterResult.ALLOW) {
throw new IllegalStateException(
MyType + " no-args ctor inaccessible and filter is " + result + "; make ctor public or register InstanceCreator");
} Try / catch
try {
return gson.fromJson(json, MyType.class);
} catch (JsonIOException e) {
if (e.getMessage().startsWith("Unable to invoke no-args constructor") && e.getMessage().contains("not accessible")) {
// make ctor public, widen filter, or register InstanceCreator
throw new MyParseException("Constructor inaccessible for " + MyType.class, e);
}
throw e;
} Prevention
- Make no-args constructors public on classes you want Gson to deserialize, so setAccessible isn't needed
- If you use BLOCK_INACCESSIBLE filters, register InstanceCreators for types with non-public constructors
- Add module 'opens' directives for packages Gson needs to access under JPMS
- Test on the target JDK with the same module/filter configuration as production
When it happens
Trigger: A class has a non-public no-args constructor; a ReflectionAccessFilter with BLOCK_INACCESSIBLE or BLOCK_ALL is registered for the type; the canAccess check fails or the filter blocks the accessibility escalation.
Common situations: Java 9+ JPMS modules where the package isn't open to Gson; private constructors with strict reflection filters; using the built-in BLOCK_INACCESSIBLE_JAVA filter; library classes with package-private constructors.
Related errors
- Unable to create instance of ${rawType}; ReflectionAccessFil
- ${memberDescription} is not accessible and ReflectionAccessF
- cannot deserialize ${baseType} subtype named ${label}; did y
- Failed parsing JSON source to Json
- Invalid EnumSet type: ${type}
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/bbb521d296b891cf.
Report an issue: GitHub.