{"id":"86c95a01a93e095f","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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java#L871-L907","documentation":"Thrown inside the private KotlinDelegate.findPrimaryConstructor (IllegalStateException) when the class is detected as Kotlin and a primary constructor exists, but kotlin.reflect.jvm.ReflectJvmMapping.getJavaConstructor returned null - the Kotlin-to-Java constructor mapping could not be resolved. This is an internal Spring failure to bridge Kotlin reflection to a Java Constructor, surfaced when BeanUtils.instantiateClass resolves constructor args for a Kotlin bean.","triggerScenarios":"BeanUtils.findPrimaryConstructor(clazz) on a Kotlin class whose primary constructor is present in Kotlin metadata but has no resolvable Java Constructor (obfuscated bytecode, unusual compiler-generated synthetic constructors, mismatched kotlin-reflect runtime version vs. the Kotlin compiler that produced the class).","commonSituations":"Upgrading the Kotlin compiler without bumping kotlin-reflect on the classpath; R8/ProGuard stripping the constructor metadata; Kotlin classes compiled with experimental language versions; Spring trying to autowire a Kotlin data class when kotlin-reflect is missing or a different version.","solutions":["Ensure kotlin-reflect version exactly matches the Kotlin stdlib version used to compile the class.","If the class does not need primary-constructor resolution, annotate it with a plain Java-style constructor or @JvmOverloads so Spring falls back to Java resolution.","Disable bytecode shrinking/obfuscation for Kotlin metadata, or add keep rules for constructors.","Report to Spring if reproducible with aligned versions - getJavaConstructor returning null for a valid primary constructor is a bridge bug."],"exampleFix":"// before: kotlin-reflect 1.6 on classpath, bean compiled with Kotlin 1.9\nimplementation \"org.jetbrains.kotlin:kotlin-reflect:1.6.21\"\n\n// after: align reflect to the compiler\nimplementation \"org.jetbrains.kotlin:kotlin-reflect:1.9.24\"\nimplementation \"org.jetbrains.kotlin:kotlin-stdlib:1.9.24\"","handlingStrategy":"validation","validationCode":"// Confirm kotlin-reflect is present and version-aligned before instantiation\ntry {\n    Class<?> kd = Class.forName(\"kotlin.reflect.jvm.ReflectJvmMapping\");\n    // optionally compare KotlinVersion to compiled metadata\n} catch (ClassNotFoundException e) {\n    throw new IllegalStateException(\"kotlin-reflect missing on classpath\");\n}\nConstructor<?> c = BeanUtils.findPrimaryConstructor(clazz);\nif (c == null) {\n    // fall back: use a declared constructor explicitly\n    c = clazz.getDeclaredConstructors()[0];\n}","typeGuard":"// Verify the class actually has a resolvable Java constructor before Spring touches it\nstatic boolean isKotlinBeanInstantiable(Class<?> clazz) {\n    if (!KotlinDetector.isKotlinPresent() || !KotlinDetector.isKotlinType(clazz)) return true;\n    Constructor<?> c = BeanUtils.findPrimaryConstructor(clazz);\n    return c != null;\n}","tryCatchPattern":"try {\n    Constructor<T> ctor = BeanUtils.findPrimaryConstructor(clazz);\n    return BeanUtils.instantiateClass(ctor == null ? clazz.getDeclaredConstructor() : ctor);\n} catch (IllegalStateException ex) {\n    if (ex.getMessage().contains(\"Failed to find Java constructor for Kotlin\")) {\n        // fall back to a manually chosen declared constructor\n        Constructor<T> fallback = clazz.getDeclaredConstructors()[0];\n        ReflectionUtils.makeAccessible(fallback);\n        return fallback.newInstance();\n    }\n    throw ex;\n}","preventionTips":["Pin kotlin-reflect to the exact Kotlin stdlib version used to compile the beans.","Add ProGuard/R8 keep rules for Kotlin metadata and constructors.","Annotate Kotlin beans with @JvmOverloads or explicit Java constructors when they must be reflectively instantiated.","Test Spring instantiation of every Kotlin bean in a focused unit test."],"tags":["kotlin","reflection","constructor","spring-beans","version-mismatch"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}