spring-projects/spring-framework · error · IllegalStateException
Failed to find Java constructor for Kotlin primary…
Error message
Failed to find Java constructor for Kotlin primary constructor: {clazz.getName()} What it means
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.
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...).
Example fix
// before MyBean bean = BeanUtils.instantiateClass(MyKotlinValueClass.class); // after - resolve a concrete constructor Constructor<MyBean> ctor = MyKotlinValueClass.class.getDeclaredConstructors()[0]; ReflectionUtils.makeAccessible(ctor); MyBean bean = ctor.newInstance();
Defensive patterns
Strategy: validation
Validate before calling
// before BeanUtils.instantiateClass on a possibly-Kotlin class
Constructor<?> ctor = BeanUtils.findPrimaryConstructor(type);
if (ctor == null) {
ctor = type.getDeclaredConstructors()[0]; // explicit fallback
}
ReflectionUtils.makeAccessible(ctor);
Object instance = ctor.newInstance(); Type guard
static boolean hasResolvableJavaConstructor(Class<?> clazz) {
try {
return BeanUtils.findPrimaryConstructor(clazz) != null
|| clazz.getDeclaredConstructors().length > 0;
} catch (IllegalStateException ex) {
return false;
}
} Try / catch
try {
return BeanUtils.instantiateClass(kotlinType);
} catch (IllegalStateException ex) {
// Kotlin primary constructor unmappable - resolve manually
Constructor<?> c = kotlinType.getDeclaredConstructors()[0];
ReflectionUtils.makeAccessible(c);
return c.newInstance();
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Constructor threw exception
- Illegal arguments for constructor
- Cannot create default implementation for
- Cannot resolve method
- Could not copy property
AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09).
Data as JSON: /api/errors/275f2e1bb2f51d16.
Report an issue: GitHub.
Appendix: source
Thrown at spring-beans/src/main/java/org/springframework/beans/BeanUtils.java:889
* https://kotlinlang.org/docs/reference/classes.html#constructors</a>
*/
@SuppressWarnings("unchecked")
public static <T> @Nullable Constructor<T> findPrimaryConstructor(Class<T> clazz) {
try {
KClass<T> kClass = JvmClassMappingKt.getKotlinClass(clazz);
KFunction<T> primaryCtor = KClasses.getPrimaryConstructor(kClass);
if (primaryCtor == null) {
return null;
}
if (KotlinDetector.isInlineClass(clazz)) {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
Assert.state(constructors.length == 1,
"Kotlin value classes annotated with @JvmInline are expected to have a single JVM constructor");
return (Constructor<T>) constructors[0];
}
Constructor<T> constructor = ReflectJvmMapping.getJavaConstructor(primaryCtor);
if (constructor == null) {
throw new IllegalStateException(
"Failed to find Java constructor for Kotlin primary constructor: " + clazz.getName());
}
return constructor;
}
catch (UnsupportedOperationException ex) {
return null;
}
}
/**
* Instantiate a Kotlin class using the provided constructor.
* @param ctor the constructor of the Kotlin class to instantiate
* @param args the constructor arguments to apply
* (use {@code null} for unspecified parameter if needed)
*/
public static <T> T instantiateClass(Constructor<T> ctor, @Nullable Object... args)
throws IllegalAccessException, InvocationTargetException, InstantiationException {
View on GitHub (pinned to 69bf83ad71)