{"id":"377dcbff995123b3","repo":"google/gson","slug":"failed-to-invoke-constructor-constructor-with","errorCode":null,"errorMessage":"Failed to invoke constructor '{constructor}' with no args","messagePattern":"Failed to invoke constructor '(.+?)' with no args","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java","lineNumber":268,"sourceCode":"    // Only try to make accessible if allowed; in all other cases checks above should\n    // have verified that constructor is accessible\n    if (filterResult == FilterResult.ALLOW) {\n      String exceptionMessage = ReflectionHelper.tryMakeAccessible(constructor);\n      if (exceptionMessage != null) {\n        return new ThrowingObjectConstructor<>(exceptionMessage);\n      }\n    }\n\n    return () -> {\n      try {\n        @SuppressWarnings(\"unchecked\") // T is the same raw type as is requested\n        T newInstance = (T) constructor.newInstance();\n        return newInstance;\n      }\n      // Note: InstantiationException should be impossible because check at start of method made\n      // sure that class is not abstract\n      catch (InstantiationException e) {\n        throw new RuntimeException(\n            \"Failed to invoke constructor '\"\n                + ReflectionHelper.constructorToString(constructor)\n                + \"' with no args\",\n            e);\n      } catch (InvocationTargetException e) {\n        // TODO: don't wrap if cause is unchecked?\n        // TODO: JsonParseException ?\n        throw new RuntimeException(\n            \"Failed to invoke constructor '\"\n                + ReflectionHelper.constructorToString(constructor)\n                + \"' with no args\",\n            e.getCause());\n      } catch (IllegalAccessException e) {\n        throw ReflectionHelper.createExceptionForUnexpectedIllegalAccess(e);\n      }\n    };\n  }\n","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java#L250-L286","documentation":"newDefaultConstructor reflectively invokes the no-args constructor; InstantiationException indicates the JVM refused instantiation (class is abstract, array, primitive, or interface — though abstract is pre-filtered). It is wrapped as RuntimeException with the constructor signature.","triggerScenarios":"Deserializing into an abstract class that slipped past the abstract check via a synthetic bridge; an array or interface type reaching the reflective path; class loaded with a non-instantiable metadata shape.","commonSituations":"Rare; usually follows a custom InstanceCreator/TypeAdapter wiring bug; reflective binding to types Gson cannot allocate (local/anonymous classes, arrays as bean types).","solutions":["Register an InstanceCreator<T> supplying a concrete instance.","Target a concrete subclass instead of an abstract/interface type.","Register a TypeAdapter<T> that constructs the object manually.","Ensure the type is a normal top-level or static nested class, not a local/anonymous one."],"exampleFix":"// before\nGson gson = new Gson();\nAbstractShape s = gson.fromJson(json, AbstractShape.class);\n\n// after\nGson gson = new GsonBuilder()\n    .registerTypeAdapter(AbstractShape.class, (InstanceCreator<AbstractShape>) t -> new Circle())\n    .create();","handlingStrategy":"fallback","validationCode":"if (Modifier.isAbstract(rawType.getModifiers()) || rawType.isInterface() || rawType.isArray()) {\n  // cannot instantiate via reflection; register InstanceCreator\n}\n","typeGuard":"boolean isReflectivelyInstantiable(Class<?> c) {\n  return !c.isInterface() && !Modifier.isAbstract(c.getModifiers())\n      && !c.isArray() && !c.isPrimitive();\n}\n","tryCatchPattern":"try {\n  return gson.fromJson(json, type);\n} catch (RuntimeException ex) {\n  if (ex.getMessage().contains(\"Failed to invoke constructor\")) {\n    // register InstanceCreator/TypeAdapter and retry with concrete subtype\n  }\n  throw ex;\n}\n","preventionTips":["Target concrete non-abstract types for deserialization.","Register InstanceCreator for abstract/interface types.","Use a TypeAdapter that builds the object manually."],"tags":["deserialization","reflection","instantiation","abstract-type"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}