DATACMNS-1170 - Fallback to default constructor discovery for Kotlin classes without primary constructor.
We now fall back to default preferred constructor discovery if a Kotlin class has no primary constructor and no preferred constructor is resolved. Previously, primary constructor resolution yielded no result (returned null) which caused the subsequent Java constructor lookup to fail. Original pull request: #244.
This commit is contained in:
committed by
Oliver Gierke
parent
93c65c02fe
commit
041c18bef9
@@ -166,6 +166,11 @@ public interface PreferredConstructorDiscoverer<T, P extends PersistentProperty<
|
||||
|
||||
KFunction<T> primaryConstructor = KClasses
|
||||
.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(type.getType()));
|
||||
|
||||
if (primaryConstructor == null) {
|
||||
return DEFAULT.discover(type, entity);
|
||||
}
|
||||
|
||||
Constructor<T> javaConstructor = ReflectJvmMapping.getJavaConstructor(primaryConstructor);
|
||||
|
||||
return javaConstructor != null ? buildPreferredConstructor(javaConstructor, type, entity) : null;
|
||||
|
||||
@@ -43,6 +43,14 @@ class PreferredConstructorDiscovererUnitTests {
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1170
|
||||
fun `should fall back to no-args constructor if no primary constructor available`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<TwoConstructorsWithoutDefault, SamplePersistentProperty>(TwoConstructorsWithoutDefault::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters).isEmpty()
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should discover annotated constructor`() {
|
||||
|
||||
@@ -69,6 +77,17 @@ class PreferredConstructorDiscovererUnitTests {
|
||||
|
||||
data class Simple(val firstname: String)
|
||||
|
||||
class TwoConstructorsWithoutDefault {
|
||||
|
||||
var firstname: String? = null
|
||||
|
||||
constructor() {}
|
||||
|
||||
constructor(firstname: String?) {
|
||||
this.firstname = firstname
|
||||
}
|
||||
}
|
||||
|
||||
class TwoConstructors(val firstname: String) {
|
||||
constructor(firstname: String, lastname: String) : this(firstname)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user