{"record":{"id":"d2254194f51e2db8","repo":"google/gson","slug":"unable-to-create-instance-of-rawtype-usage-of","errorCode":null,"errorMessage":"Unable to create instance of ${rawType}; usage of JDK Unsafe is disabled. Registering an InstanceCreator or a TypeAdapter for this type, adding a no-args constructor, or enabling usage of JDK Unsafe may fix this problem.","messagePattern":"Unable to create instance of (.+?); usage of JDK Unsafe is disabled\\. Registering an InstanceCreator or a TypeAdapter for this type, adding a no-args constructor, or enabling usage of JDK Unsafe may fix this problem\\.","errorType":"exception","errorClass":"JsonIOException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java","lineNumber":424,"sourceCode":"   * ObjectConstructor}, which would then choose another way of creating the object. And it supports\n   * types which are only serialized but not deserialized (compared to directly throwing an\n   * exception when the {@code ObjectConstructor} is requested), e.g. when the runtime type of an\n   * object is inaccessible, but the compile-time type is accessible.\n   */\n  private static final class ThrowingObjectConstructor<T> implements ObjectConstructor<T> {\n    private final String exceptionMessage;\n\n    ThrowingObjectConstructor(String exceptionMessage) {\n      this.exceptionMessage = exceptionMessage;\n    }\n\n    @Override\n    public T construct() {\n      // New exception is created every time to avoid keeping a reference to an exception with\n      // potentially long stack trace, causing a memory leak\n      // (which would happen if the exception was already created when the\n      // `ThrowingObjectConstructor` is created)\n      throw new JsonIOException(exceptionMessage);\n    }\n  }\n\n  private static final class InstanceCreatorConstructor<T> implements ObjectConstructor<T> {\n    private final InstanceCreator<T> instanceCreator;\n    private final Type type;\n\n    InstanceCreatorConstructor(InstanceCreator<T> instanceCreator, Type type) {\n      this.instanceCreator = instanceCreator;\n      this.type = type;\n    }\n\n    @Override\n    public T construct() {\n      return instanceCreator.createInstance(type);\n    }\n  }\n}","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java#L406-L442","documentation":"Thrown when GsonBuilder.disableJdkUnsafe() has been called (useJdkUnsafe=false), the type has no accessible no-args constructor, no InstanceCreator, and no special/default-implementation constructor applies. Unsafe would be the only remaining option but it is disabled. If R8 removed all constructors, the message appends an R8-specific hint.","triggerScenarios":"Gson configured with GsonBuilder.disableJdkUnsafe(); deserializing a class without a no-args constructor and without a registered InstanceCreator/TypeAdapter. The newUnsafeAllocator path returns a ThrowingObjectConstructor because useJdkUnsafe is false.","commonSituations":"Applications that disable Unsafe for safety/predictability (recommended for Android and JDK 16+); test environments requiring explicit constructors; R8 stripping constructors on top of disabled Unsafe.","solutions":["Add a no-args constructor to the class (preferred)","Register an InstanceCreator for the type","Register a custom TypeAdapter for the type","If R8 removed constructors, add keep rules: -keep class com.example.MyClass { <init>(); }","Re-enable Unsafe by removing disableJdkUnsafe() (less safe, not recommended for production)"],"exampleFix":"// before\ngsonBuilder.disableJdkUnsafe();\nclass Foo { public Foo(String s) {} } // no no-args ctor\n\n// after\nclass Foo { public Foo() {} public Foo(String s) {} }","handlingStrategy":"validation","validationCode":"// When using disableJdkUnsafe(), verify no-args constructors at startup\nClass<?> c = MyType.class;\nboolean hasNoArgs;\ntry { c.getDeclaredConstructor(); hasNoArgs = true; }\ncatch (NoSuchMethodException e) { hasNoArgs = false; }\nif (!hasNoArgs && !hasInstanceCreator(c)) {\n  throw new IllegalStateException(c + \" has no no-args ctor and Unsafe is disabled; add ctor or InstanceCreator\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  return gson.fromJson(json, MyType.class);\n} catch (JsonIOException e) {\n  if (e.getMessage().contains(\"usage of JDK Unsafe is disabled\")) {\n    // add no-args ctor, register InstanceCreator, or re-enable Unsafe\n    throw new MyParseException(\"Unsafe disabled and no ctor for \" + MyType.class, e);\n  }\n  throw e;\n}","preventionTips":["With disableJdkUnsafe(), ensure every deserialized class has a no-args constructor","Register InstanceCreators for classes that can't have no-args constructors","Add R8/ProGuard keep rules: -keep class com.example.** { <init>(); }","Run a startup self-test that attempts construction of all model types"],"tags":["unsafe","constructor","disable-unsafe","instantiation","instance-creator"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}