{"record":{"id":"275f2e1bb2f51d16","repo":"spring-projects/spring-framework","slug":"failed-to-find-java-constructor-for-kotlin-primary","errorCode":null,"errorMessage":"Failed to find Java constructor for Kotlin primary constructor: {clazz.getName()}","messagePattern":"Failed to find Java constructor for Kotlin primary constructor: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/BeanUtils.java","lineNumber":889,"sourceCode":"\t\t * https://kotlinlang.org/docs/reference/classes.html#constructors</a>\n\t\t */\n\t\t@SuppressWarnings(\"unchecked\")\n\t\tpublic static <T> @Nullable Constructor<T> findPrimaryConstructor(Class<T> clazz) {\n\t\t\ttry {\n\t\t\t\tKClass<T> kClass = JvmClassMappingKt.getKotlinClass(clazz);\n\t\t\t\tKFunction<T> primaryCtor = KClasses.getPrimaryConstructor(kClass);\n\t\t\t\tif (primaryCtor == null) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\t\t\t\tif (KotlinDetector.isInlineClass(clazz)) {\n\t\t\t\t\tConstructor<?>[] constructors = clazz.getDeclaredConstructors();\n\t\t\t\t\tAssert.state(constructors.length == 1,\n\t\t\t\t\t\t\t\"Kotlin value classes annotated with @JvmInline are expected to have a single JVM constructor\");\n\t\t\t\t\treturn (Constructor<T>) constructors[0];\n\t\t\t\t}\n\t\t\t\tConstructor<T> constructor = ReflectJvmMapping.getJavaConstructor(primaryCtor);\n\t\t\t\tif (constructor == null) {\n\t\t\t\t\tthrow new IllegalStateException(\n\t\t\t\t\t\t\t\"Failed to find Java constructor for Kotlin primary constructor: \" + clazz.getName());\n\t\t\t\t}\n\t\t\t\treturn constructor;\n\t\t\t}\n\t\t\tcatch (UnsupportedOperationException ex) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * Instantiate a Kotlin class using the provided constructor.\n\t\t * @param ctor the constructor of the Kotlin class to instantiate\n\t\t * @param args the constructor arguments to apply\n\t\t * (use {@code null} for unspecified parameter if needed)\n\t\t */\n\t\tpublic static <T> T instantiateClass(Constructor<T> ctor, @Nullable Object... args)\n\t\t\t\tthrows IllegalAccessException, InvocationTargetException, InstantiationException {\n","sourceCodeStart":871,"sourceCodeEnd":907,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java#L871-L907","documentation":"Thrown by the internal KotlinDelegate.findPrimaryConstructor when a Kotlin primary constructor was found via the Kotlin reflection API but ReflectJvmMapping.getJavaConstructor(primaryCtor) returned null, so Spring cannot map the Kotlin constructor to a Java Constructor for instantiation. This is an IllegalStateException, indicating an unusual mismatch between the Kotlin reflection model and the JVM constructor model.","triggerScenarios":"Instantiating a Kotlin class through BeanUtils.instantiateClass(clazz) / BeanUtils.findPrimaryConstructor(clazz) on a class compiled by Kotlin where the primary constructor exists in Kotlin metadata but has no mappable Java Constructor (e.g. certain inline/value classes with unusual shapes, compiler-version mismatches, or classes generated by older/newer kotlin-reflect).","commonSituations":"Mixing kotlin-reflect versions (kotlin-reflect version != compiler version that produced the classes), instantiating a Kotlin value/inline class with an atypical constructor layout, or running on a Kotlin class whose primary constructor is synthetic and not directly mappable.","solutions":["Align kotlin-reflect and the Kotlin compiler version used to compile the class on the same Kotlin release.","Avoid passing Kotlin value/inline (@JvmInline) classes through BeanUtils.instantiateClass; instantiate them directly.","If you must instantiate, resolve a constructor explicitly via clazz.getDeclaredConstructors() and call instantiateClass(Constructor, args...)."],"exampleFix":"// before\nMyBean bean = BeanUtils.instantiateClass(MyKotlinValueClass.class);\n\n// after - resolve a concrete constructor\nConstructor<MyBean> ctor = MyKotlinValueClass.class.getDeclaredConstructors()[0];\nReflectionUtils.makeAccessible(ctor);\nMyBean bean = ctor.newInstance();","handlingStrategy":"validation","validationCode":"// before BeanUtils.instantiateClass on a possibly-Kotlin class\nConstructor<?> ctor = BeanUtils.findPrimaryConstructor(type);\nif (ctor == null) {\n    ctor = type.getDeclaredConstructors()[0]; // explicit fallback\n}\nReflectionUtils.makeAccessible(ctor);\nObject instance = ctor.newInstance();","typeGuard":"static boolean hasResolvableJavaConstructor(Class<?> clazz) {\n    try {\n        return BeanUtils.findPrimaryConstructor(clazz) != null\n            || clazz.getDeclaredConstructors().length > 0;\n    } catch (IllegalStateException ex) {\n        return false;\n    }\n}","tryCatchPattern":"try {\n    return BeanUtils.instantiateClass(kotlinType);\n} catch (IllegalStateException ex) {\n    // Kotlin primary constructor unmappable - resolve manually\n    Constructor<?> c = kotlinType.getDeclaredConstructors()[0];\n    ReflectionUtils.makeAccessible(c);\n    return c.newInstance();\n}","preventionTips":["Keep kotlin-reflect and the Kotlin compiler version aligned.","Avoid routing Kotlin value/inline classes through BeanUtils.instantiateClass.","Pin the Kotlin version in your build to avoid surprise upgrades."],"tags":["kotlin","instantiation","reflection","spring-beans"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}