Skip to content

Default generic type arguments when resolving KType for Class. #3048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3041-SNAPSHOT</version>

<name>Spring Data Core</name>
<description>Core Spring concepts underpinning every Spring Data module.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import kotlin.reflect.KProperty;
import kotlin.reflect.KType;
import kotlin.reflect.KTypeParameter;
import kotlin.reflect.KTypeProjection;
import kotlin.reflect.jvm.ReflectJvmMapping;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -39,6 +41,7 @@
* Utilities for Kotlin Value class support.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 3.2
*/
class KotlinValueUtils {
Expand Down Expand Up @@ -72,7 +75,28 @@ public static ValueBoxing getConstructorValueHierarchy(KParameter parameter) {
public static ValueBoxing getConstructorValueHierarchy(Class<?> cls) {

KClass<?> kotlinClass = JvmClassMappingKt.getKotlinClass(cls);
return new ValueBoxing(BoxingRules.CONSTRUCTOR, Reflection.typeOf(kotlinClass), kotlinClass, false);
KType kType = extractKType(kotlinClass);
return new ValueBoxing(BoxingRules.CONSTRUCTOR, kType, kotlinClass, false);
}

/**
* Get the {@link KType} for a given {@link KClass} and potentially fill missing generic type arguments with
* {@link KTypeProjection#star} to prevent Kotlin internal checks to fail.
*
* @param kotlinClass
* @return
*/
private static KType extractKType(KClass<?> kotlinClass) {

return kotlinClass.getTypeParameters().isEmpty() ? Reflection.typeOf(kotlinClass)
: Reflection.typeOf(JvmClassMappingKt.getJavaClass(kotlinClass), stubKTypeProjections(kotlinClass));
}

private static KTypeProjection[] stubKTypeProjections(KClass<?> kotlinClass) {

KTypeProjection[] kTypeProjections = new KTypeProjection[kotlinClass.getTypeParameters().size()];
Arrays.fill(kTypeProjections, KTypeProjection.star);
return kTypeProjections;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test
import org.springframework.data.annotation.PersistenceConstructor
import org.springframework.data.annotation.Persistent
import org.springframework.data.mapping.PersistentEntity
import org.springframework.data.mapping.context.SamplePersistentProperty
import org.springframework.data.mapping.model.KotlinValueUtils.BoxingRules
import kotlin.jvm.internal.Reflection
import kotlin.reflect.KClass

/**
Expand Down Expand Up @@ -149,6 +152,28 @@ class KotlinClassGeneratingEntityInstantiatorUnitTests {
assertThat(instance.aBool).isTrue()
}

@Test // GH-3041
fun `should pick preferred constructor if multiple with same argument count are present`() {

val entity =
mockk<PersistentEntity<WithConstructorsHavingSameParameterCount, SamplePersistentProperty>>()
val constructor =
PreferredConstructorDiscoverer.discover<WithConstructorsHavingSameParameterCount, SamplePersistentProperty>(
WithConstructorsHavingSameParameterCount::class.java
)

every { provider.getParameterValue<Any>(any()) }.returns(1L).andThen(null)
every { entity.instanceCreatorMetadata } returns constructor
every { entity.type } returns constructor!!.constructor.declaringClass
every { entity.typeInformation } returns mockk()

val instance: WithConstructorsHavingSameParameterCount =
KotlinClassGeneratingEntityInstantiator().createInstance(entity, provider)

assertThat(instance.id).isEqualTo(1L)
assertThat(instance.notes).isEmpty();
}

@Test // DATACMNS-1338
fun `should create instance using @PersistenceConstructor`() {

Expand Down Expand Up @@ -271,6 +296,11 @@ class KotlinClassGeneratingEntityInstantiatorUnitTests {
val aFloat: Float = 0.0f, val aDouble: Double = 0.0, val aChar: Char = 'a', val aBool: Boolean = true
)


data class WithConstructorsHavingSameParameterCount @PersistenceConstructor constructor(val id: Long?, val notes: Map<String, String> = emptyMap()) {
constructor(notes: Map<String, String>, additionalNotes: Map<String, String> = emptyMap()) : this(null, notes + additionalNotes)
}

data class ContactWithPersistenceConstructor(val firstname: String, val lastname: String) {

@PersistenceConstructor
Expand Down