{"record":{"id":"cf04c6f42b78d78e","repo":"quarkusio/quarkus","slug":"unable-to-create-new-instance-for-clazz","errorCode":null,"errorMessage":"Unable to create new instance for ${clazz}","messagePattern":"Unable to create new instance for (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"extensions/avro/runtime/src/main/java/io/quarkus/avro/runtime/graal/AvroSubstitutions.java","lineNumber":83,"sourceCode":"    }\n\n}\n\n@TargetClass(className = \"org.apache.avro.reflect.ReflectionUtil\")\nfinal class Target_org_apache_avro_reflect_ReflectionUtil {\n\n    @Substitute\n    public static <V, R> Function<V, R> getConstructorAsFunction(Class<V> parameterClass, Class<R> clazz) {\n        // Cannot use the method handle approach as it uses ProtectionDomain which got removed.\n        try {\n            Constructor<R> constructor = clazz.getConstructor(parameterClass);\n            return new Function<V, R>() {\n                @Override\n                public R apply(V v) {\n                    try {\n                        return constructor.newInstance(v);\n                    } catch (Exception e) {\n                        throw new IllegalStateException(\"Unable to create new instance for \" + clazz, e);\n                    }\n                }\n            };\n        } catch (Throwable t) {\n            // if something goes wrong, do not provide a Function instance\n            return null;\n        }\n    }\n}\n\nclass AvroSubstitutions {\n}\n","sourceCodeStart":65,"sourceCodeEnd":96,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/avro/runtime/src/main/java/io/quarkus/avro/runtime/graal/AvroSubstitutions.java#L65-L96","documentation":"In the Quarkus Avro GraalVM substitution (AvroSubstitutions), a Function that instantiates a class reflectively via constructor.newInstance wraps any reflective failure in an IllegalStateException naming the class. This runs at native-image runtime when Avro needs to create a specific record instance from a value; failures mean the constructor is inaccessible or the class/constructor was not registered for reflection.","triggerScenarios":"At native runtime, Avro's ReflectDatumReader or similar needs to construct an instance and the substituted factory's Constructor.newInstance(value) throws (IllegalAccessException, IllegalArgumentException, or InstantiationException wrapped as Exception).","commonSituations":"Native-image build missing @RegisterForReflection on the record/POJO; constructor signature mismatch (e.g. single-arg constructor absent); class removed by native reachability analysis.","solutions":["Annotate the affected class with @io.quarkus.runtime.annotations.RegisterForReflection (and its field types)","Check the chained cause: IllegalArgumentException means wrong constructor arity/type; IllegalAccessException means constructor is not public","Ensure the class has a constructor matching the expected single-value signature","Rebuild the native image after adding reflection registration"],"exampleFix":"// before\npublic class MyDto { public MyDto(String v) {...} } // no reflection registration\n// after\n@RegisterForReflection\npublic class MyDto { public MyDto(String v) {...} }","handlingStrategy":"validation","validationCode":"// ensure native reflection registration before building native image\nboolean registered = MyDto.class.isAnnotationPresent(\n    io.quarkus.runtime.annotations.RegisterForReflection.class);\nif (!registered && System.getProperty(\"native.image\", \"false\").equals(\"true\")) {\n    throw new IllegalStateException(\"Add @RegisterForReflection to \" + MyDto.class.getName());\n}","typeGuard":null,"tryCatchPattern":"try {\n    R value = avroReadNext(reader);\n} catch (IllegalStateException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Unable to create new instance for\")) {\n        log.error(\"Register \" + e.getMessage().replace(\"Unable to create new instance for \", \"\")\n            + \" for reflection in native mode\");\n    }\n    throw e;\n}","preventionTips":["Annotate Avro record/POJO classes with @RegisterForReflection for native builds","Test the native image early, not only JVM mode","Keep constructor signatures matching what Avro's reflect reader expects","Inspect the chained cause to distinguish access vs arity errors"],"tags":["avro","graalvm","native-image","reflection","quarkus"],"backgroundTag":"native-reflection-not-registered","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}