{"record":{"id":"e74cb494339f19a9","repo":"apache/beam","slug":"no-matching-constructor-found-for-class-clazz","errorCode":null,"errorMessage":"No matching constructor found for class ${clazz}","messagePattern":"No matching constructor found for class (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/schemas/utils/AvroByteBuddyUtils.java","lineNumber":76,"sourceCode":"      Maps.newConcurrentMap();\n\n  static <T extends SpecificRecord> SchemaUserTypeCreator getCreator(\n      Class<T> clazz, Schema schema) {\n    return CACHED_CREATORS.computeIfAbsent(\n        ClassWithSchema.create(clazz, schema), c -> createCreator(clazz, schema));\n  }\n\n  private static <T> SchemaUserTypeCreator createCreator(Class<T> clazz, Schema schema) {\n    Constructor baseConstructor = null;\n    Constructor[] constructors = clazz.getDeclaredConstructors();\n    for (Constructor constructor : constructors) {\n      // TODO: This assumes that Avro only generates one constructor with this many fields.\n      if (constructor.getParameterCount() == schema.getFieldCount()) {\n        baseConstructor = constructor;\n      }\n    }\n    if (baseConstructor == null) {\n      throw new RuntimeException(\"No matching constructor found for class \" + clazz);\n    }\n\n    // Generate a method call to create and invoke the SpecificRecord's constructor. .\n    MethodCall construct = MethodCall.construct(baseConstructor);\n    for (int i = 0; i < baseConstructor.getParameterTypes().length; ++i) {\n      Class<?> baseType = baseConstructor.getParameterTypes()[i];\n      construct = construct.with(readAndConvertParameter(baseType, i), baseType);\n    }\n\n    try {\n      DynamicType.Builder<SchemaUserTypeCreator> builder =\n          BYTE_BUDDY\n              .with(new InjectPackageStrategy(clazz))\n              .subclass(SchemaUserTypeCreator.class)\n              .method(ElementMatchers.named(\"create\"))\n              .intercept(construct);\n\n      return builder","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/schemas/utils/AvroByteBuddyUtils.java#L58-L94","documentation":"AvroByteBuddyUtils generates fast object constructors for Avro SpecificRecords via ByteBuddy. It expects the record class to have a constructor whose parameter count equals the schema's field count; if none is found it throws RuntimeException.","triggerScenarios":"Calling AvroByteBuddyUtils.getCreator (used by AvroSchemaUtil / converters) with a class whose declared constructors don't match the schema field count — e.g. non-SpecificRecord classes, manually edited records, or a schema whose fieldCount disagrees with the generated class.","commonSituations":"Using AvroSchemaUtil.toJavaBean-style conversion with plain POJOs instead of Avro-generated classes, stale generated classes after schema evolution, or hand-written classes implementing SpecificRecord.","solutions":["Use an Avro-generated SpecificRecord class matching the schema (regenerate with avro-maven-plugin)","Ensure the schema passed matches the class's actual fields","Fall back to reflection-based conversion instead of ByteBuddy (set the appropriate AvroUtils option)"],"exampleFix":"// before\nAvroByteBuddyUtils.getTypeCreator(myPlainPojoClass, schema);\n// after (regenerate class from schema first)\nmvn generate-sources // avro-maven-plugin produces MyRecord with matching constructor\nAvroByteBuddyUtils.getTypeCreator(MyRecord.class, schema);","handlingStrategy":"validation","validationCode":"long ctors = Arrays.stream(clazz.getConstructors())\n    .filter(c -> c.getParameterCount() == schema.getFieldCount()).count();\nif (ctors == 0) throw new IllegalStateException(clazz + \" has no constructor matching \" + schema.getFieldCount() + \" fields\");","typeGuard":"static boolean hasMatchingConstructor(Class<?> c, Schema s) {\n  return Arrays.stream(c.getConstructors())\n      .anyMatch(k -> k.getParameterCount() == s.getFields().size());\n}","tryCatchPattern":"try { getCreator(clazz, schema); } catch (RuntimeException e) { /* fall back to reflection-based conversion */ }","preventionTips":["Only pass Avro-generated SpecificRecord classes to ByteBuddy converters","Regenerate classes whenever the schema changes","Verify schema field count matches class constructor arity in tests"],"tags":["avro","bytebuddy","codegen"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}