Browse Source

Fix constructor resolution for Kotlin classes using unsigned types.

We now leniently skip parameter name resolution for types using unsigned types. The issue is caused by Kotlin's DefaultConstructorMarker that doesn't report a parameter name.

Closes #2215
2.3.x
Mark Paluch 5 years ago
parent
commit
07dad67c38
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 2
      src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java
  2. 36
      src/test/kotlin/org/springframework/data/mapping/model/PreferredConstructorDiscovererUnitTests.kt

2
src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java

@ -212,7 +212,7 @@ public interface PreferredConstructorDiscoverer<T, P extends PersistentProperty< @@ -212,7 +212,7 @@ public interface PreferredConstructorDiscoverer<T, P extends PersistentProperty<
for (int i = 0; i < parameterTypes.size(); i++) {
String name = parameterNames == null ? null : parameterNames[i];
String name = parameterNames == null || parameterNames.length <= i ? null : parameterNames[i];
TypeInformation<?> type = parameterTypes.get(i);
Annotation[] annotations = parameterAnnotations[i];

36
src/test/kotlin/org/springframework/data/mapping/model/PreferredConstructorDiscovererUnitTests.kt

@ -79,13 +79,27 @@ class PreferredConstructorDiscovererUnitTests { @@ -79,13 +79,27 @@ class PreferredConstructorDiscovererUnitTests {
@Suppress("UNCHECKED_CAST")
fun `should not resolve constructor for synthetic Kotlin class`() {
val c = Class.forName("org.springframework.data.mapping.model.TypeCreatingSyntheticClassKt") as Class<Any>
val c =
Class.forName("org.springframework.data.mapping.model.TypeCreatingSyntheticClassKt") as Class<Any>
val constructor = PreferredConstructorDiscoverer.discover<Any, SamplePersistentProperty>(c)
val constructor =
PreferredConstructorDiscoverer.discover<Any, SamplePersistentProperty>(c)
assertThat(constructor).isNull()
}
@Test // DATACMNS-1800, gh-2215
@ExperimentalUnsignedTypes
fun `should discover constructor for class using unsigned types`() {
val constructor =
PreferredConstructorDiscoverer.discover<UnsignedTypesEntity, SamplePersistentProperty>(
UnsignedTypesEntity::class.java
)
assertThat(constructor).isNotNull()
}
data class Simple(val firstname: String)
class TwoConstructorsWithoutDefault {
@ -111,9 +125,23 @@ class PreferredConstructorDiscovererUnitTests { @@ -111,9 +125,23 @@ class PreferredConstructorDiscovererUnitTests {
class DefaultConstructor(val firstname: String = "foo")
class TwoDefaultConstructorsAnnotated(val firstname: String = "foo", val lastname: String = "bar") {
class TwoDefaultConstructorsAnnotated(
val firstname: String = "foo",
val lastname: String = "bar"
) {
@PersistenceConstructor
constructor(firstname: String = "foo", lastname: String = "bar", age: Int) : this(firstname, lastname)
constructor(firstname: String = "foo", lastname: String = "bar", age: Int) : this(
firstname,
lastname
)
}
@ExperimentalUnsignedTypes
data class UnsignedTypesEntity(
val id: String,
val a: UInt = 5u,
val b: Int = 5,
val c: Double = 1.5
)
}

Loading…
Cancel
Save